fix COPYING
[btrfs-progs-unstable/devel.git] / btrfsctl.c
blobae2f27b3a5265f82dc737c2361fb47e32dc90af5
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #ifndef __CHECKER__
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
22 #include "ioctl.h"
23 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include "kerncompat.h"
33 #ifdef __CHECKER__
34 #define BLKGETSIZE64 0
35 #define BTRFS_IOC_SNAP_CREATE 0
36 #define BTRFS_IOC_ADD_DISK 0
37 #define BTRFS_VOL_NAME_MAX 255
38 struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
39 static inline int ioctl(int fd, int define, void *arg) { return 0; }
40 #endif
42 void print_usage(void)
44 printf("usage: btrfsctl [ -s snapshot_name ] dir\n");
45 exit(1);
48 int main(int ac, char **av)
50 char *fname;
51 int fd;
52 int ret;
53 struct btrfs_ioctl_vol_args args;
54 char *name = NULL;
55 int i;
56 struct stat st;
57 DIR *dirstream;
58 unsigned long command = 0;
59 int len;
61 for (i = 1; i < ac - 1; i++) {
62 if (strcmp(av[i], "-s") == 0) {
63 if (i + 1 >= ac - 1) {
64 fprintf(stderr, "-s requires an arg");
65 print_usage();
67 name = av[i + 1];
68 len = strlen(name);
69 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
70 fprintf(stderr,
71 "snapshot name zero length or too long\n");
72 exit(1);
74 if (strchr(name, '/')) {
75 fprintf(stderr,
76 "error: / not allowed in names\n");
77 exit(1);
79 command = BTRFS_IOC_SNAP_CREATE;
82 if (command == 0) {
83 fprintf(stderr, "no valid commands given\n");
84 exit(1);
86 fname = av[ac - 1];
87 ret = stat(fname, &st);
88 if (ret < 0) {
89 perror("stat:");
90 exit(1);
92 if (S_ISDIR(st.st_mode)) {
93 dirstream = opendir(fname);
94 if (!dirstream) {
95 perror("opendir");
96 exit(1);
98 fd = dirfd(dirstream);
99 } else {
100 fd = open(fname, O_RDWR);
101 } if (fd < 0) {
102 perror("open");
103 exit(1);
105 strcpy(args.name, name);
106 ret = ioctl(fd, command, &args);
107 printf("ioctl returns %d\n", ret);
108 return 0;