Check if the file readable.

This commit is contained in:
Robert Ormandi 2024-12-04 10:37:14 -06:00
parent ad5734c34b
commit 133d95fae6

View file

@ -509,30 +509,31 @@ static struct ggml_backend_metal_context * ggml_metal_init(ggml_backend_dev_t de
NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"]; NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
if (path_lib == nil) { if (path_lib == nil) {
// Try to find the resource in the directory where the current binary located. // Try to find the resource in the directory where the current binary located.
NSString *currentBinary = [[NSProcessInfo processInfo] arguments][0]; NSString *current_binary = [[NSProcessInfo processInfo] arguments][0];
NSString *binDir = [currentBinary stringByDeletingLastPathComponent]; NSString *bin_dir = [current_binary stringByDeletingLastPathComponent];
NSString *defaultMetalibPath = [NSString pathWithComponents:@[binDir, @"default.metallib"]]; NSString *default_metallib_path = [NSString pathWithComponents:@[bin_dir, @"default.metallib"]];
NSDictionary *atts = [[NSFileManager defaultManager] attributesOfItemAtPath:defaultMetalibPath error:&error]; if ([[NSFileManager defaultManager] isReadableFileAtPath:default_metallib_path]) {
if (error) { GGML_LOG_INFO("%s: found '%s'\n", __func__, [default_metallib_path UTF8String]);
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]); NSDictionary *atts = [[NSFileManager defaultManager] attributesOfItemAtPath:default_metallib_path error:&error];
return NULL; if (atts && atts[NSFileType] == NSFileTypeSymbolicLink) {
}
if (!atts) {
GGML_METAL_LOG_ERROR("%s: error: src path attribute is NULL\n", __func__);
return NULL;
}
if (atts[NSFileType] == NSFileTypeSymbolicLink) {
// Optionally, if this is a symlink, try to resolve it. // Optionally, if this is a symlink, try to resolve it.
defaultMetalibPath = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:defaultMetalibPath error:&error]; default_metallib_path = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:default_metallib_path error:&error];
if (error) { if (default_metallib_path && [default_metallib_path length] > 0 && ![[default_metallib_path substringToIndex:1] isEqualToString:@"/"]) {
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]); // It is a relative path, adding the binary directory as directory prefix.
return NULL; default_metallib_path = [NSString pathWithComponents:@[bin_dir, default_metallib_path]];
} }
if (defaultMetalibPath && [defaultMetalibPath length] > 0 && ![[defaultMetalibPath substringToIndex:1] isEqualToString:@"/"]) { if (!default_metallib_path || ![[NSFileManager defaultManager] isReadableFileAtPath:default_metallib_path]) {
defaultMetalibPath = [NSString pathWithComponents:@[binDir, defaultMetalibPath]]; // Link to the resource could not be resolved.
default_metallib_path = nil;
} else {
GGML_LOG_INFO("%s: symlink resolved '%s'\n", __func__, [default_metallib_path UTF8String]);
} }
} }
path_lib = defaultMetalibPath; } else {
// The resource couldn't be found in the binary's directory.
default_metallib_path = nil;
}
path_lib = default_metallib_path;
} }
if (try_metallib && path_lib != nil) { if (try_metallib && path_lib != nil) {