fs_mgr_main.c revision 7574c035b2dcf8316c562c648113be6be65cb061
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
23char *me = "";
24
25static 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 */
35static 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
78int 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;
84    char *n_blk_dev;
85    char *fstab;
86
87    klog_init();
88    klog_set_level(6);
89
90    parse_options(argc, argv, &a_flag, &u_flag, &n_flag, &n_name, &n_blk_dev);
91
92    /* The name of the fstab file is last, after the option */
93    fstab = argv[argc - 1];
94
95    if (a_flag) {
96        return fs_mgr_mount_all(fstab);
97    } else if (n_flag) {
98        return fs_mgr_do_mount(fstab, n_name, n_blk_dev, 0);
99    } else if (u_flag) {
100        return fs_mgr_unmount_all(fstab);
101    } else {
102        ERROR("%s: Internal error, unknown option\n", me);
103        exit(1);
104    }
105
106    /* Should not get here */
107    exit(1);
108}
109
110