Skip to content

Commit dc298a4

Browse files
committed
replace accidentalkly deleted file close
1 parent b3f95f9 commit dc298a4

1 file changed

Lines changed: 35 additions & 22 deletions

File tree

Source/NSData.m

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@
497497
}
498498
}
499499

500+
if (theFile != 0)
501+
{
502+
fclose(theFile);
503+
}
500504
if (buf)
501505
{
502506
*buf = tmp;
@@ -2018,6 +2022,7 @@ - (BOOL) writeToFile: (NSString *)path
20182022
NSFileManager *mgr = [NSFileManager defaultManager];
20192023
NSMutableDictionary *att = nil;
20202024
NSUInteger perm;
2025+
DWORD err;
20212026

20222027
if ([mgr fileExistsAtPath: path])
20232028
{
@@ -2044,48 +2049,56 @@ - (BOOL) writeToFile: (NSString *)path
20442049
{
20452050
c = 0;
20462051
}
2047-
/* Windows 9x does not support MoveFileEx */
2048-
else if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
2052+
else
20492053
{
2050-
GSNativeChar secondaryFile[length + 100];
2051-
2052-
wcscpy(secondaryFile, wthePath);
2053-
wcscat(secondaryFile, L"-delete");
2054-
// Delete the intermediate name just in case
2055-
DeleteFileW(secondaryFile);
2056-
// Move the existing file to the temp name
2057-
if (MoveFileW(wtheRealPath, secondaryFile) != 0)
2054+
/* Try harder if we can't use MoveFileEx */
2055+
err = GetLastError();
2056+
if (ERROR_CALL_NOT_IMPLEMENTED == err
2057+
|| ERROR_ACCESS_DENIED == err)
20582058
{
2059-
if (MoveFileW(wthePath, wtheRealPath) != 0)
2059+
GSNativeChar secondaryFile[length + 100];
2060+
2061+
wcscpy(secondaryFile, wthePath);
2062+
wcscat(secondaryFile, L"-delete");
2063+
// Delete the intermediate name just in case
2064+
DeleteFileW(secondaryFile);
2065+
// Move the existing file to the temp name
2066+
if (MoveFileW(wtheRealPath, secondaryFile) != 0)
20602067
{
2061-
c = 0;
2062-
// Delete the old file if possible
2063-
DeleteFileW(secondaryFile);
2068+
if (MoveFileW(wthePath, wtheRealPath) != 0)
2069+
{
2070+
c = 0;
2071+
// Delete the old file if possible
2072+
DeleteFileW(secondaryFile);
2073+
}
2074+
else
2075+
{
2076+
c = -1; // failure, restore the old file if possible
2077+
MoveFileW(secondaryFile, wtheRealPath);
2078+
}
20642079
}
20652080
else
20662081
{
2067-
c = -1; // failure, restore the old file if possible
2068-
MoveFileW(secondaryFile, wtheRealPath);
2082+
c = -1; // failure
20692083
}
2084+
err = GetLastError();
20702085
}
20712086
else
20722087
{
2073-
c = -1; // failure
2088+
c = -1;
20742089
}
20752090
}
2076-
else
2077-
{
2078-
c = -1;
2079-
}
20802091

20812092
if (c != 0) /* Many things could go wrong, I guess. */
20822093
{
2094+
NSError *e = [NSError _systemError: err];
2095+
20832096
NSWarnMLog(@"Rename ('%@' to '%@') failed - %@",
20842097
[NSString stringWithCharacters: wthePath
20852098
length: wcslen(wthePath)],
20862099
[NSString stringWithCharacters: wtheRealPath
20872100
length: wcslen(wtheRealPath)],
2088-
[NSError _last]);
2101+
e);
20892102
goto failure;
20902103
}
20912104

0 commit comments

Comments
 (0)