aboutsummaryrefslogtreecommitdiffstats
path: root/ports/unix/guts/fchownat.c
blob: 8d3ee0562d3d0c2db7d06fe71ecefc8fe0de069d (plain)
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
/* 
 * Copyright (c) 2008-2010, 2012 Wind River Systems; see
 * guts/COPYRIGHT for information.
 *
 * static int
 * wrap_fchownat(int dirfd, const char *path, uid_t owner, gid_t group, int flags) {
 *	int rc = -1;
 */
 	pseudo_msg_t *msg;
	PSEUDO_STATBUF buf;
	int save_errno;
	int doing_link = 0;

#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	if (dirfd != AT_FDCWD) {
		errno = ENOSYS;
		return -1;
	}
	if (flags & AT_SYMLINK_NOFOLLOW) {
		rc = base_lstat(path, &buf);
	} else {
		rc = base_stat(path, &buf);
	}
#else
	rc = base_fstatat(dirfd, path, &buf, flags);
#endif
	if (rc == -1) {
		return rc;
	}
	/* pseudo won't track the ownership, here */
	if (S_ISLNK(buf.st_mode)) {
		doing_link = 1;
	}
	save_errno = errno;

	if (owner == (uid_t) -1 || group == (gid_t) -1) {
		msg = pseudo_client_op(OP_STAT, 0, -1, -1, path, &buf);
		/* copy in any existing values... */
		if (msg) {
			if (msg->result == RESULT_SUCCEED) {
				pseudo_stat_msg(&buf, msg);
			} else {
				pseudo_debug(2, "chownat to %d:%d on %d/%s, ino %llu, new file.\n",
					owner, group, dirfd, path,
					(unsigned long long) buf.st_ino);
			}
		}
	}
	/* now override with arguments */
	if (owner != (uid_t) -1) {
		buf.st_uid = owner;
	}
	if (group != (gid_t) -1) {
		buf.st_gid = group;
	}
	msg = pseudo_client_op(OP_CHOWN, 0, -1, dirfd, path, &buf);
	if (msg && msg->result != RESULT_SUCCEED) {
		errno = EPERM;
		rc = -1;
	} else {
		/* just pretend we worked */
		rc = 0;
	}

/*	return rc;
 * }
 */