xref: /aosp_15_r20/external/e2fsprogs/lib/e2p/fsetproject.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * fgetproject.c --- get project id
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1999  Theodore Ts'o <[email protected]>
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
8*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE_SOURCE
13*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE_SOURCE
14*6a54128fSAndroid Build Coastguard Worker #endif
15*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE64_SOURCE
16*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE64_SOURCE
17*6a54128fSAndroid Build Coastguard Worker #endif
18*6a54128fSAndroid Build Coastguard Worker 
19*6a54128fSAndroid Build Coastguard Worker #include "config.h"
20*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
21*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
22*6a54128fSAndroid Build Coastguard Worker #endif
23*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
24*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
25*6a54128fSAndroid Build Coastguard Worker #endif
26*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
27*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
28*6a54128fSAndroid Build Coastguard Worker #if HAVE_EXT2_IOCTLS
29*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
30*6a54128fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
31*6a54128fSAndroid Build Coastguard Worker #include "project.h"
32*6a54128fSAndroid Build Coastguard Worker #endif
33*6a54128fSAndroid Build Coastguard Worker 
34*6a54128fSAndroid Build Coastguard Worker #include "e2p.h"
35*6a54128fSAndroid Build Coastguard Worker 
36*6a54128fSAndroid Build Coastguard Worker #ifdef O_LARGEFILE
37*6a54128fSAndroid Build Coastguard Worker #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
38*6a54128fSAndroid Build Coastguard Worker #else
39*6a54128fSAndroid Build Coastguard Worker #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
40*6a54128fSAndroid Build Coastguard Worker #endif
41*6a54128fSAndroid Build Coastguard Worker 
fsetproject(const char * name,unsigned long project)42*6a54128fSAndroid Build Coastguard Worker int fsetproject(const char *name, unsigned long project)
43*6a54128fSAndroid Build Coastguard Worker {
44*6a54128fSAndroid Build Coastguard Worker #ifndef FS_IOC_FSGETXATTR
45*6a54128fSAndroid Build Coastguard Worker 	errno = EOPNOTSUPP;
46*6a54128fSAndroid Build Coastguard Worker 	return -1;
47*6a54128fSAndroid Build Coastguard Worker #else
48*6a54128fSAndroid Build Coastguard Worker 	int fd, r, save_errno = 0;
49*6a54128fSAndroid Build Coastguard Worker 	struct fsxattr fsx;
50*6a54128fSAndroid Build Coastguard Worker 
51*6a54128fSAndroid Build Coastguard Worker 	fd = open (name, OPEN_FLAGS);
52*6a54128fSAndroid Build Coastguard Worker 	if (fd == -1)
53*6a54128fSAndroid Build Coastguard Worker 		return -1;
54*6a54128fSAndroid Build Coastguard Worker 	r = ioctl (fd, FS_IOC_FSGETXATTR, &fsx);
55*6a54128fSAndroid Build Coastguard Worker 	if (r == -1) {
56*6a54128fSAndroid Build Coastguard Worker 		save_errno = errno;
57*6a54128fSAndroid Build Coastguard Worker 		goto errout;
58*6a54128fSAndroid Build Coastguard Worker 	}
59*6a54128fSAndroid Build Coastguard Worker 	fsx.fsx_projid = project;
60*6a54128fSAndroid Build Coastguard Worker 	r = ioctl (fd, FS_IOC_FSSETXATTR, &fsx);
61*6a54128fSAndroid Build Coastguard Worker 	if (r == -1)
62*6a54128fSAndroid Build Coastguard Worker 		save_errno = errno;
63*6a54128fSAndroid Build Coastguard Worker errout:
64*6a54128fSAndroid Build Coastguard Worker 	close (fd);
65*6a54128fSAndroid Build Coastguard Worker 	if (save_errno)
66*6a54128fSAndroid Build Coastguard Worker 		errno = save_errno;
67*6a54128fSAndroid Build Coastguard Worker 	return r;
68*6a54128fSAndroid Build Coastguard Worker #endif
69*6a54128fSAndroid Build Coastguard Worker }
70