]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - tests/zfs-tests/cmd/mkbusy/mkbusy.c
1e4567488d98be997bbb343b710b3531c3252abe
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
;
101 int arglen
, dlen
, flen
;
105 * The argument supplied doesn't exist. Copy the path, and
106 * remove the trailing slash if presnt.
108 if ((arg
= strdup(argv
[0])) == NULL
)
110 arglen
= strlen(arg
);
111 if (arg
[arglen
- 1] == '/')
112 arg
[arglen
- 1] = '\0';
115 * Get the directory and file names, using the current directory
116 * if the provided path doesn't specify a directory at all.
118 if ((slash
= strrchr(arg
, '/')) == NULL
) {
124 fname
= strdup(slash
+ 1);
127 if (dname
== NULL
|| fname
== NULL
)
129 dlen
= strlen(dname
);
130 flen
= strlen(fname
);
132 /* The directory portion of the path must exist */
133 if ((ret
= stat(dname
, &sbuf
)) != 0 || !(sbuf
.st_mode
&
137 if ((fpath
= (char *)malloc(dlen
+ 1 + flen
+ 1)) == NULL
)
139 (void) memset(fpath
, '\0', dlen
+ 1 + flen
+ 1);
141 (void) strncpy(fpath
, dname
, dlen
);
143 (void) strncat(fpath
, fname
, flen
);
146 } else if ((sbuf
.st_mode
& S_IFMT
) == S_IFREG
||
147 (sbuf
.st_mode
& S_IFMT
) == S_IFLNK
||
148 (sbuf
.st_mode
& S_IFMT
) == S_IFCHR
||
149 (sbuf
.st_mode
& S_IFMT
) == S_IFBLK
) {
150 fpath
= strdup(argv
[0]);
151 } else if ((sbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
152 fpath
= strdup(argv
[0]);
161 if (isdir
== B_FALSE
) {
163 mode_t mode
= S_IRUSR
| S_IWUSR
;
165 flags
= rflag
== B_FALSE
? O_CREAT
| O_RDWR
: O_RDONLY
;
167 if ((fd
= open(fpath
, flags
, mode
)) < 0)
172 if ((dp
= opendir(fpath
)) == NULL
)
177 if (fflag
== B_FALSE
)