Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) 2021 Mihai Moldovan <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /* MP support header */
18 #include "MacportsLegacySupport.h"
19 #if __MP_LEGACY_SUPPORT_FSETATTRLIST__
20
21 #include <sys/attr.h>
22 #include <sys/fcntl.h>
23 #include <sys/param.h>
24 #include <sys/errno.h>
25
26 #include <fcntl.h>
27 #include <string.h>
28 #include <unistd.h>
29
30
31 #ifdef __LP64__
32 int fsetattrlist(int fd, void *a, void *buf, size_t size, unsigned int flags)
33 #else /* defined (__LP64__) */
34 int fsetattrlist(int fd, void *a, void *buf, size_t size, unsigned long flags)
35 #endif /* defined (__LP64__) */
36 {
37 int cont = 1,
38 ret = 0;
39
40 char fpath[MAXPATHLEN];
41 memset (fpath, 0, MAXPATHLEN);
42 if (-1 == fcntl(fd, F_GETPATH, fpath)) {
43 ret = EBADF;
44 cont = 0;
45 }
46
47 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1080
48 /*
49 * Older systems don't correctly check if no attributes are to be set, which usually
50 * means a buffer size of zero and return an error since they malloc a block of
51 * memory with size zero, leading to ENOMEM.
52 *
53 * Emulate the fix from 10.8 for those.
54 */
55 const struct attrlist *al = a;
56 if (al->commonattr == 0 &&
57 (al->volattr & ~ATTR_VOL_INFO) == 0 &&
58 al->dirattr == 0 &&
59 al->fileattr == 0 &&
60 al->forkattr == 0) {
61 cont = 0;
62
63 /*
64 * Explicitly let the potential error from above pass through, since that's what
65 * the original function seems to do as well.
66 */
67 }
68 #endif
69
70 if (cont) {
71 ret = setattrlist(fpath, a, buf, size, flags);
72 }
73
74 return ret;
75 }
76
77 #endif /* __MP_LEGACY_SUPPORT_FSETATTRLIST__ */