diff options
Diffstat (limited to 'mdk-stage1/tools.c')
-rw-r--r-- | mdk-stage1/tools.c | 305 |
1 files changed, 153 insertions, 152 deletions
diff --git a/mdk-stage1/tools.c b/mdk-stage1/tools.c index 5674c4f3b..14374661f 100644 --- a/mdk-stage1/tools.c +++ b/mdk-stage1/tools.c @@ -49,48 +49,48 @@ int image_has_stage2() { - return access(COMPRESSED_FILE_REL(IMAGE_LOCATION "/"), R_OK) == 0 || - access(IMAGE_LOCATION "/" LIVE_LOCATION_REL, R_OK) == 0; + return access(COMPRESSED_FILE_REL(IMAGE_LOCATION "/"), R_OK) == 0 || + access(IMAGE_LOCATION "/" LIVE_LOCATION_REL, R_OK) == 0; } enum return_type create_IMAGE_LOCATION(char *location_full) { - struct stat statbuf; - int offset = strncmp(location_full, IMAGE_LOCATION_DIR, sizeof(IMAGE_LOCATION_DIR) - 1) == 0 ? sizeof(IMAGE_LOCATION_DIR) - 1 : 0; - char *with_arch = asprintf_("%s/%s", location_full, ARCH); + struct stat statbuf; + int offset = strncmp(location_full, IMAGE_LOCATION_DIR, sizeof(IMAGE_LOCATION_DIR) - 1) == 0 ? sizeof(IMAGE_LOCATION_DIR) - 1 : 0; + char *with_arch = asprintf_("%s/%s", location_full, ARCH); - log_message("trying %s", with_arch); + log_message("trying %s", with_arch); - if (stat(with_arch, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) - location_full = with_arch; + if (stat(with_arch, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) + location_full = with_arch; - log_message("assuming %s is a mirror tree", location_full + offset); + log_message("assuming %s is a mirror tree", location_full + offset); - unlink(IMAGE_LOCATION); - if (symlink(location_full + offset, IMAGE_LOCATION) != 0) - return RETURN_ERROR; + unlink(IMAGE_LOCATION); + if (symlink(location_full + offset, IMAGE_LOCATION) != 0) + return RETURN_ERROR; - return RETURN_OK; + return RETURN_OK; } int ramdisk_possible(void) { - if (total_memory() > (IS_RESCUE ? MEM_LIMIT_RESCUE : MEM_LIMIT_DRAKX)) - return 1; - else { - log_message("warning, ramdisk is not possible due to low mem!"); - return 0; - } + if (total_memory() > (IS_RESCUE ? MEM_LIMIT_RESCUE : MEM_LIMIT_DRAKX)) + return 1; + else { + log_message("warning, ramdisk is not possible due to low mem!"); + return 0; + } } int compressed_image_preload(void) { - if (total_memory() > (IS_RESCUE ? MEM_LIMIT_RESCUE_PRELOAD : MEM_LIMIT_DRAKX_PRELOAD)) - return 1; - else { - log_message("warning, not preloading compressed due to low mem"); - return 0; - } + if (total_memory() > (IS_RESCUE ? MEM_LIMIT_RESCUE_PRELOAD : MEM_LIMIT_DRAKX_PRELOAD)) + return 1; + else { + log_message("warning, not preloading compressed due to low mem"); + return 0; + } } enum return_type save_fd(int from_fd, char * to, void (*callback_func)(int overall)) @@ -106,16 +106,16 @@ enum return_type save_fd(int from_fd, char * to, void (*callback_func)(int overa } do { - quantity = read(from_fd, buf, sizeof(buf)); - if (quantity > 0) { + quantity = read(from_fd, buf, sizeof(buf)); + if (quantity > 0) { if (fwrite(buf, 1, quantity, f_to) != quantity) { log_message("short write (%s)", strerror(errno)); goto cleanup; } } else if (quantity == -1) { - log_message("an error occured: %s", strerror(errno)); - goto cleanup; - } + log_message("an error occured: %s", strerror(errno)); + goto cleanup; + } if (callback_func) { overall += quantity; @@ -137,12 +137,12 @@ enum return_type copy_file(char * from, char * to, void (*callback_func)(int ove { int from_fd; - log_message("copy_file: %s -> %s", from, to); + log_message("copy_file: %s -> %s", from, to); - from_fd = open(from, O_RDONLY); - if (from_fd != -1) { - return save_fd(from_fd, to, callback_func); - } else { + from_fd = open(from, O_RDONLY); + if (from_fd != -1) { + return save_fd(from_fd, to, callback_func); + } else { log_perror(from); return RETURN_ERROR; } @@ -150,137 +150,138 @@ enum return_type copy_file(char * from, char * to, void (*callback_func)(int ove enum return_type recursiveRemove(char *file) { - struct stat sb; - - if (lstat(file, &sb) != 0) { - log_message("failed to stat %s: %d", file, errno); - return RETURN_ERROR; - } - - /* only descend into subdirectories if device is same as dir */ - if (S_ISDIR(sb.st_mode)) { - char * strBuf = alloca(strlen(file) + 1024); - DIR * dir; - struct dirent * d; - - if (!(dir = opendir(file))) { - log_message("error opening %s: %d", file, errno); - return RETURN_ERROR; - } - while ((d = readdir(dir))) { - if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) - continue; - - strcpy(strBuf, file); - strcat(strBuf, "/"); - strcat(strBuf, d->d_name); - - if (recursiveRemove(strBuf) != 0) { - closedir(dir); - return RETURN_ERROR; - } - } - closedir(dir); - - if (rmdir(file)) { - log_message("failed to rmdir %s: %d", file, errno); - return RETURN_ERROR; - } - } else { - if (unlink(file) != 0) { - log_message("failed to remove %s: %d", file, errno); - return RETURN_ERROR; - } - } - return RETURN_OK; + struct stat sb; + + if (lstat(file, &sb) != 0) { + log_message("failed to stat %s: %d", file, errno); + return RETURN_ERROR; + } + + /* only descend into subdirectories if device is same as dir */ + if (S_ISDIR(sb.st_mode)) { + char * strBuf = alloca(strlen(file) + 1024); + DIR * dir; + struct dirent * d; + + if (!(dir = opendir(file))) { + log_message("error opening %s: %d", file, errno); + return RETURN_ERROR; + } + while ((d = readdir(dir))) { + if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + continue; + + strcpy(strBuf, file); + strcat(strBuf, "/"); + strcat(strBuf, d->d_name); + + if (recursiveRemove(strBuf) != 0) { + closedir(dir); + return RETURN_ERROR; + } + } + closedir(dir); + + if (rmdir(file)) { + log_message("failed to rmdir %s: %d", file, errno); + return RETURN_ERROR; + } + } else { + if (unlink(file) != 0) { + log_message("failed to remove %s: %d", file, errno); + return RETURN_ERROR; + } + } + return RETURN_OK; } enum return_type recursiveRemove_if_it_exists(char *file) { - struct stat sb; + struct stat sb; - if (lstat(file, &sb) != 0) { - /* if file doesn't exist, simply return OK */ - return RETURN_OK; - } + if (lstat(file, &sb) != 0) { + /* if file doesn't exist, simply return OK */ + return RETURN_OK; + } - return recursiveRemove(file); + return recursiveRemove(file); } enum return_type mount_compressed_image(char *compressed_image, char *location_mount) { - if (lomount(compressed_image, location_mount, NULL, 1)) { + if (lomount(compressed_image, location_mount, NULL, 1)) { stg1_error_message("Could not mount compressed loopback :(."); return RETURN_ERROR; } - return RETURN_OK; + return RETURN_OK; } enum return_type preload_mount_compressed_fd(int compressed_fd, int image_size, char *image_name, char *location_mount) { - int ret; - char *compressed_tmpfs = asprintf_("/tmp/%s", image_name); - char *buf = "Loading program into memory..."; - init_progression(buf, image_size); - ret = save_fd(compressed_fd, compressed_tmpfs, update_progression); - end_progression(); - if (ret != RETURN_OK) - return ret; - - return mount_compressed_image(compressed_tmpfs, location_mount); + int ret; + char *compressed_tmpfs = asprintf_("/tmp/%s", image_name); + char *buf = "Loading program into memory..."; + init_progression(buf, image_size); + ret = save_fd(compressed_fd, compressed_tmpfs, update_progression); + end_progression(); + if (ret != RETURN_OK) + return ret; + + return mount_compressed_image(compressed_tmpfs, location_mount); } enum return_type mount_compressed_image_may_preload(char *image_name, char *location_mount, int preload) { - char *compressed_image = asprintf_("%s/%s", COMPRESSED_LOCATION, image_name); + char *compressed_image = asprintf_("%s/%s", COMPRESSED_LOCATION, image_name); - log_message("mount_compressed_may_preload: %s into %s (preload = %d)", compressed_image, location_mount, preload); + log_message("mount_compressed_may_preload: %s into %s (preload = %d)", compressed_image, location_mount, preload); if (access(compressed_image, R_OK) != 0) return RETURN_ERROR; if (preload) { - int compressed_fd = open(compressed_image, O_RDONLY); - if (compressed_fd != -1) { - return preload_mount_compressed_fd(compressed_fd, file_size(compressed_image), image_name, location_mount); - } else { - log_perror(compressed_image); - return RETURN_ERROR; - } - } else { - return mount_compressed_image(compressed_image, location_mount); - } + int compressed_fd = open(compressed_image, O_RDONLY); + if (compressed_fd != -1) { + return preload_mount_compressed_fd(compressed_fd, file_size(compressed_image), image_name, location_mount); + } else { + log_perror(compressed_image); + return RETURN_ERROR; + } + } else { + return mount_compressed_image(compressed_image, location_mount); + } } enum return_type may_load_compressed_image(void) { - if (!IS_RESCUE && access(IMAGE_LOCATION "/" LIVE_LOCATION_REL, R_OK) == 0) { - /* LIVE install */ - return RETURN_OK; - } else { - /* compressed install */ - return mount_compressed_image_may_preload(COMPRESSED_NAME(""), STAGE2_LOCATION, compressed_image_preload()); - } + if (!IS_RESCUE && access(IMAGE_LOCATION "/" LIVE_LOCATION_REL, R_OK) == 0) { + /* LIVE install */ + return RETURN_OK; + } else { + /* compressed install */ + return mount_compressed_image_may_preload(COMPRESSED_NAME(""), STAGE2_LOCATION, compressed_image_preload()); + } } enum return_type load_compressed_fd(int fd, int size) { - return preload_mount_compressed_fd(fd, size, COMPRESSED_NAME(""), STAGE2_LOCATION); + return preload_mount_compressed_fd(fd, size, COMPRESSED_NAME(""), STAGE2_LOCATION); } +// FIXME: guess right module from partition type (using libblkid?) int try_mount(char * dev, char * location) { - char device_fullname[50]; - snprintf(device_fullname, sizeof(device_fullname), "/dev/%s", dev); - - if (my_mount(device_fullname, location, "ext4", 0) == -1 && - my_mount(device_fullname, location, "btrfs", 0) == -1 && - my_mount(device_fullname, location, "vfat", 0) == -1 && - my_mount(device_fullname, location, "ntfs", 0) == -1 && - my_mount(device_fullname, location, "reiserfs", 0) == -1 && - my_mount(device_fullname, location, "reiser4", 0) == -1 && - my_mount(device_fullname, location, "jfs", 0) == -1 && - my_mount(device_fullname, location, "xfs", 0) == -1 && - my_mount(device_fullname, location, "iso9660", 0) == -1) { + char device_fullname[50]; + snprintf(device_fullname, sizeof(device_fullname), "/dev/%s", dev); + + if (my_mount(device_fullname, location, "ext4", 0) == -1 && + my_mount(device_fullname, location, "btrfs", 0) == -1 && + my_mount(device_fullname, location, "vfat", 0) == -1 && + my_mount(device_fullname, location, "ntfs", 0) == -1 && + my_mount(device_fullname, location, "reiserfs", 0) == -1 && + my_mount(device_fullname, location, "reiser4", 0) == -1 && + my_mount(device_fullname, location, "jfs", 0) == -1 && + my_mount(device_fullname, location, "xfs", 0) == -1 && + my_mount(device_fullname, location, "iso9660", 0) == -1) { return 1; } @@ -290,19 +291,19 @@ int try_mount(char * dev, char * location) #ifndef DISABLE_DISK int get_disks(char *** names, char *** models) { - char ** ptr; - int count = 0; + char ** ptr; + int count = 0; - my_insmod("ide_disk", ANY_DRIVER_TYPE, NULL, 0); - my_insmod("sd_mod", ANY_DRIVER_TYPE, NULL, 0); + my_insmod("ide_disk", ANY_DRIVER_TYPE, NULL, 0); + my_insmod("sd_mod", ANY_DRIVER_TYPE, NULL, 0); - get_medias(DISK, names, models, BUS_ANY); + get_medias(DISK, names, models, BUS_ANY); - ptr = *names; - while (ptr && *ptr) { - count++; - ptr++; - } + ptr = *names; + while (ptr && *ptr) { + count++; + ptr++; + } return count; } @@ -311,21 +312,21 @@ int get_disks(char *** names, char *** models) #ifndef DISABLE_CDROM int get_cdroms(char *** names, char *** models) { - char ** ptr; - int count = 0; + char ** ptr; + int count = 0; - my_insmod("ide_cd_mod", ANY_DRIVER_TYPE, NULL, 0); - my_insmod("sr_mod", ANY_DRIVER_TYPE, NULL, 0); + my_insmod("ide_cd_mod", ANY_DRIVER_TYPE, NULL, 0); + my_insmod("sr_mod", ANY_DRIVER_TYPE, NULL, 0); - get_medias(CDROM, names, models, BUS_ANY); + get_medias(CDROM, names, models, BUS_ANY); - ptr = *names; - while (ptr && *ptr) { - count++; - ptr++; - } + ptr = *names; + while (ptr && *ptr) { + count++; + ptr++; + } - return count; + return count; } #endif @@ -333,7 +334,7 @@ char * floppy_device(void) { char ** names, ** models; int fd; - my_insmod("floppy", ANY_DRIVER_TYPE, NULL, 0); + my_insmod("floppy", ANY_DRIVER_TYPE, NULL, 0); fd = open("/dev/fd0", O_RDONLY|O_NONBLOCK); if (fd != -1) { char drivtyp[17]; @@ -354,8 +355,8 @@ char * floppy_device(void) } log_message("seems that you don't have a regular floppy drive"); my_insmod("sd_mod", ANY_DRIVER_TYPE, NULL, 0); - get_medias(FLOPPY, &names, &models, BUS_ANY); - if (names && *names) + get_medias(FLOPPY, &names, &models, BUS_ANY); + if (names && *names) return asprintf_("/dev/%s", *names); else return NULL; |