Eliminated a handful of deprecation warnings, as well as inconsistent handling of FS operations in OS X

This commit is contained in:
Thomas "elfprince13" Dickerson 2017-01-05 17:19:19 -05:00
parent 32f726dcc6
commit 0e30426def
2 changed files with 96 additions and 72 deletions

View file

@ -27,7 +27,7 @@
#endif #endif
#if defined(TORQUE_OS_MAC) #if defined(TORQUE_OS_MAC)
#include <CoreServices/CoreServices.h> // For high resolution timer #include <mach/mach_time.h>
#endif #endif
#include "core/stream/fileStream.h" #include "core/stream/fileStream.h"
@ -135,20 +135,26 @@ U32 endHighResolutionTimer(U32 time[2])
void startHighResolutionTimer(U32 time[2]) { void startHighResolutionTimer(U32 time[2]) {
UnsignedWide t; U64 now = mach_absolute_time();
Microseconds(&t); AssertFatal(sizeof(U32[2]) == sizeof(U64), "Can't pack mach_absolute_time into U32[2]");
time[0] = t.lo; memcpy(time, &now, sizeof(U64));
time[1] = t.hi;
} }
U32 endHighResolutionTimer(U32 time[2]) { U32 endHighResolutionTimer(U32 time[2]) {
UnsignedWide t; static mach_timebase_info_data_t sTimebaseInfo = {0, 0};
Microseconds(&t);
return t.lo - time[0]; U64 now = mach_absolute_time();
// given that we're returning a 32 bit integer, and this is unsigned subtraction... AssertFatal(sizeof(U32[2]) == sizeof(U64), "Can't pack mach_absolute_time into U32[2]");
// it will just wrap around, we don't need the upper word of the time. U64 then;
// NOTE: the code assumes that more than 3 hrs will not go by between calls to startHighResolutionTimer() and endHighResolutionTimer(). memcpy(&then, time, sizeof(U64));
// I mean... that damn well better not happen anyway.
if(sTimebaseInfo.denom == 0){
mach_timebase_info(&sTimebaseInfo);
}
// Handle the micros/nanos conversion first, because shedding a few bits is better than overflowing.
U64 elapsedMicros = ((now - then) / 1000) * sTimebaseInfo.numer / sTimebaseInfo.denom;
return (U32)elapsedMicros; // Just truncate, and hope we didn't overflow
} }
#else #else

View file

@ -68,11 +68,14 @@ bool dFileTouch(const char *path)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool dPathCopy(const char* source, const char* dest, bool nooverwrite) bool dPathCopy(const char* source, const char* dest, bool nooverwrite)
{ {
NSFileManager *manager = [NSFileManager defaultManager]; if(source == NULL || dest == NULL)
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; return false;
NSString *nsource = [[NSString stringWithUTF8String:source] stringByStandardizingPath]; @autoreleasepool {
NSString *ndest = [[NSString stringWithUTF8String:dest] stringByStandardizingPath]; NSFileManager *manager = [NSFileManager defaultManager];
NSString *nsource = [manager stringWithFileSystemRepresentation:source length:dStrlen(source)];
NSString *ndest = [manager stringWithFileSystemRepresentation:dest length:dStrlen(dest)];
NSString *ndestFolder = [ndest stringByDeletingLastPathComponent]; NSString *ndestFolder = [ndest stringByDeletingLastPathComponent];
if(! [manager fileExistsAtPath:nsource]) if(! [manager fileExistsAtPath:nsource])
@ -89,8 +92,7 @@ bool dPathCopy(const char* source, const char* dest, bool nooverwrite)
return false; return false;
} }
Con::warnf("Deleting files at path: %s", dest); Con::warnf("Deleting files at path: %s", dest);
bool deleted = [manager removeFileAtPath:ndest handler:nil]; if(![manager removeItemAtPath:ndest error:nil] || [manager fileExistsAtPath:ndest])
if(!deleted)
{ {
Con::errorf("Copy failed! Could not delete files at path: %s", dest); Con::errorf("Copy failed! Could not delete files at path: %s", dest);
return false; return false;
@ -103,10 +105,16 @@ bool dPathCopy(const char* source, const char* dest, bool nooverwrite)
Platform::createPath([ndestFolder UTF8String]); Platform::createPath([ndestFolder UTF8String]);
} }
bool ret = [manager copyPath:nsource toPath:ndest handler:nil]; bool ret = [manager copyItemAtPath:nsource toPath:ndest error:nil];
// n.b.: The "success" semantics don't guarantee a copy actually took place, so we'll verify
[pool release]; // because this is surprising behavior for a method called copy.
if( ![manager fileExistsAtPath:ndest] )
{
Con::warnf("The filemanager returned success, but the file was not copied. Something strange is happening");
ret = false;
}
return ret; return ret;
}
} }
@ -117,6 +125,7 @@ bool dFileRename(const char *source, const char *dest)
if(source == NULL || dest == NULL) if(source == NULL || dest == NULL)
return false; return false;
@autoreleasepool {
NSFileManager *manager = [NSFileManager defaultManager]; NSFileManager *manager = [NSFileManager defaultManager];
NSString *nsource = [manager stringWithFileSystemRepresentation:source length:dStrlen(source)]; NSString *nsource = [manager stringWithFileSystemRepresentation:source length:dStrlen(source)];
@ -133,10 +142,19 @@ bool dFileRename(const char *source, const char *dest)
Con::warnf("dFileRename: Deleting files at path: %s", dest); Con::warnf("dFileRename: Deleting files at path: %s", dest);
} }
bool ret = [manager movePath:nsource toPath:ndest handler:nil]; bool ret = [manager moveItemAtPath:nsource toPath:ndest error:nil];
// n.b.: The "success" semantics don't guarantee a move actually took place, so we'll verify
// because this is surprising behavior for a method called rename.
if( ![manager fileExistsAtPath:ndest] )
{
Con::warnf("The filemanager returned success, but the file was not moved. Something strange is happening");
ret = false;
}
return ret; return ret;
} }
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Constructors & Destructor // Constructors & Destructor