Reputation: 3
Solution
There was no error, I didn't pay attention. Thanks to Rob for pointing this out
I have an iOS app that need to read files using a C library.
I only get permission denied error (13) no such file or directory when I try to access the file from the C function, and I can't find the cause. It doesn't happen when I create, read or write using Swift.
I tried several methods to create file URL inside the user data container, nothing works with C, it only works with Swift.
Even when the file is being created with Swift, being opened, read and verified before and after the C call, it only have errors in C.
// I tried different paths before and added the filename to these directories
// FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
let path: String = NSTemporaryDirectory().appending("test_file.txt")
FileManager.default.fileExists(atPath: path.path)
// true
XCTAssertNoThrow(try "test".write(to: URL(fileURLWithPath: path), atomically: true, encoding: .utf8))
XCTAssertNoThrow(try FileManager.default.setAttributes([FileAttributeKey.posixPermissions: 0o777], ofItemAtPath: path))
// call C
test_read_file(path)
FileManager.default.fileExists(atPath: path.path)
// true
// test_read_file.c
int test_read_file(const char* path) {
access(file_path, W_OK);
// returns 0
open(path, O_RDONLY);
// returns 13
// errno is 2 (ENOENT: No such file or directory)
return 0;
}
Correction
The errno
value is 2
ENOENT 2 No such file or directory
Upvotes: 0
Views: 351
Reputation: 299345
The 13 return value is a file handle, not an error code, and suggests your code is working exactly as you expect.
open
returns a non-negative value for success, and -1 for failure. To check the specific error, you check errno
.
Upvotes: 2