no slashes in subvolume names
[btrfs-progs-unstable.git] / btrfsctl.c
blob196853e351b03e39fcfd2762ca36d5ebacb3d775
1 #ifndef __CHECKER__
2 #include <sys/ioctl.h>
3 #include <sys/mount.h>
4 #include "ioctl.h"
5 #endif
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <dirent.h>
13 #include "kerncompat.h"
15 #ifdef __CHECKER__
16 #define BLKGETSIZE64 0
17 #define BTRFS_IOC_SNAP_CREATE 0
18 #define BTRFS_IOC_ADD_DISK 0
19 #define BTRFS_VOL_NAME_MAX 255
20 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
21 static inline int ioctl(int fd, int define, void *arg) { return 0; }
22 #endif
24 void print_usage(void)
26 printf("usage: btrfsctl [ -s snapshot_name ] dir\n");
27 exit(1);
30 int main(int ac, char **av)
32 char *fname;
33 int fd;
34 int ret;
35 struct btrfs_ioctl_vol_args args;
36 char *name = NULL;
37 int i;
38 struct stat st;
39 DIR *dirstream;
40 unsigned long command = 0;
41 int len;
43 for (i = 1; i < ac - 1; i++) {
44 if (strcmp(av[i], "-s") == 0) {
45 if (i + 1 >= ac - 1) {
46 fprintf(stderr, "-s requires an arg");
47 print_usage();
49 name = av[i + 1];
50 len = strlen(name);
51 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
52 fprintf(stderr,
53 "snapshot name zero length or too long\n");
54 exit(1);
56 if (strchr(name, '/')) {
57 fprintf(stderr,
58 "error: / not allowed in names\n");
59 exit(1);
61 command = BTRFS_IOC_SNAP_CREATE;
64 if (command == 0) {
65 fprintf(stderr, "no valid commands given\n");
66 exit(1);
68 fname = av[ac - 1];
69 ret = stat(fname, &st);
70 if (ret < 0) {
71 perror("stat:");
72 exit(1);
74 if (S_ISDIR(st.st_mode)) {
75 dirstream = opendir(fname);
76 if (!dirstream) {
77 perror("opendir");
78 exit(1);
80 fd = dirfd(dirstream);
81 } else {
82 fd = open(fname, O_RDWR);
83 } if (fd < 0) {
84 perror("open");
85 exit(1);
87 strcpy(args.name, name);
88 ret = ioctl(fd, command, &args);
89 printf("ioctl returns %d\n", ret);
90 return 0;