-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing ABI wrappers for readdir_r. The dirent struct size is different on 64/32bit platforms. b/302730696
- Loading branch information
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
starboard/shared/modular/cobalt_layer_posix_directory_abi_wrappers.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if SB_API_VERSION >= 16 | ||
|
||
#include <dirent.h> | ||
|
||
extern "C" { | ||
|
||
int __abi_wrap_readdir_r(DIR* dirp, | ||
struct dirent* entry, | ||
struct dirent** result); | ||
|
||
int readdir_r(DIR* dirp, struct dirent* entry, struct dirent** result) { | ||
return __abi_wrap_readdir_r(dirp, entry, result); | ||
} | ||
} | ||
|
||
#endif // SB_API_VERSION >= 16 |
49 changes: 49 additions & 0 deletions
49
starboard/shared/modular/starboard_layer_posix_directory_abi_wrappers.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "starboard/shared/modular/starboard_layer_posix_directory_abi_wrappers.h" | ||
#include <string.h> | ||
#include <algorithm> | ||
#include "starboard/common/log.h" | ||
|
||
int __abi_wrap_readdir_r(DIR* dirp, | ||
struct musl_dirent* musl_entry, | ||
struct musl_dirent** musl_result) { | ||
// readdir_r segfaults if any of those parameters are missing. | ||
SB_CHECK(dirp); | ||
SB_CHECK(musl_entry); | ||
SB_CHECK(musl_result); | ||
|
||
struct dirent entry; // The type from platform toolchain. | ||
struct dirent* result; | ||
int retval = readdir_r(dirp, &entry, &result); | ||
if (retval != 0) { | ||
return retval; | ||
} | ||
musl_entry->d_ino = entry.d_ino; | ||
musl_entry->d_off = entry.d_off; | ||
musl_entry->d_reclen = entry.d_reclen; | ||
musl_entry->d_type = entry.d_type; | ||
|
||
memset(musl_entry->d_name, 0, sizeof(musl_entry->d_name)); | ||
constexpr auto minlen = | ||
std::min(sizeof(musl_entry->d_name), sizeof(entry.d_name)); | ||
memcpy(musl_entry->d_name, entry.d_name, minlen); | ||
if (result == NULL) { | ||
*musl_result = NULL; | ||
} else { | ||
*musl_result = musl_entry; | ||
} | ||
return 0; | ||
} |
44 changes: 44 additions & 0 deletions
44
starboard/shared/modular/starboard_layer_posix_directory_abi_wrappers.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_DIRECTORY_ABI_WRAPPERS_H_ | ||
#define STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_DIRECTORY_ABI_WRAPPERS_H_ | ||
|
||
#include <dirent.h> | ||
#include <stdint.h> | ||
#include <sys/types.h> | ||
|
||
#include "starboard/export.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef struct musl_dirent { | ||
ino_t d_ino; | ||
int64_t /* off_t */ d_off; | ||
uint16_t /* short */ d_reclen; | ||
unsigned char d_type; | ||
char d_name[256]; | ||
} musl_dirent; | ||
|
||
SB_EXPORT int __abi_wrap_readdir_r(DIR* dirp, | ||
struct musl_dirent* entry, | ||
struct musl_dirent** result); | ||
|
||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif | ||
|
||
#endif // STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_DIRECTORY_ABI_WRAPPERS_H_ |