diff options
Diffstat (limited to 'udflabel')
-rw-r--r-- | udflabel/Makefile.am | 2 | ||||
-rw-r--r-- | udflabel/Makefile.in | 8 | ||||
-rw-r--r-- | udflabel/main.c | 32 | ||||
-rw-r--r-- | udflabel/options.c | 55 |
4 files changed, 39 insertions, 58 deletions
diff --git a/udflabel/Makefile.am b/udflabel/Makefile.am index 4f5a488..99de9d7 100644 --- a/udflabel/Makefile.am +++ b/udflabel/Makefile.am @@ -1,4 +1,4 @@ sbin_PROGRAMS = udflabel udflabel_LDADD = $(top_builddir)/libudffs/libudffs.la -udflabel_SOURCES = main.c options.c ../udfinfo/readdisc.c options.h ../udfinfo/readdisc.h ../include/ecma_167.h ../include/osta_udf.h ../include/libudffs.h ../include/udf_endian.h ../include/bswap.h +udflabel_SOURCES = main.c options.c ../udfinfo/readdisc.c options.h ../udfinfo/readdisc.h ../include/ecma_167.h ../include/osta_udf.h ../include/libudffs.h ../include/bswap.h AM_CPPFLAGS = -I$(top_srcdir)/include diff --git a/udflabel/Makefile.in b/udflabel/Makefile.in index 0dba0ab..14abcb8 100644 --- a/udflabel/Makefile.in +++ b/udflabel/Makefile.in @@ -228,11 +228,17 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PKG_CONFIG = @PKG_CONFIG@ +PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ +PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ RANLIB = @RANLIB@ SED = @SED@ SET_MAKE = @SET_MAKE@ SHELL = @SHELL@ STRIP = @STRIP@ +UDEVDIR = @UDEVDIR@ +UDEV_CFLAGS = @UDEV_CFLAGS@ +UDEV_LIBS = @UDEV_LIBS@ VERSION = @VERSION@ abs_builddir = @abs_builddir@ abs_srcdir = @abs_srcdir@ @@ -288,7 +294,7 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ udflabel_LDADD = $(top_builddir)/libudffs/libudffs.la -udflabel_SOURCES = main.c options.c ../udfinfo/readdisc.c options.h ../udfinfo/readdisc.h ../include/ecma_167.h ../include/osta_udf.h ../include/libudffs.h ../include/udf_endian.h ../include/bswap.h +udflabel_SOURCES = main.c options.c ../udfinfo/readdisc.c options.h ../udfinfo/readdisc.h ../include/ecma_167.h ../include/osta_udf.h ../include/libudffs.h ../include/bswap.h AM_CPPFLAGS = -I$(top_srcdir)/include all: all-am diff --git a/udflabel/main.c b/udflabel/main.c index 5c8c48d..28ff242 100644 --- a/udflabel/main.c +++ b/udflabel/main.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 Pali Rohár <pali.rohar@gmail.com> + * Copyright (C) 2017-2018 Pali Rohár <pali.rohar@gmail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -85,7 +85,7 @@ static int get_sector_size(int fd) static uint16_t compute_crc(void *desc, size_t length) { - return cpu_to_le16(udf_crc((uint8_t *)desc + sizeof(tag), length - sizeof(tag), 0)); + return udf_crc((uint8_t *)desc + sizeof(tag), length - sizeof(tag), 0); } static uint8_t compute_checksum(tag *tag) @@ -127,7 +127,7 @@ static void write_desc(int fd, struct udf_disc *disc, enum udf_space_type type, struct udf_extent *ext; struct udf_desc *desc; off_t off; - size_t offset; + off_t offset; ssize_t ret; ext = disc->head; @@ -141,9 +141,9 @@ static void write_desc(int fd, struct udf_disc *disc, enum udf_space_type type, printf(" ... at block %lu\n", (unsigned long int)(ext->start + desc->offset)); - offset = (size_t)disc->blocksize * (ext->start + desc->offset); + offset = (off_t)disc->blocksize * (ext->start + desc->offset); off = lseek(fd, offset, SEEK_SET); - if (off != (off_t)-1 && (size_t)off != offset) + if (off != (off_t)-1 && off != offset) { errno = EIO; off = (off_t)-1; @@ -213,8 +213,15 @@ int main(int argc, char *argv[]) appname = "udflabel"; memset(&disc, 0, sizeof(disc)); - disc.flags = FLAG_LOCALE; + disc.head = calloc(1, sizeof(struct udf_extent)); + if (!disc.head) + { + fprintf(stderr, "%s: Error: calloc failed: %s\n", appname, strerror(errno)); + exit(1); + } + + disc.flags = FLAG_LOCALE; disc.tail = disc.head; disc.head->space_type = USPACE; @@ -381,11 +388,6 @@ int main(int argc, char *argv[]) break; case PD_ACCESS_TYPE_WRITE_ONCE: - if (!force && disc.udf_rev < 0x0200) - { - fprintf(stderr, "%s: Error: Cannot update writeonce partition of UDF revision prior to 2.00\n", appname); - exit(1); - } if (!force && (new_fullvsid[0] != 0xFF || new_vid[0] != 0xFF)) { fprintf(stderr, "%s: Error: Cannot update --vid, --vsid, --uuid or --fullvid on writeonce partition\n", appname); @@ -535,7 +537,7 @@ int main(int argc, char *argv[]) { if (!disc.udf_fsd || !check_desc(disc.udf_fsd, sizeof(*disc.udf_fsd))) { - fprintf(stderr, "%s: Error: Main File Set Descriptor is damaged\n", appname); + fprintf(stderr, "%s: Error: File Set Descriptor is damaged\n", appname); exit(1); } } @@ -647,21 +649,21 @@ int main(int argc, char *argv[]) write_desc(fd, &disc, PSPACE, TAG_IDENT_FSD, disc.udf_fsd); } - if (update_pvd) + if (update_pvd && disc.udf_pvd[1] != disc.udf_pvd[0]) { printf("Updating Reserve Primary Volume Descriptor...\n"); update_desc(disc.udf_pvd[1], sizeof(*disc.udf_pvd[1])); write_desc(fd, &disc, RVDS, TAG_IDENT_PVD, disc.udf_pvd[1]); } - if (update_lvd) + if (update_lvd && disc.udf_lvd[1] != disc.udf_lvd[0]) { printf("Updating Reserve Logical Volume Descriptor...\n"); update_desc(disc.udf_lvd[1], sizeof(*disc.udf_lvd[1]) + le32_to_cpu(disc.udf_lvd[1]->mapTableLength)); write_desc(fd, &disc, RVDS, TAG_IDENT_LVD, disc.udf_lvd[1]); } - if (update_iuvd) + if (update_iuvd && disc.udf_iuvd[1] != disc.udf_iuvd[0]) { printf("Updating Reserve Implementation Use Volume Descriptor...\n"); update_desc(disc.udf_iuvd[1], sizeof(*disc.udf_iuvd[1])); diff --git a/udflabel/options.c b/udflabel/options.c index 347461f..8220785 100644 --- a/udflabel/options.c +++ b/udflabel/options.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 Pali Rohár <pali.rohar@gmail.com> + * Copyright (C) 2017-2018 Pali Rohár <pali.rohar@gmail.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,7 @@ #include <stdint.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <time.h> #include <fcntl.h> @@ -89,45 +90,20 @@ static void usage(void) exit(1); } -static unsigned long int strtoul_safe(const char *str, int base, int *failed) -{ - char *endptr = NULL; - unsigned long int ret; - errno = 0; - ret = strtoul(str, &endptr, base); - *failed = (!*str || *endptr || errno) ? 1 : 0; - return ret; -} - -static void get_random_bytes(void *buffer, size_t count) -{ - int fd; - size_t i; - - fd = open("/dev/urandom", O_RDONLY); - if (fd >= 0) - { - if (read(fd, buffer, count) == (ssize_t)count) - { - close(fd); - return; - } - close(fd); - } - - for (i = 0; i < count; ++i) - ((uint8_t *)buffer)[i] = rand() % 0xFF; -} - static void process_uuid_arg(const char *arg, char *new_uuid) { - unsigned long int rnd; int i; + time_t cur_time; + uint32_t uuid_time; if (strcmp(arg, "random") == 0) { - get_random_bytes(&rnd, sizeof(rnd)); - snprintf(new_uuid, 17, "%08lx%08lx", ((unsigned long int)time(NULL)) % 0xFFFFFFFF, rnd % 0xFFFFFFFF); + cur_time = time(NULL); + if (cur_time != (time_t)-1 && cur_time >= 0) + uuid_time = cur_time & 0xFFFFFFFF; + else + uuid_time = randu32(); + snprintf(new_uuid, 17, "%08lx%08lx", (unsigned long int)uuid_time, (unsigned long int)randu32()); return; } @@ -181,7 +157,6 @@ static void process_vid_lvid_arg(struct udf_disc *disc, int option, const char * void parse_args(int argc, char *argv[], struct udf_disc *disc, char **filename, int *force, dstring *new_lvid, dstring *new_vid, dstring *new_fsid, dstring *new_fullvsid, char *new_uuid, dstring *new_vsid) { - unsigned long int value; int failed; int ret; size_t len; @@ -196,22 +171,20 @@ void parse_args(int argc, char *argv[], struct udf_disc *disc, char **filename, break; case OPT_BLK_SIZE: case 'b': - value = strtoul_safe(optarg, 0, &failed); - if (failed || value < 512 || value > 32768 || (value & (value - 1))) + disc->blocksize = strtou32(optarg, 0, &failed); + if (failed || disc->blocksize < 512 || disc->blocksize > 32768 || (disc->blocksize & (disc->blocksize - 1))) { fprintf(stderr, "%s: Error: Invalid value for option --blocksize\n", appname); exit(1); } - disc->blocksize = value; break; case OPT_VAT_BLOCK: - value = strtoul_safe(optarg, 0, &failed); - if (failed || value > UINT32_MAX) + disc->vat_block = strtou32(optarg, 0, &failed); + if (failed) { fprintf(stderr, "%s: Error: Invalid value for option --vatblock\n", appname); exit(1); } - disc->vat_block = value; break; case OPT_FORCE: *force = 1; |