Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 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; |
Sasha Levitskiy | cdc1cfb | 2014-04-10 17:10:21 -0700 | [diff] [blame] | 83 | char *n_name=NULL; |
| 84 | char *n_blk_dev=NULL; |
| 85 | char *fstab_file=NULL; |
| 86 | struct fstab *fstab=NULL; |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 87 | |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 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 */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 93 | fstab_file = argv[argc - 1]; |
| 94 | |
| 95 | fstab = fs_mgr_read_fstab(fstab_file); |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 96 | |
| 97 | if (a_flag) { |
Wei Wang | 254f443 | 2016-08-23 11:58:09 -0700 | [diff] [blame^] | 98 | return fs_mgr_mount_all(fstab, MOUNT_MODE_DEFAULT); |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 99 | } else if (n_flag) { |
| 100 | return fs_mgr_do_mount(fstab, n_name, n_blk_dev, 0); |
| 101 | } else if (u_flag) { |
| 102 | return fs_mgr_unmount_all(fstab); |
| 103 | } else { |
| 104 | ERROR("%s: Internal error, unknown option\n", me); |
| 105 | exit(1); |
| 106 | } |
| 107 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 108 | fs_mgr_free_fstab(fstab); |
| 109 | |
Ken Sumrall | 7574c03 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 110 | /* Should not get here */ |
| 111 | exit(1); |
| 112 | } |