summaryrefslogtreecommitdiff
path: root/btrfsctl.c
blob: b8ba96264a29cf61bd61f766ef198feff60a3db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef __CHECKER__
#include <sys/ioctl.h>
#include <sys/mount.h>
#include "ioctl.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include "kerncompat.h"

#ifdef __CHECKER__
#define BLKGETSIZE64 0
#define BTRFS_IOC_SNAP_CREATE 0
#define BTRFS_IOC_ADD_DISK 0
#define BTRFS_VOL_NAME_MAX 255
struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
static inline int ioctl(int fd, int define, void *arg) { return 0; }
#endif

void print_usage(void)
{
	printf("usage: btrfsctl [ -s snapshot_name ] dir\n");
	exit(1);
}

int main(int ac, char **av)
{
	char *fname;
	int fd;
	int ret;
	struct btrfs_ioctl_vol_args args;
	char *name = NULL;
	int i;
	struct stat st;
	DIR *dirstream;
	unsigned long command = 0;

	for (i = 1; i < ac - 1; i++) {
		if (strcmp(av[i], "-s") == 0) {
			if (i + 1 >= ac - 1) {
				fprintf(stderr, "-s requires an arg");
				print_usage();
			}
			name = av[i + 1];
			if (strlen(name) >= BTRFS_VOL_NAME_MAX) {
				fprintf(stderr, "snapshot name is too long\n");
				exit(1);
			}
			command = BTRFS_IOC_SNAP_CREATE;
		}
	}
	if (command == 0) {
		fprintf(stderr, "no valid commands given\n");
		exit(1);
	}
	fname = av[ac - 1];
	ret = stat(fname, &st);
	if (ret < 0) {
		perror("stat:");
		exit(1);
	}
	if (S_ISDIR(st.st_mode)) {
		dirstream = opendir(fname);
		if (!dirstream) {
			perror("opendir");
			exit(1);
		}
		fd = dirfd(dirstream);
	} else {
		fd = open(fname, O_RDWR);
	} if (fd < 0) {
		perror("open");
		exit(1);
	}
	strcpy(args.name, name);
	ret = ioctl(fd, command, &args);
	printf("ioctl returns %d\n", ret);
	return 0;
}