Skip to content

Commit 701aff9

Browse files
committed
add file existence check
1 parent 629b625 commit 701aff9

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

tools/mtmd/ane/ane.mm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
}
1616

1717
NSString *pathString = [NSString stringWithUTF8String:model_path];
18+
19+
// Check if file exists
20+
NSFileManager *fileManager = [NSFileManager defaultManager];
21+
if (![fileManager fileExistsAtPath:pathString]) {
22+
NSLog(@"Error: ANE model file does not exist at path: %@", pathString);
23+
return nullptr;
24+
}
25+
26+
// Check if it's a directory (for .mlmodelc packages)
27+
BOOL isDirectory;
28+
if ([fileManager fileExistsAtPath:pathString isDirectory:&isDirectory]) {
29+
if (!isDirectory && ![pathString hasSuffix:@".mlmodelc"]) {
30+
NSLog(@"Warning: ANE model path should typically be a .mlmodelc directory: %@", pathString);
31+
}
32+
}
33+
1834
NSURL *modelURL = [NSURL fileURLWithPath:pathString];
1935

2036
NSLog(@"Loading ANE model from: %@", modelURL.absoluteString);
@@ -32,6 +48,7 @@
3248
return nullptr;
3349
}
3450

51+
NSLog(@"Successfully loaded ANE model from: %@", pathString);
3552
return model;
3653
}
3754

tools/mtmd/mtmd.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cstdio>
1111
#include <cstdlib>
1212
#include <cstring>
13+
#include <fstream>
1314
#include <limits>
1415
#include <vector>
1516

@@ -159,7 +160,15 @@ struct mtmd_context {
159160

160161
// Set ANE model path for iOS
161162
if (ctx_params.ane_model_path && ctx_v) {
163+
// Check if ANE model file exists
164+
std::ifstream ane_file(ctx_params.ane_model_path);
165+
if (!ane_file.good()) {
166+
throw std::runtime_error(string_format("ANE model file does not exist: %s", ctx_params.ane_model_path));
167+
}
168+
ane_file.close();
169+
162170
clip_set_ane_model_path(ctx_v, ctx_params.ane_model_path);
171+
LOG_INF("ANE model path set to: %s\n", ctx_params.ane_model_path);
163172
}
164173
if (!ctx_v && !ctx_a) {
165174
throw std::runtime_error(string_format("Failed to load CLIP model from %s\n", mmproj_fname));

0 commit comments

Comments
 (0)