Debian Codegraph @

android/app/jni/extern/macports-legacy-support/src/fsgetpath.c

raw
1 /*
2 * Copyright (c) 2019
3 * from an example posted in Apple Developer Support
4 * https://forums.developer.apple.com/thread/103162
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19
20 // MP support header
21 #include "MacportsLegacySupport.h"
22 #if __MP_LEGACY_SUPPORT_FSGETPATH__
23
24
25 #if 1
26 /* SYS_fsgetpath is only available on 10.6 and up */
27 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
28 /* implement using a syscall available macOS 10.6 to 10.12 */
29 /* this should be thoroughly vetted as a syscall, but is private API */
30 #include <unistd.h>
31 #include <sys/syscall.h>
32 #include <sys/types.h>
33 #include <sys/mount.h>
34 ssize_t fsgetpath(char * buf, size_t buflen, fsid_t * fsid, uint64_t obj_id) {
35 return (ssize_t)syscall(SYS_fsgetpath, buf, (size_t)buflen, fsid, (uint64_t)obj_id);
36 }
37 #endif
38 #endif
39
40 #if 0
41 /* implement with a compatability function that presently compiles on 10.6 and over */
42 /* this may be better (see linked post above) but it's hard to thoroughly test it. */
43 /* this may also be able to be expanded to cover 10.4 and 10.5 if we can workaround ATTR_CMN_FULLPATH */
44 #include <stdio.h>
45 #include <errno.h>
46 #include <string.h>
47 #include <getopt.h>
48 #include <sys/attr.h>
49 #include <sys/mount.h>
50
51 ssize_t fsgetpath(char * buf, size_t buflen, fsid_t * fsid, uint64_t obj_id) {
52 char volfsPath[64]; // 8 for `/.vol//\0`, 10 for `fsid->val[0]`, 20 for `obj_id`, rounded up for paranoia
53
54 snprintf(volfsPath, sizeof(volfsPath), "/.vol/%ld/%llu", (long) fsid->val[0], (unsigned long long) obj_id);
55
56 struct {
57 uint32_t length;
58 attrreference_t pathRef;
59 char buffer[MAXPATHLEN];
60 } __attribute__((aligned(4), packed)) attrBuf;
61
62 struct attrlist attrList;
63 memset(&attrList, 0, sizeof(attrList));
64 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
65 attrList.commonattr = ATTR_CMN_FULLPATH;
66
67 int success = getattrlist(volfsPath, &attrList, &attrBuf, sizeof(attrBuf), 0) == 0;
68 if ( ! success ) {
69 return -1;
70 }
71 if (attrBuf.pathRef.attr_length > buflen) {
72 errno = ENOSPC;
73 return -1;
74 }
75 strlcpy(buf, ((const char *) &attrBuf.pathRef) + attrBuf.pathRef.attr_dataoffset, buflen);
76 return attrBuf.pathRef.attr_length;
77 }
78 #endif
79
80 #endif /* __MP_LEGACY_SUPPORT_FSGETPATH__ */