Home | History | Annotate | Line # | Download | only in gpt
main.c revision 1.11.2.1
      1  1.11.2.1  christos /*	$NetBSD: main.c,v 1.11.2.1 2019/06/10 22:05:33 christos Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*-
      4       1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      5       1.1  christos  * All rights reserved.
      6       1.1  christos  *
      7       1.1  christos  * Redistribution and use in source and binary forms, with or without
      8       1.1  christos  * modification, are permitted provided that the following conditions
      9       1.1  christos  * are met:
     10       1.1  christos  *
     11       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  christos  *    documentation and/or other materials provided with the distribution.
     16       1.1  christos  *
     17       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22       1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23       1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24       1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25       1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26       1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1  christos  *
     28       1.1  christos  * CRC32 code derived from work by Gary S. Brown.
     29       1.1  christos  */
     30       1.1  christos 
     31       1.1  christos #if HAVE_NBTOOL_CONFIG_H
     32       1.1  christos #include "nbtool_config.h"
     33       1.1  christos #endif
     34       1.1  christos 
     35       1.1  christos #include <sys/cdefs.h>
     36       1.1  christos #ifdef __RCSID
     37  1.11.2.1  christos __RCSID("$NetBSD: main.c,v 1.11.2.1 2019/06/10 22:05:33 christos Exp $");
     38       1.1  christos #endif
     39       1.1  christos 
     40       1.1  christos #include <stdio.h>
     41       1.1  christos #include <stdlib.h>
     42       1.1  christos #include <unistd.h>
     43       1.1  christos #include <err.h>
     44       1.9  christos #include <errno.h>
     45       1.9  christos #include <sys/stat.h>
     46       1.9  christos #ifndef NBTOOL_CONFIG_H
     47       1.9  christos #include <util.h>
     48       1.9  christos #endif
     49       1.1  christos 
     50       1.1  christos #include "map.h"
     51       1.1  christos #include "gpt.h"
     52       1.1  christos 
     53       1.3  christos static const struct gpt_cmd c_null;
     54       1.3  christos 
     55       1.3  christos extern const struct gpt_cmd
     56       1.3  christos 	c_add,
     57       1.1  christos #ifndef HAVE_NBTOOL_CONFIG_H
     58       1.3  christos 	c_backup,
     59       1.1  christos #endif
     60       1.3  christos 	c_biosboot,
     61       1.3  christos 	c_create,
     62       1.3  christos 	c_destroy,
     63       1.3  christos 	c_header,
     64       1.3  christos 	c_label,
     65       1.3  christos 	c_migrate,
     66       1.3  christos 	c_recover,
     67       1.3  christos 	c_remove,
     68       1.3  christos 	c_resize,
     69       1.3  christos 	c_resizedisk,
     70       1.3  christos #ifndef HAVE_NBTOOL_CONFIG_H
     71       1.3  christos 	c_restore,
     72       1.3  christos #endif
     73       1.3  christos 	c_set,
     74       1.3  christos 	c_show,
     75       1.3  christos 	c_type,
     76       1.3  christos 	c_unset;
     77       1.3  christos 
     78       1.3  christos static const struct gpt_cmd *cmdsw[] = {
     79       1.3  christos 	&c_add,
     80       1.3  christos #ifndef HAVE_NBTOOL_CONFIG_H
     81       1.3  christos 	&c_backup,
     82       1.3  christos #endif
     83       1.3  christos 	&c_biosboot,
     84       1.3  christos 	&c_create,
     85       1.3  christos 	&c_destroy,
     86       1.3  christos 	&c_header,
     87       1.3  christos 	&c_label,
     88       1.3  christos 	&c_migrate,
     89       1.3  christos 	&c_recover,
     90       1.3  christos 	&c_remove,
     91       1.3  christos 	&c_resize,
     92       1.3  christos 	&c_resizedisk,
     93       1.3  christos #ifndef HAVE_NBTOOL_CONFIG_H
     94       1.3  christos 	&c_restore,
     95       1.3  christos #endif
     96       1.3  christos 	&c_set,
     97       1.3  christos 	&c_show,
     98       1.3  christos 	&c_type,
     99       1.3  christos 	&c_unset,
    100       1.3  christos 	&c_null,
    101       1.1  christos };
    102       1.1  christos 
    103       1.1  christos __dead static void
    104       1.1  christos usage(void)
    105       1.1  christos {
    106       1.1  christos 	const char *p = getprogname();
    107       1.1  christos 	const char *f =
    108  1.11.2.1  christos 	    "[-nrqv] [-m mediasize] [-s sectorsize] [-T timestamp]";
    109       1.3  christos 	size_t i;
    110       1.1  christos 
    111       1.2  christos 	if (strcmp(p, "gpt") == 0)
    112       1.2  christos 		fprintf(stderr,
    113       1.5  christos 		    "Usage: %s %s command device\n", p, f);
    114       1.2  christos 	else
    115       1.2  christos 		fprintf(stderr,
    116       1.5  christos 		    "Usage: %s %s device command\n", p, f);
    117       1.3  christos 	fprintf(stderr, "Commands:\n");
    118       1.3  christos 	for (i = 0; i < __arraycount(cmdsw); i++)
    119       1.3  christos 		gpt_usage("\t", cmdsw[i]);
    120       1.3  christos 	exit(EXIT_FAILURE);
    121       1.1  christos }
    122       1.1  christos 
    123       1.1  christos static void
    124       1.1  christos prefix(const char *cmd)
    125       1.1  christos {
    126       1.1  christos 	char *pfx;
    127       1.1  christos 
    128       1.1  christos 	if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0)
    129       1.1  christos 		pfx = NULL;
    130       1.1  christos 	else
    131       1.1  christos 		setprogname(pfx);
    132       1.1  christos }
    133       1.1  christos 
    134       1.9  christos static time_t
    135       1.9  christos get_tstamp(const char *b)
    136       1.9  christos {
    137       1.9  christos 	struct stat st;
    138       1.9  christos 	char *eb;
    139       1.9  christos 	long long l;
    140       1.9  christos #ifndef HAVE_NBTOOL_CONFIG_H
    141       1.9  christos 	time_t when;
    142       1.9  christos #endif
    143       1.9  christos 
    144       1.9  christos 	if (stat(b, &st) != -1)
    145       1.9  christos 		return (time_t)st.st_mtime;
    146       1.9  christos 
    147       1.9  christos #ifndef HAVE_NBTOOL_CONFIG_H
    148       1.9  christos 	errno = 0;
    149       1.9  christos 	if ((when = parsedate(b, NULL, NULL)) != -1 || errno == 0)
    150       1.9  christos 		return when;
    151       1.9  christos #endif
    152       1.9  christos 	errno = 0;
    153       1.9  christos 	l = strtoll(b, &eb, 0);
    154       1.9  christos 	if (b == eb || *eb || errno)
    155       1.9  christos 		errx(EXIT_FAILURE, "Can't parse timestamp `%s'", b);
    156       1.9  christos 	return (time_t)l;
    157       1.9  christos }
    158       1.9  christos 
    159       1.1  christos int
    160       1.1  christos main(int argc, char *argv[])
    161       1.1  christos {
    162       1.2  christos 	char *cmd, *p, *dev = NULL;
    163       1.1  christos 	int ch, i;
    164       1.2  christos 	u_int secsz = 0;
    165       1.2  christos 	off_t mediasz = 0;
    166       1.2  christos 	int flags = 0;
    167       1.2  christos 	int verbose = 0;
    168       1.9  christos 	time_t timestamp = 0;
    169       1.2  christos 	gpt_t gpt;
    170       1.2  christos 
    171       1.2  christos 	setprogname(argv[0]);
    172       1.2  christos 
    173       1.2  christos 	if (strcmp(getprogname(), "gpt") == 0) {
    174       1.2  christos 		if (argc < 3)
    175       1.2  christos 			usage();
    176       1.2  christos 		dev = argv[--argc];
    177       1.2  christos 	}
    178       1.1  christos 
    179       1.8   aymeric #ifdef __GLIBC__
    180       1.8   aymeric #define GETOPT_BE_POSIX		"+"
    181       1.8   aymeric #else
    182       1.8   aymeric #define GETOPT_BE_POSIX		""
    183       1.8   aymeric #endif
    184       1.8   aymeric 
    185       1.1  christos 	/* Get the generic options */
    186       1.9  christos 	while ((ch = getopt(argc, argv, GETOPT_BE_POSIX "m:nqrs:T:v")) != -1) {
    187       1.1  christos 		switch(ch) {
    188       1.1  christos 		case 'm':
    189       1.1  christos 			if (mediasz > 0)
    190       1.1  christos 				usage();
    191       1.6  christos 			mediasz = strtol(optarg, &p, 10);
    192       1.1  christos 			if (*p != 0 || mediasz < 1)
    193       1.1  christos 				usage();
    194       1.1  christos 			break;
    195       1.1  christos 		case 'n':
    196       1.2  christos 			flags |= GPT_NOSYNC;
    197       1.1  christos 			break;
    198       1.1  christos 		case 'r':
    199       1.2  christos 			flags |= GPT_READONLY;
    200       1.1  christos 			break;
    201       1.1  christos 		case 'q':
    202       1.2  christos 			flags |= GPT_QUIET;
    203       1.1  christos 			break;
    204       1.1  christos 		case 's':
    205       1.7  christos 			if (gpt_uint_get(NULL, &secsz) == -1)
    206       1.1  christos 				usage();
    207       1.1  christos 			break;
    208       1.9  christos 		case 'T':
    209      1.10  christos 			flags |= GPT_TIMESTAMP;
    210       1.9  christos 			timestamp = get_tstamp(optarg);
    211       1.9  christos 			break;
    212       1.1  christos 		case 'v':
    213       1.1  christos 			verbose++;
    214       1.1  christos 			break;
    215       1.1  christos 		default:
    216       1.1  christos 			usage();
    217       1.1  christos 		}
    218       1.1  christos 	}
    219       1.2  christos 
    220       1.2  christos 	if (argc == optind)
    221       1.2  christos 		usage();
    222       1.2  christos 
    223       1.2  christos 	if (dev == NULL)
    224       1.2  christos 		dev = argv[optind++];
    225       1.1  christos 
    226       1.1  christos 	if (argc == optind)
    227       1.1  christos 		usage();
    228       1.1  christos 
    229       1.1  christos 	cmd = argv[optind++];
    230       1.3  christos 	for (i = 0; cmdsw[i]->name != NULL && strcmp(cmd, cmdsw[i]->name); i++)
    231       1.2  christos 		continue;
    232       1.1  christos 
    233       1.3  christos 	if (cmdsw[i]->fptr == NULL)
    234       1.2  christos 		errx(EXIT_FAILURE, "Unknown command: %s", cmd);
    235       1.1  christos 
    236       1.1  christos 	prefix(cmd);
    237       1.2  christos 
    238       1.7  christos 	if (*dev != '-') {
    239       1.7  christos 		gpt = gpt_open(dev, flags | cmdsw[i]->flags,
    240       1.9  christos 		    verbose, mediasz, secsz, timestamp);
    241       1.7  christos 		if (gpt == NULL)
    242       1.7  christos 			return EXIT_FAILURE;
    243       1.7  christos 	} else {
    244      1.11   mlelstv 		if ((cmdsw[i]->flags & GPT_OPTDEV) == 0)
    245      1.11   mlelstv 			errx(EXIT_FAILURE,
    246      1.11   mlelstv 			     "Command %s needs a device parameter", cmd);
    247       1.7  christos 		argc++;
    248       1.7  christos 		gpt = NULL;
    249       1.7  christos 	}
    250       1.2  christos 
    251       1.3  christos 	if ((*cmdsw[i]->fptr)(gpt, argc, argv) == -1)
    252       1.2  christos 		return EXIT_FAILURE;
    253       1.2  christos 
    254       1.7  christos 	if (gpt)
    255       1.7  christos 		gpt_close(gpt);
    256       1.2  christos 	return EXIT_SUCCESS;
    257       1.1  christos }
    258