OSDN Git Service

tests: try harder to clean up scsi_debug
[android-x86/external-parted.git] / partprobe / partprobe.c
1 /*
2     partprobe - informs the OS kernel of partition layout
3     Copyright (C) 2001-2002, 2007-2012 Free Software Foundation, Inc.
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* it's best to compile this with:
20  *
21  *       CFLAGS=-Os ./configure --disable-nls --disable-shared --disable-debug
22  *                  --enable-discover-only
23  *
24  * And strip(1) afterwards!
25  */
26
27 #include <config.h>
28
29 #include <parted/parted.h>
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <getopt.h>
34
35 #include "closeout.h"
36 #include "configmake.h"
37 #include "progname.h"
38 #include "version-etc.h"
39
40 #include <locale.h>
41 #include "gettext.h"
42 #if ! ENABLE_NLS
43 # undef textdomain
44 # define textdomain(Domainname) /* empty */
45 # undef bindtextdomain
46 # define bindtextdomain(Domainname, Dirname) /* empty */
47 #endif
48
49 #undef _
50 #define _(msgid) gettext (msgid)
51
52 #define AUTHORS \
53   "<http://git.debian.org/?p=parted/parted.git;a=blob_plain;f=AUTHORS>"
54
55 /* The official name of this program (e.g., no `g' prefix).  */
56 #define PROGRAM_NAME "partprobe"
57
58 static struct option const long_options[] =
59   {
60     /* Note: the --no-update option is deprecated, and deliberately
61      * not documented.  FIXME: remove --no-update in 2009. */
62     {"no-update", no_argument, NULL, 'd'},
63     {"dry-run", no_argument, NULL, 'd'},
64     {"summary", no_argument, NULL, 's'},
65     {"help", no_argument, NULL, 'h'},
66     {"version", no_argument, NULL, 'v'},
67     {NULL, 0, NULL, 0}
68   };
69
70 /* initialized to 0 according to the language lawyers */
71 static int      opt_no_inform;
72 static int      opt_summary;
73
74 static void
75 summary (PedDisk* disk)
76 {
77         PedPartition*   walk;
78
79         printf ("%s: %s partitions", disk->dev->path, disk->type->name);
80         for (walk = disk->part_list; walk; walk = walk->next) {
81                 if (!ped_partition_is_active (walk))
82                         continue;
83
84                 printf (" %d", walk->num);
85                 if (walk->type & PED_PARTITION_EXTENDED) {
86                         PedPartition*   log_walk;
87                         int             is_first = 1;
88
89                         printf (" <");
90                         for (log_walk = walk->part_list; log_walk;
91                              log_walk = log_walk->next) {
92                                 if (!ped_partition_is_active (log_walk))
93                                         continue;
94                                 if (!is_first)
95                                         printf (" ");
96                                 printf ("%d", log_walk->num);
97                                 is_first = 0;
98                         }
99                         printf (">");
100                 }
101         }
102         printf ("\n");
103 }
104
105 static int
106 process_dev (PedDevice* dev)
107 {
108         PedDiskType*    disk_type;
109         PedDisk*        disk;
110
111         disk_type = ped_disk_probe (dev);
112         if (!disk_type || !strcmp (disk_type->name, "loop"))
113                 return 1;
114
115         disk = ped_disk_new (dev);
116         if (!disk)
117                 goto error;
118         if (!opt_no_inform) {
119                 if (!ped_disk_commit_to_os (disk))
120                         goto error_destroy_disk;
121         }
122         if (opt_summary)
123                 summary (disk);
124         ped_disk_destroy (disk);
125         return 1;
126
127 error_destroy_disk:
128         ped_disk_destroy (disk);
129 error:
130         return 0;
131 }
132
133 static void
134 usage (int status)
135 {
136   if (status != EXIT_SUCCESS)
137     fprintf (stderr, _("Try `%s --help' for more information.\n"),
138              program_name);
139   else
140     {
141       printf (_("Usage: %s [OPTION] [DEVICE]...\n"), PROGRAM_NAME);
142       fputs (_("\
143 Inform the operating system about partition table changes.\n\
144 \n\
145   -d, --dry-run    do not actually inform the operating system\n\
146   -s, --summary    print a summary of contents\n\
147   -h, --help       display this help and exit\n\
148   -v, --version    output version information and exit\n\
149 "), stdout);
150       fputs (_("\
151 \n\
152 When no DEVICE is given, probe all partitions.\n\
153 "), stdout);
154       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
155     }
156   exit (status);
157 }
158
159 int
160 main (int argc, char* argv[])
161 {
162         int             status = 0;
163
164         set_program_name (argv[0]);
165
166         setlocale (LC_ALL, "");
167         bindtextdomain (PACKAGE, LOCALEDIR);
168         textdomain (PACKAGE);
169
170         atexit (close_stdout);
171
172         int c;
173         while ((c = getopt_long (argc, argv, "dhsv", long_options, NULL)) != -1)
174                 switch (c) {
175                         case 'd':
176                                 opt_no_inform = 1;
177                                 break;
178
179                         case 's':
180                                 opt_summary = 1;
181                                 break;
182
183                         case 'h':
184                                 usage (EXIT_SUCCESS);
185                                 break;
186
187                         case 'v':
188                                 version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME,
189                                              VERSION, AUTHORS, (char *) NULL);
190                                 exit (EXIT_SUCCESS);
191                                 break;
192
193                         default:
194                                 usage (EXIT_FAILURE);
195                 }
196
197         int n_dev = argc - optind;
198         if (n_dev != 0) {
199                 int i;
200                 for (i = optind; i < argc; i++) {
201                         PedDevice *dev = ped_device_get (argv[i]);
202                         if (dev == NULL || process_dev (dev) == 0)
203                                 status = 1;
204                 }
205         } else {
206                 ped_device_probe_all ();
207                 PedDevice *dev;
208                 for (dev = ped_device_get_next (NULL); dev;
209                      dev = ped_device_get_next (dev))
210                         if (process_dev (dev) == 0)
211                                 status = 1;
212         }
213
214         return status;
215 }