]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - tests/zfs-tests/cmd/mkbusy/mkbusy.c
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>
33 typedef enum boolean
{ B_FALSE
, B_TRUE
} boolean_t
;
38 (void) fprintf(stderr
, "Usage: %s <dirname|filename>\n", progname
);
43 fail(char *err
, int rval
)
54 if ((pid
= fork()) < 0) {
56 } else if (pid
!= 0) {
57 (void) fprintf(stdout
, "%ld\n", (long)pid
);
68 main(int argc
, char *argv
[])
71 boolean_t isdir
= B_FALSE
;
72 boolean_t fflag
= B_FALSE
;
73 boolean_t rflag
= B_FALSE
;
78 while ((c
= getopt(argc
, argv
, "fr")) != -1) {
80 /* Open the file or directory read only */
84 /* Run in the foreground */
99 if ((ret
= stat(argv
[0], &sbuf
)) != 0) {
100 char *arg
, *dname
, *fname
;
106 * The argument supplied doesn't exist. Copy the path, and
107 * remove the trailing slash if presnt.
109 if ((arg
= strdup(argv
[0])) == NULL
)
111 arglen
= strlen(arg
);
112 if (arg
[arglen
- 1] == '/')
113 arg
[arglen
- 1] = '\0';
116 * Get the directory and file names, using the current directory
117 * if the provided path doesn't specify a directory at all.
119 if ((slash
= strrchr(arg
, '/')) == NULL
) {
125 fname
= strdup(slash
+ 1);
128 if (dname
== NULL
|| fname
== NULL
)
131 /* The directory portion of the path must exist */
132 if ((ret
= stat(dname
, &sbuf
)) != 0 || !(sbuf
.st_mode
&
136 rc
= asprintf(&fpath
, "%s/%s", dname
, fname
);
139 if (rc
== -1 || fpath
== NULL
)
142 } else if ((sbuf
.st_mode
& S_IFMT
) == S_IFREG
||
143 (sbuf
.st_mode
& S_IFMT
) == S_IFLNK
||
144 (sbuf
.st_mode
& S_IFMT
) == S_IFCHR
||
145 (sbuf
.st_mode
& S_IFMT
) == S_IFBLK
) {
146 fpath
= strdup(argv
[0]);
147 } else if ((sbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
148 fpath
= strdup(argv
[0]);
157 if (isdir
== B_FALSE
) {
159 mode_t mode
= S_IRUSR
| S_IWUSR
;
161 flags
= rflag
== B_FALSE
? O_CREAT
| O_RDWR
: O_RDONLY
;
163 if ((fd
= open(fpath
, flags
, mode
)) < 0)
168 if ((dp
= opendir(fpath
)) == NULL
)
173 if (fflag
== B_FALSE
)