blob: 4bde4a1da6434b435e8789dbb2eff8455f041642 [file] [log] [blame]
Ken Sumrall7574c032012-01-06 19:09:42 -08001/*
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;
Ken Sumrallab6b8522013-02-13 12:58:40 -080085 char *fstab_file;
86 struct fstab *fstab;
Ken Sumrall7574c032012-01-06 19:09:42 -080087
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 */
Ken Sumrallab6b8522013-02-13 12:58:40 -080094 fstab_file = argv[argc - 1];
95
96 fstab = fs_mgr_read_fstab(fstab_file);
Ken Sumrall7574c032012-01-06 19:09:42 -080097
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
Ken Sumrallab6b8522013-02-13 12:58:40 -0800109 fs_mgr_free_fstab(fstab);
110
Ken Sumrall7574c032012-01-06 19:09:42 -0800111 /* Should not get here */
112 exit(1);
113}
114