Add the program name as the first parameter in the command of the btrfs tool

Signed-off-by: Chris Mason <chris.mason@oracle.com>
This commit is contained in:
Goffredo Baroncelli 2010-03-11 21:08:22 +01:00 committed by Chris Mason
parent 6d2cf04247
commit 7ebebb7bb1
2 changed files with 91 additions and 31 deletions

47
btrfs.c
View file

@ -104,6 +104,9 @@ static struct Command commands[] = {
{ 0, 0 , 0 } { 0, 0 , 0 }
}; };
static char *get_prgname(char *programname) static char *get_prgname(char *programname)
{ {
char *np; char *np;
@ -207,6 +210,41 @@ static int check_ambiguity(struct Command *cmd, char **argv){
return 0; return 0;
} }
/*
* This function, compacts the program name and the command in the first
* element of the '*av' array
*/
static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
char **ret;
int i;
char *newname;
ret = (char **)malloc(sizeof(char*)*(*ac+1));
newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
if( !ret || !newname ){
free(ret);
free(newname);
return -1;
}
ret[0] = newname;
for(i=0; i < *ac ; i++ )
ret[i+1] = (*av)[i];
strcpy(newname, prgname);
strcat(newname, " ");
strcat(newname, cmd->verb);
(*ac)++;
*av = ret;
return 0;
}
/* /*
This function perform the following jobs: This function perform the following jobs:
@ -307,15 +345,20 @@ static int parse_args(int argc, char **argv,
matchcmd->verb, -matchcmd->nargs); matchcmd->verb, -matchcmd->nargs);
return -2; return -2;
} }
if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999 ){ if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n", fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
matchcmd->verb, matchcmd->nargs); matchcmd->verb, matchcmd->nargs);
return -2; return -2;
} }
if (prepare_args( nargs_, args_, prgname, matchcmd )){
fprintf(stderr, "ERROR: not enough memory\\n");
return -20;
}
return 1; return 1;
} }
int main(int ac, char **av ) int main(int ac, char **av )
{ {

View file

@ -142,7 +142,7 @@ static u64 parse_size(char *s)
return atoll(s) * mult; return atoll(s) * mult;
} }
int do_defrag(int ac, char **avp) int do_defrag(int ac, char **av)
{ {
int fd; int fd;
int compress = 0; int compress = 0;
@ -156,17 +156,6 @@ int do_defrag(int ac, char **avp)
int verbose = 0; int verbose = 0;
int fancy_ioctl = 0; int fancy_ioctl = 0;
struct btrfs_ioctl_defrag_range_args range; struct btrfs_ioctl_defrag_range_args range;
char **av;
/*
* getopt expects av[0] to be the program name and it seems
* to get confused when this isn't the case
*/
av = malloc((ac + 2) * sizeof(char *));
av[0] = "defrag";
av[ac + 1] = NULL;
memcpy(av + 1, avp, ac * sizeof(char *));
ac += 1;
optind = 1; optind = 1;
while(1) { while(1) {
@ -264,7 +253,7 @@ int do_subvol_list(int argc, char **argv)
int ret; int ret;
char *subvol; char *subvol;
subvol = argv[0]; subvol = argv[1];
ret = test_issubvolume(subvol); ret = test_issubvolume(subvol);
if (ret < 0) { if (ret < 0) {
@ -294,8 +283,8 @@ int do_clone(int argc, char **argv)
char *newname; char *newname;
char *dstdir; char *dstdir;
subvol = argv[0]; subvol = argv[1];
dst = argv[1]; dst = argv[2];
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
res = test_issubvolume(subvol); res = test_issubvolume(subvol);
@ -375,7 +364,7 @@ int do_delete_subvolume(int argc, char **argv)
int res, fd, len; int res, fd, len;
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
char *dname, *vname, *cpath; char *dname, *vname, *cpath;
char *path = argv[0]; char *path = argv[1];
res = test_issubvolume(path); res = test_issubvolume(path);
if(res<0){ if(res<0){
@ -436,7 +425,7 @@ int do_create_subvol(int argc, char **argv)
char *newname; char *newname;
char *dstdir; char *dstdir;
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
char *dst = argv[0]; char *dst = argv[1];
res = test_isdir(dst); res = test_isdir(dst);
if(res >= 0 ){ if(res >= 0 ){
@ -487,7 +476,7 @@ int do_create_subvol(int argc, char **argv)
int do_fssync(int argc, char **argv) int do_fssync(int argc, char **argv)
{ {
int fd, res; int fd, res;
char *path = argv[0]; char *path = argv[1];
fd = open_file_or_dir(path); fd = open_file_or_dir(path);
if (fd < 0) { if (fd < 0) {
@ -506,10 +495,10 @@ int do_fssync(int argc, char **argv)
return 0; return 0;
} }
int do_scan(int nargs, char **argv) int do_scan(int argc, char **argv)
{ {
int i, fd; int i, fd;
if(!nargs){ if(argc<=1){
int ret; int ret;
printf("Scanning for Btrfs filesystems\n"); printf("Scanning for Btrfs filesystems\n");
@ -527,7 +516,7 @@ int do_scan(int nargs, char **argv)
return 10; return 10;
} }
for( i = 0 ; i < nargs ; i++ ){ for( i = 1 ; i < argc ; i++ ){
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
int ret; int ret;
@ -558,7 +547,7 @@ int do_resize(int argc, char **argv)
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
int fd, res, len; int fd, res, len;
char *amount=argv[0], *path=argv[1]; char *amount=argv[1], *path=argv[2];
fd = open_file_or_dir(path); fd = open_file_or_dir(path);
if (fd < 0) { if (fd < 0) {
@ -646,7 +635,7 @@ int do_show_filesystem(int argc, char **argv)
struct list_head *all_uuids; struct list_head *all_uuids;
struct btrfs_fs_devices *fs_devices; struct btrfs_fs_devices *fs_devices;
struct list_head *cur_uuid; struct list_head *cur_uuid;
char *search = argv[0]; char *search = argv[1];
int ret; int ret;
ret = btrfs_scan_one_dir("/dev", 0); ret = btrfs_scan_one_dir("/dev", 0);
@ -680,7 +669,7 @@ int do_add_volume(int nargs, char **args)
return 12; return 12;
} }
for(i=0 ; i < (nargs-1) ; i++ ){ for(i=1 ; i < (nargs-1) ; i++ ){
struct btrfs_ioctl_vol_args ioctl_args; struct btrfs_ioctl_vol_args ioctl_args;
int devfd, res; int devfd, res;
u64 dev_block_count = 0; u64 dev_block_count = 0;
@ -737,8 +726,8 @@ int do_balance(int argc, char **argv)
{ {
int fdmnt, ret=0; int fdmnt, ret=0;
char *path = argv[0];
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
char *path = argv[1];
fdmnt = open_file_or_dir(path); fdmnt = open_file_or_dir(path);
if (fdmnt < 0) { if (fdmnt < 0) {
@ -768,7 +757,7 @@ int do_remove_volume(int nargs, char **args)
return 12; return 12;
} }
for(i=0 ; i < (nargs-1) ; i++ ){ for(i=1 ; i < (nargs-1) ; i++ ){
struct btrfs_ioctl_vol_args arg; struct btrfs_ioctl_vol_args arg;
int res; int res;
@ -786,3 +775,31 @@ int do_remove_volume(int nargs, char **args)
else else
return 0; return 0;
} }
int do_set_default_subvol(int nargs, char **argv)
{
int ret=0, fd;
u64 objectid;
char *path = argv[2];
char *subvolid = argv[1];
fd = open_file_or_dir(path);
if (fd < 0) {
fprintf(stderr, "ERROR: can't access to '%s'\n", path);
return 12;
}
objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
if (errno == ERANGE) {
fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
return 30;
}
ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
close(fd);
if( ret < 0 ){
fprintf(stderr, "ERROR: unable to set a new default subvolume\n");
return 30;
}
return 0;
}