xref: /haiku/src/system/libroot/posix/unistd/chown.c (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <errno.h>
8 #include <unistd.h>
9 
10 #include <NodeMonitor.h>
11 
12 #include <errno_private.h>
13 #include <syscalls.h>
14 #include <syscall_utils.h>
15 
16 
17 static int
18 common_chown(int fd, const char* path, bool followLinks, uid_t owner,
19 	gid_t group)
20 {
21 	struct stat stat;
22 	status_t status;
23 
24 	int mask = 0;
25 	if (owner != (uid_t)-1) {
26 		stat.st_uid = owner;
27 		mask |= B_STAT_UID;
28 	}
29 	if (group != (gid_t)-1) {
30 		stat.st_gid = group;
31 		mask |= B_STAT_GID;
32 	}
33 
34 	status = _kern_write_stat(fd, path, followLinks, &stat,
35 		sizeof(struct stat), mask);
36 
37 	RETURN_AND_SET_ERRNO(status);
38 }
39 
40 
41 int
42 chown(const char *path, uid_t owner, gid_t group)
43 {
44 	return common_chown(AT_FDCWD, path, true, owner, group);
45 }
46 
47 
48 int
49 lchown(const char *path, uid_t owner, gid_t group)
50 {
51 	return common_chown(AT_FDCWD, path, false, owner, group);
52 }
53 
54 
55 int
56 fchown(int fd, uid_t owner, gid_t group)
57 {
58 	return common_chown(fd, NULL, false, owner, group);
59 }
60 
61 
62 int
63 fchownat(int fd, const char* path, uid_t owner, gid_t group, int flag)
64 {
65 	return common_chown(fd, path, (flag & AT_SYMLINK_NOFOLLOW) == 0, owner,
66 		group);
67 }
68