Home | History | Annotate | Download | only in fs_mgr
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdio.h>
     18 #include <string.h>
     19 #include <stdlib.h>
     20 #include <libgen.h>
     21 #include "fs_mgr_priv.h"
     22 
     23 char *me = "";
     24 
     25 static void usage(void)
     26 {
     27     ERROR("%s: usage: %s <-a | -n mnt_point blk_dev | -u> <fstab_file>\n", me, me);
     28     exit(1);
     29 }
     30 
     31 /* Parse the command line.  If an error is encountered, print an error message
     32  * and exit the program, do not return to the caller.
     33  * Return the number of argv[] entries consumed.
     34  */
     35 static void parse_options(int argc, char *argv[], int *a_flag, int *u_flag, int *n_flag,
     36                      char **n_name, char **n_blk_dev)
     37 {
     38     me = basename(strdup(argv[0]));
     39 
     40     if (argc <= 1) {
     41         usage();
     42     }
     43 
     44     if (!strcmp(argv[1], "-a")) {
     45         if (argc != 3) {
     46             usage();
     47         }
     48         *a_flag = 1;
     49     }
     50     if (!strcmp(argv[1], "-n")) {
     51         if (argc != 5) {
     52             usage();
     53         }
     54         *n_flag = 1;
     55         *n_name = argv[2];
     56         *n_blk_dev = argv[3];
     57     }
     58     if (!strcmp(argv[1], "-u")) {
     59         if (argc != 3) {
     60             usage();
     61         }
     62         *u_flag = 1;
     63     }
     64 
     65     /* If no flag is specified, it's an error */
     66     if (!(*a_flag | *n_flag | *u_flag)) {
     67         usage();
     68     }
     69 
     70     /* If more than one flag is specified, it's an error */
     71     if ((*a_flag + *n_flag + *u_flag) > 1) {
     72         usage();
     73     }
     74 
     75     return;
     76 }
     77 
     78 int main(int argc, char *argv[])
     79 {
     80     int a_flag=0;
     81     int u_flag=0;
     82     int n_flag=0;
     83     char *n_name=NULL;
     84     char *n_blk_dev=NULL;
     85     char *fstab_file=NULL;
     86     struct fstab *fstab=NULL;
     87 
     88     klog_init();
     89     klog_set_level(6);
     90 
     91     parse_options(argc, argv, &a_flag, &u_flag, &n_flag, &n_name, &n_blk_dev);
     92 
     93     /* The name of the fstab file is last, after the option */
     94     fstab_file = argv[argc - 1];
     95 
     96     fstab = fs_mgr_read_fstab(fstab_file);
     97 
     98     if (a_flag) {
     99         return fs_mgr_mount_all(fstab);
    100     } else if (n_flag) {
    101         return fs_mgr_do_mount(fstab, n_name, n_blk_dev, 0);
    102     } else if (u_flag) {
    103         return fs_mgr_unmount_all(fstab);
    104     } else {
    105         ERROR("%s: Internal error, unknown option\n", me);
    106         exit(1);
    107     }
    108 
    109     fs_mgr_free_fstab(fstab);
    110 
    111     /* Should not get here */
    112     exit(1);
    113 }
    114 
    115