检查 Objective C 中的资源分支
Check for resource fork in Objective C
如何检查文件是否存在资源叉?
Note : With out using the API's of CarbonCore->Resources.h, Since they
are deprecated.
无论是否存在资源分支,我都可以打开文件。但我需要检查资源叉是否存在。这正是我要找的。
使用 getattrlist 或 fgetattrlist 在 attrList 参数中传递 ATTR_FILE_RSRCLENGTH。看到 man page for getattrlist.
或者
BOOL hasResourceFork = getxattr("/path/To/file", "com.apple.ResourceFork", NULL, 0, 0, 0) != -1;
这需要
#include <sys/xattr.h>
对于那些想要获得实际资源分叉长度的人,这里是代码:
#include <sys/attr.h>
struct attrlist attrlist = {0};
attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
attrlist.fileattr = ATTR_FILE_RSRCLENGTH;
typedef struct __attribute__((__packed__)) {
uint32_t size;
off_t forkSize;
} result_t;
result_t result = {0};
if (getattrlist (filepath, &attrlist, &result, sizeof(result), 0) == 0) {
assert(sizeof(result)==result.size); // makes sure we got the struct right
// now we find the rsrc fork size in result.forkSize
}
如何检查文件是否存在资源叉?
Note : With out using the API's of CarbonCore->Resources.h, Since they are deprecated.
无论是否存在资源分支,我都可以打开文件。但我需要检查资源叉是否存在。这正是我要找的。
使用 getattrlist 或 fgetattrlist 在 attrList 参数中传递 ATTR_FILE_RSRCLENGTH。看到 man page for getattrlist.
或者
BOOL hasResourceFork = getxattr("/path/To/file", "com.apple.ResourceFork", NULL, 0, 0, 0) != -1;
这需要
#include <sys/xattr.h>
对于那些想要获得实际资源分叉长度的人,这里是代码:
#include <sys/attr.h>
struct attrlist attrlist = {0};
attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
attrlist.fileattr = ATTR_FILE_RSRCLENGTH;
typedef struct __attribute__((__packed__)) {
uint32_t size;
off_t forkSize;
} result_t;
result_t result = {0};
if (getattrlist (filepath, &attrlist, &result, sizeof(result), 0) == 0) {
assert(sizeof(result)==result.size); // makes sure we got the struct right
// now we find the rsrc fork size in result.forkSize
}