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
17static int
18common_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
41int
42chown(const char *path, uid_t owner, gid_t group)
43{
44	return common_chown(-1, path, true, owner, group);
45}
46
47
48int
49lchown(const char *path, uid_t owner, gid_t group)
50{
51	return common_chown(-1, path, false, owner, group);
52}
53
54
55int
56fchown(int fd, uid_t owner, gid_t group)
57{
58	return common_chown(fd, NULL, false, owner, group);
59}
60
61
62int
63fchownat(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