2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2012 by Delphix. All rights reserved.
17 * Make a directory busy. If the argument is an existing file or directory,
18 * simply open it directly and pause. If not, verify that the parent directory
19 * exists, and create a new file in that directory.
23 #include <sys/types.h>
36 (void) fprintf(stderr, "Usage: %s <dirname|filename>\n", progname);
41 fail(char *err, int rval)
52 if ((pid = fork()) < 0) {
54 } else if (pid != 0) {
55 (void) fprintf(stdout, "%ld\n", (long)pid);
66 main(int argc, char *argv[])
69 boolean_t isdir = B_FALSE;
70 boolean_t fflag = B_FALSE;
71 boolean_t rflag = B_FALSE;
76 while ((c = getopt(argc, argv, "fr")) != -1) {
78 /* Open the file or directory read only */
82 /* Run in the foreground */
97 if ((ret = stat(argv[0], &sbuf)) != 0) {
98 char *arg, *dname, *fname;
104 * The argument supplied doesn't exist. Copy the path, and
105 * remove the trailing slash if present.
107 if ((arg = strdup(argv[0])) == NULL)
109 arglen = strlen(arg);
110 if (arg[arglen - 1] == '/')
111 arg[arglen - 1] = '\0';
114 * Get the directory and file names, using the current directory
115 * if the provided path doesn't specify a directory at all.
117 if ((slash = strrchr(arg, '/')) == NULL) {
123 fname = strdup(slash + 1);
126 if (dname == NULL || fname == NULL)
129 /* The directory portion of the path must exist */
130 if ((ret = stat(dname, &sbuf)) != 0 || !(sbuf.st_mode &
134 rc = asprintf(&fpath, "%s/%s", dname, fname);
137 if (rc == -1 || fpath == NULL)
140 } else if ((sbuf.st_mode & S_IFMT) == S_IFREG ||
141 (sbuf.st_mode & S_IFMT) == S_IFLNK ||
142 (sbuf.st_mode & S_IFMT) == S_IFCHR ||
143 (sbuf.st_mode & S_IFMT) == S_IFBLK) {
144 fpath = strdup(argv[0]);
145 } else if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {
146 fpath = strdup(argv[0]);
155 if (isdir == B_FALSE) {
157 mode_t mode = S_IRUSR | S_IWUSR;
159 flags = rflag == B_FALSE ? O_CREAT | O_RDWR : O_RDONLY;
161 if ((fd = open(fpath, flags, mode)) < 0)
166 if ((dp = opendir(fpath)) == NULL)
171 if (fflag == B_FALSE)