aboutsummaryrefslogtreecommitdiffstats
path: root/ports/darwin/guts/open.c
blob: afe19a24f3395576e4ea40e529c567c3b04224d3 (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
/*
 * Copyright (c) 2011-2013 Wind River Systems; see
 * guts/COPYRIGHT for information.
 *
 * int open(const char *path, int flags, ... { int mode })
 *	int rc = -1;
 */

	struct stat buf = { };
	int existed = 1;
	int save_errno;

	/* mask out mode bits appropriately */
	mode = mode & ~pseudo_umask;
#ifdef PSEUDO_FORCE_ASYNCH
        flags &= ~O_SYNC;
#endif

	/* if a creation has been requested, check whether file exists */
	if (flags & O_CREAT) {
		save_errno = errno;
		rc = real_stat(path, &buf);
		existed = (rc != -1);
		if (!existed)
			pseudo_debug(PDBGF_FILE, "open_creat: %s -> 0%o\n", path, mode);
		errno = save_errno;
	}

	/* because we are not actually root, secretly mask in 0600 to the
	 * underlying mode.  The ", 0" is because the only time mode matters
	 * is if a file is going to be created, in which case it's
	 * not a directory.
	 */
	rc = real_open(path, flags, PSEUDO_FS_MODE(mode, 0));
	save_errno = errno;

	if (rc != -1) {
		int stat_rc;
		stat_rc = real_stat(path, &buf);

		if (stat_rc != -1) {
			buf.st_mode = PSEUDO_DB_MODE(buf.st_mode, mode);
			if (!existed) {
				pseudo_client_op(OP_CREAT, 0, -1, -1, path, &buf);
			}
			pseudo_client_op(OP_OPEN, PSEUDO_ACCESS(flags), rc, -1, path, &buf);
		} else {
			pseudo_debug(PDBGF_CONSISTENCY, "open (fd %d, path %s, flags %d) succeeded, but stat failed (%s).\n",
				rc, path, flags, strerror(errno));
			pseudo_client_op(OP_OPEN, PSEUDO_ACCESS(flags), rc, -1, path, 0);
		}
		errno = save_errno;
	}

/*	return rc;
 * }
 */