-
Notifications
You must be signed in to change notification settings - Fork 0
/
extattr_get_fd.c
78 lines (62 loc) · 1.71 KB
/
extattr_get_fd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright 2022-2024 Gaël PORTAY
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#if defined __FreeBSD__ || defined __NetBSD__
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <dlfcn.h>
#if defined __NetBSD__
#include <sys/param.h>
#endif
#include <sys/types.h>
#include <sys/extattr.h>
#include "iamroot.h"
#ifndef EXTATTR_MAXNAMELEN
#define EXTATTR_MAXNAMELEN 255
#endif
static ssize_t (*sym)(int, int, const char *, void *, size_t);
hidden ssize_t next_extattr_get_fd(int fd, int attrnamespace,
const char *attrname, void *data,
size_t nbytes)
{
if (!sym)
sym = dlsym(RTLD_NEXT, "extattr_get_fd");
if (!sym)
return __dl_set_errno_and_perror(ENOSYS, -1);
return sym(fd, attrnamespace, attrname, data, nbytes);
}
ssize_t extattr_get_fd(int fd, int attrnamespace, const char *attrname,
void *data, size_t nbytes)
{
char extbuf[EXTATTR_MAXNAMELEN+1]; /* NULL-terminated */
const int oldattrnamespace = attrnamespace;
const char *oldattrname = attrname;
ssize_t siz, ret = -1;
char buf[PATH_MAX];
(void)oldattrnamespace;
(void)oldattrname;
siz = fpath(fd, buf, sizeof(buf));
if (siz == -1)
goto exit;
if (attrnamespace == EXTATTR_NAMESPACE_SYSTEM) {
int n;
n = _snprintf(extbuf, sizeof(extbuf), "%s%s",
IAMROOT_EXTATTR_PREFIX, attrname);
if (n == -1)
goto exit;
attrnamespace = EXTATTR_NAMESPACE_USER;
attrname = extbuf;
}
ret = next_extattr_get_fd(fd, attrnamespace, attrname, data, nbytes);
exit:
__debug("%s(fd: %i <-> '%s', attrnamespace: %i -> %i, attrname: '%s' -> '%s', ...) -> %zi\n",
__func__, fd, buf, oldattrnamespace, attrnamespace,
oldattrname, attrname, ret);
return ret;
}
#endif