aboutsummaryrefslogtreecommitdiffstats
path: root/guts/unlinkat.c
blob: 13fdc8220cd7db05ee14e4aba07159e1eae80a65 (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
/* 
 * Copyright (c) 2008-2010 Wind River Systems; see
 * guts/COPYRIGHT for information.
 *
 * static int
 * wrap_unlinkat(int dirfd, const char *path, int rflags) {
 *	int rc = -1;
 */
	pseudo_msg_t *old_file;
	int save_errno;
	struct stat64 buf;

#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	if (dirfd != AT_FDCWD) {
		errno = ENOSYS;
		return -1;
	}
	if (rflags) {
		/* the only supported flag is AT_REMOVEDIR.  We'd never call
		 * with that flag unless the real AT functions exist, so 
		 * something must have gone horribly wrong....
		 */
		pseudo_diag("wrap_unlinkat called with flags (0x%x), path '%s'\n",
			rflags, path ? path : "<nil>");
		errno = ENOSYS;
		return -1;
	}
#endif

#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	rc = real___lxstat64(_STAT_VER, path, &buf);
#else
	rc = real___fxstatat64(_STAT_VER, dirfd, path, &buf, AT_SYMLINK_NOFOLLOW);
#endif
	if (rc == -1) {
		return rc;
	}
	old_file = pseudo_client_op(OP_UNLINK, 0, -1, dirfd, path, &buf);
#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	rc = real_unlink(path);
#else
	rc = real_unlinkat(dirfd, path, rflags);
#endif
	if (rc == -1) {
		save_errno = errno;
		if (old_file && old_file->result == RESULT_SUCCEED) {
			pseudo_debug(1, "unlink failed, trying to relink...\n");
			buf.st_uid = old_file->uid;
			buf.st_gid = old_file->uid;
			buf.st_mode = old_file->mode;
			buf.st_dev = old_file->dev;
			buf.st_ino = old_file->ino;
			pseudo_client_op(OP_LINK, 0, -1, dirfd, path, &buf);
		} else {
			pseudo_debug(1, "unlink failed, but found no database entry, ignoring.\n");
		}
		errno = save_errno;
	}
/*	return rc;
 * }
 */