Skip to content

Commit 133d95f

Browse files
committed
Check if the file readable.
1 parent ad5734c commit 133d95f

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

ggml/src/ggml-metal/ggml-metal.m

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -509,30 +509,31 @@ @implementation GGMLMetalClass
509509
NSString * path_lib = [bundle pathForResource:@"default" ofType:@"metallib"];
510510
if (path_lib == nil) {
511511
// Try to find the resource in the directory where the current binary located.
512-
NSString *currentBinary = [[NSProcessInfo processInfo] arguments][0];
513-
NSString *binDir = [currentBinary stringByDeletingLastPathComponent];
514-
NSString *defaultMetalibPath = [NSString pathWithComponents:@[binDir, @"default.metallib"]];
515-
NSDictionary *atts = [[NSFileManager defaultManager] attributesOfItemAtPath:defaultMetalibPath error:&error];
516-
if (error) {
517-
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
518-
return NULL;
519-
}
520-
if (!atts) {
521-
GGML_METAL_LOG_ERROR("%s: error: src path attribute is NULL\n", __func__);
522-
return NULL;
523-
}
524-
if (atts[NSFileType] == NSFileTypeSymbolicLink) {
525-
// Optionally, if this is a symlink, try to resolve it.
526-
defaultMetalibPath = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:defaultMetalibPath error:&error];
527-
if (error) {
528-
GGML_METAL_LOG_ERROR("%s: error: %s\n", __func__, [[error description] UTF8String]);
529-
return NULL;
530-
}
531-
if (defaultMetalibPath && [defaultMetalibPath length] > 0 && ![[defaultMetalibPath substringToIndex:1] isEqualToString:@"/"]) {
532-
defaultMetalibPath = [NSString pathWithComponents:@[binDir, defaultMetalibPath]];
512+
NSString *current_binary = [[NSProcessInfo processInfo] arguments][0];
513+
NSString *bin_dir = [current_binary stringByDeletingLastPathComponent];
514+
NSString *default_metallib_path = [NSString pathWithComponents:@[bin_dir, @"default.metallib"]];
515+
if ([[NSFileManager defaultManager] isReadableFileAtPath:default_metallib_path]) {
516+
GGML_LOG_INFO("%s: found '%s'\n", __func__, [default_metallib_path UTF8String]);
517+
NSDictionary *atts = [[NSFileManager defaultManager] attributesOfItemAtPath:default_metallib_path error:&error];
518+
if (atts && atts[NSFileType] == NSFileTypeSymbolicLink) {
519+
// Optionally, if this is a symlink, try to resolve it.
520+
default_metallib_path = [[NSFileManager defaultManager] destinationOfSymbolicLinkAtPath:default_metallib_path error:&error];
521+
if (default_metallib_path && [default_metallib_path length] > 0 && ![[default_metallib_path substringToIndex:1] isEqualToString:@"/"]) {
522+
// It is a relative path, adding the binary directory as directory prefix.
523+
default_metallib_path = [NSString pathWithComponents:@[bin_dir, default_metallib_path]];
524+
}
525+
if (!default_metallib_path || ![[NSFileManager defaultManager] isReadableFileAtPath:default_metallib_path]) {
526+
// Link to the resource could not be resolved.
527+
default_metallib_path = nil;
528+
} else {
529+
GGML_LOG_INFO("%s: symlink resolved '%s'\n", __func__, [default_metallib_path UTF8String]);
530+
}
533531
}
532+
} else {
533+
// The resource couldn't be found in the binary's directory.
534+
default_metallib_path = nil;
534535
}
535-
path_lib = defaultMetalibPath;
536+
path_lib = default_metallib_path;
536537
}
537538

538539
if (try_metallib && path_lib != nil) {

0 commit comments

Comments
 (0)