Skip to content

Commit 9d48a38

Browse files
committed
Refactor file size logic to fs_wrapper function
Signed-off-by: Dom Del Nano <[email protected]>
1 parent a6bb573 commit 9d48a38

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/common/fs/fs_wrapper.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ StatusOr<int64_t> SpaceAvailableInBytes(const std::filesystem::path& path) {
175175
return si.available;
176176
}
177177

178+
StatusOr<std::uintmax_t> GetFileSize(const std::string& binary_path) {
179+
PX_ASSIGN_OR_RETURN(const auto stat, fs::Stat(binary_path));
180+
if (stat.st_size < 0) {
181+
return error::Internal("stat() returned negative file size $0 for file $1", stat.st_size,
182+
binary_path);
183+
}
184+
return stat.st_size;
185+
}
186+
178187
StatusOr<bool> IsEmpty(const std::filesystem::path& f) {
179188
std::error_code ec;
180189
bool val = std::filesystem::is_empty(f, ec);

src/common/fs/fs_wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Status Chown(const std::filesystem::path& path, const uid_t uid, const gid_t gid
6969
StatusOr<struct stat> Stat(const std::filesystem::path& path);
7070
StatusOr<int64_t> SpaceAvailableInBytes(const std::filesystem::path& path);
7171

72+
StatusOr<std::uintmax_t> GetFileSize(const std::string& binary_path);
73+
7274
StatusOr<bool> IsEmpty(const std::filesystem::path& path);
7375

7476
StatusOr<std::filesystem::path> Absolute(const std::filesystem::path& path);

src/stirling/obj_tools/elf_reader.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,11 @@ Status ElfReader::LocateDebugSymbols(const std::filesystem::path& debug_file_dir
200200
StatusOr<std::unique_ptr<ElfReader>> ElfReader::Create(
201201
const std::string& binary_path, const std::filesystem::path& debug_file_dir) {
202202
if (FLAGS_elf_reader_max_file_size != 0) {
203-
PX_ASSIGN_OR_RETURN(const auto stat, fs::Stat(binary_path));
204-
int64_t file_size = stat.st_size;
203+
PX_ASSIGN_OR_RETURN(int64_t file_size, fs::GetFileSize(binary_path));
205204
if (file_size > FLAGS_elf_reader_max_file_size) {
206205
return error::Internal(
207206
"File size $0 exceeds ElfReader's max file size $1. Refusing to process file", file_size,
208207
FLAGS_elf_reader_max_file_size);
209-
} else if (file_size < 0) {
210-
return error::Internal("stat() returned negative file size $0 for file $1", file_size,
211-
binary_path);
212208
}
213209
}
214210
return CreateImpl(binary_path, debug_file_dir);

0 commit comments

Comments
 (0)