btrfs-progs: build with -D_FORTIFY_SOURCE=2
[btrfs-progs-unstable.git] / btrfsctl.c
blob22f253a014239847cca213b25417aa02cd852619
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 name ] [-d] [-r size] file_or_dir\n");
45 printf("\t-d filename defragments one file\n");
46 printf("\t-d directory defragments the entire Btree\n");
47 printf("\t-s snap_name existing_subvol creates a new snapshot\n");
48 printf("\t-s snap_name tree_root creates a new subvolume\n");
49 printf("\t-r [+-]size[gkm] resize the FS\n");
50 exit(1);
53 int main(int ac, char **av)
55 char *fname;
56 int fd;
57 int ret;
58 struct btrfs_ioctl_vol_args args;
59 char *name = NULL;
60 int i;
61 struct stat st;
62 DIR *dirstream;
63 unsigned long command = 0;
64 int len;
66 for (i = 1; i < ac - 1; i++) {
67 if (strcmp(av[i], "-s") == 0) {
68 if (i + 1 >= ac - 1) {
69 fprintf(stderr, "-s requires an arg");
70 print_usage();
72 name = av[i + 1];
73 len = strlen(name);
74 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
75 fprintf(stderr,
76 "snapshot name zero length or too long\n");
77 exit(1);
79 if (strchr(name, '/')) {
80 fprintf(stderr,
81 "error: / not allowed in names\n");
82 exit(1);
84 command = BTRFS_IOC_SNAP_CREATE;
85 } else if (strcmp(av[i], "-d") == 0) {
86 if (i >= ac - 1) {
87 fprintf(stderr, "-d requires an arg\n");
88 print_usage();
90 command = BTRFS_IOC_DEFRAG;
91 } else if (strcmp(av[i], "-r") == 0) {
92 if (i >= ac - 1) {
93 fprintf(stderr, "-r requires an arg\n");
94 print_usage();
96 name = av[i + 1];
97 len = strlen(name);
98 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
99 fprintf(stderr, "-r size too long\n");
100 exit(1);
102 command = BTRFS_IOC_RESIZE;
105 if (command == 0) {
106 fprintf(stderr, "no valid commands given\n");
107 exit(1);
109 fname = av[ac - 1];
110 ret = stat(fname, &st);
111 if (ret < 0) {
112 perror("stat:");
113 exit(1);
115 if (S_ISDIR(st.st_mode)) {
116 dirstream = opendir(fname);
117 if (!dirstream) {
118 perror("opendir");
119 exit(1);
121 fd = dirfd(dirstream);
122 } else {
123 fd = open(fname, O_RDWR);
124 } if (fd < 0) {
125 perror("open");
126 exit(1);
128 if (name)
129 strcpy(args.name, name);
130 else
131 args.name[0] = '\0';
132 ret = ioctl(fd, command, &args);
133 printf("ioctl returns %d\n", ret);
134 return 0;