Skip to content

Commit 7834c05

Browse files
authored
Remove calls to fstat in crashlytics (#12531)
1 parent fcf5ced commit 7834c05

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

Crashlytics/Crashlytics/Helpers/FIRCLSFile.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
static const size_t FIRCLSStringBufferLength = 16;
3434
const size_t FIRCLSWriteBufferLength = 1000;
3535

36-
static bool FIRCLSFileInit(FIRCLSFile* file, int fdm, bool appendMode, bool bufferWrites);
36+
static bool FIRCLSFileInit(
37+
FIRCLSFile* file, const char* path, int fdm, bool appendMode, bool bufferWrites);
3738

3839
static void FIRCLSFileWriteToFileDescriptorOrBuffer(FIRCLSFile* file,
3940
const char* string,
@@ -55,7 +56,8 @@ static void FIRCLSFileWriteToFileDescriptorOrBuffer(FIRCLSFile* file,
5556
#define CLS_FILE_DEBUG_LOGGING 0
5657

5758
#pragma mark - File Structure
58-
static bool FIRCLSFileInit(FIRCLSFile* file, int fd, bool appendMode, bool bufferWrites) {
59+
static bool FIRCLSFileInit(
60+
FIRCLSFile* file, const char* path, int fd, bool appendMode, bool bufferWrites) {
5961
if (!file) {
6062
FIRCLSSDKLog("Error: file is null\n");
6163
return false;
@@ -83,9 +85,16 @@ static bool FIRCLSFileInit(FIRCLSFile* file, int fd, bool appendMode, bool buffe
8385

8486
file->writtenLength = 0;
8587
if (appendMode) {
86-
struct stat fileStats;
87-
fstat(fd, &fileStats);
88-
off_t currentFileSize = fileStats.st_size;
88+
NSError* attributesError;
89+
NSString* objCPath = [NSString stringWithCString:path encoding:NSUTF8StringEncoding];
90+
NSDictionary* fileAttributes =
91+
[[NSFileManager defaultManager] attributesOfItemAtPath:objCPath error:&attributesError];
92+
if (attributesError != nil) {
93+
FIRCLSErrorLog(@"Failed to read filesize from %@ with error %@", objCPath, attributesError);
94+
return false;
95+
}
96+
NSNumber* fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
97+
long long currentFileSize = [fileSizeNumber longLongValue];
8998
if (currentFileSize > 0) {
9099
file->writtenLength += currentFileSize;
91100
}
@@ -133,7 +142,7 @@ bool FIRCLSFileInitWithPathMode(FIRCLSFile* file,
133142
}
134143
}
135144

136-
return FIRCLSFileInit(file, fd, appendMode, bufferWrites);
145+
return FIRCLSFileInit(file, path, fd, appendMode, bufferWrites);
137146
}
138147

139148
bool FIRCLSFileClose(FIRCLSFile* file) {

Crashlytics/Shared/FIRCLSMachO/FIRCLSMachO.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ static void FIRCLSMachOHeaderValues(FIRCLSMachOSliceRef slice,
4242
static bool FIRCLSMachOSliceIsValid(FIRCLSMachOSliceRef slice);
4343

4444
bool FIRCLSMachOFileInitWithPath(FIRCLSMachOFileRef file, const char* path) {
45-
struct stat statBuffer;
46-
4745
if (!file || !path) {
4846
return false;
4947
}
@@ -58,16 +56,23 @@ bool FIRCLSMachOFileInitWithPath(FIRCLSMachOFileRef file, const char* path) {
5856
return false;
5957
}
6058

61-
if (fstat(file->fd, &statBuffer) == -1) {
59+
NSError* attributesError;
60+
NSString* objCPath = [NSString stringWithCString:path encoding:NSUTF8StringEncoding];
61+
NSDictionary* fileAttributes =
62+
[[NSFileManager defaultManager] attributesOfItemAtPath:objCPath error:&attributesError];
63+
if (attributesError != nil) {
6264
close(file->fd);
6365
return false;
6466
}
67+
NSNumber* fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
68+
long long currentFileSize = [fileSizeNumber longLongValue];
69+
NSFileAttributeType fileType = [fileAttributes objectForKey:NSFileType];
6570

6671
// We need some minimum size for this to even be a possible mach-o file. I believe
6772
// its probably quite a bit bigger than this, but this at least covers something.
6873
// We also need it to be a regular file.
69-
file->mappedSize = (size_t)statBuffer.st_size;
70-
if (statBuffer.st_size < 16 || !(statBuffer.st_mode & S_IFREG)) {
74+
file->mappedSize = (size_t)currentFileSize;
75+
if (currentFileSize < 16 || ![fileType isEqualToString:NSFileTypeRegular]) {
7176
close(file->fd);
7277
return false;
7378
}

0 commit comments

Comments
 (0)