Skip to content

Commit

Permalink
[cmp] Enable extraction of zip archives outside of ISO copy mode
Browse files Browse the repository at this point in the history
* This enables the use of Ctrl-SELECT to also extract files from a .zip
  when using non-bootable, DOS, UEFI-NTFS, etc.
* Also clean up some uprintf line terminations and some additional code.
* Also fix some Coverity and MinGW warnings.
  • Loading branch information
pbatard committed Mar 12, 2024
1 parent abc3312 commit 5eae8a6
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 77 deletions.
3 changes: 3 additions & 0 deletions res/loc/ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ o v4.?? (202?.??.??)
- *NEW* MSG_343 "Uncompressed VHD Image"
- *NEW* MSG_344 "Full Flash Update Image"
- *NEW* MSG_345 "Some additional data must be downloaded from Microsoft to use this functionality (...)"
- *NEW* MSG_346 "Restrict Windows to S-Mode (INCOMPATIBLE with online account bypass)"
- *NEW* MSG_347 "Expert Mode"
- *NEW* MSG_348 "Extracting archive files: %s"

o v3.22 (2023.??.??)
// MSG_144 is aimed the the ISO download feature
Expand Down
1 change: 1 addition & 0 deletions res/loc/rufus.loc
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ t MSG_345 "Some additional data must be downloaded from Microsoft to use this fu
"- Select 'No' to cancel the operation"
t MSG_346 "Restrict Windows to S-Mode (INCOMPATIBLE with online account bypass)"
t MSG_347 "Expert Mode"
t MSG_348 "Extracting archive files: %s"
# The following messages are for the Windows Store listing only and are not used by the application
t MSG_900 "Rufus is a utility that helps format and create bootable USB flash drives, such as USB keys/pendrives, memory sticks, etc."
t MSG_901 "Official site: %s"
Expand Down
43 changes: 26 additions & 17 deletions src/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ extern uint32_t dur_mins, dur_secs;
extern uint32_t wim_nb_files, wim_proc_files, wim_extra_files;
extern BOOL force_large_fat32, enable_ntfs_compression, lock_drive, zero_drive, fast_zeroing, enable_file_indexing;
extern BOOL write_as_image, use_vds, write_as_esp, is_vds_available, has_ffu_support;
extern char* archive_path;
uint8_t *grub2_buf = NULL, *sec_buf = NULL;
long grub2_len;

Expand Down Expand Up @@ -707,7 +708,7 @@ static BOOL CheckDisk(char DriveLetter)

pfChkdsk(wDriveRoot, wFSType, FALSE, FALSE, FALSE, FALSE, NULL, NULL, ChkdskCallback);
if (!IS_ERROR(FormatStatus)) {
uprintf("NTFS Fixup completed.\n");
uprintf("NTFS Fixup completed.");
r = TRUE;
}

Expand Down Expand Up @@ -797,25 +798,25 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
}

if (!read_sectors(hPhysicalDrive, SelectedDrive.SectorSize, 0, 1, buffer)) {
uprintf("Could not read MBR\n");
uprintf("Could not read MBR");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_READ_FAULT;
goto out;
}

switch (ComboBox_GetCurItemData(hFileSystem)) {
case FS_FAT16:
if (buffer[0x1c2] == 0x0e) {
uprintf("Partition is already FAT16 LBA...\n");
uprintf("Partition is already FAT16 LBA...");
} else if ((buffer[0x1c2] != 0x04) && (buffer[0x1c2] != 0x06)) {
uprintf("Warning: converting a non FAT16 partition to FAT16 LBA: FS type=0x%02x\n", buffer[0x1c2]);
uprintf("Warning: converting a non FAT16 partition to FAT16 LBA: FS type=0x%02x", buffer[0x1c2]);
}
buffer[0x1c2] = 0x0e;
break;
case FS_FAT32:
if (buffer[0x1c2] == 0x0c) {
uprintf("Partition is already FAT32 LBA...\n");
uprintf("Partition is already FAT32 LBA...");
} else if (buffer[0x1c2] != 0x0b) {
uprintf("Warning: converting a non FAT32 partition to FAT32 LBA: FS type=0x%02x\n", buffer[0x1c2]);
uprintf("Warning: converting a non FAT32 partition to FAT32 LBA: FS type=0x%02x", buffer[0x1c2]);
}
buffer[0x1c2] = 0x0c;
break;
Expand All @@ -824,11 +825,11 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
// Set first partition bootable - masquerade as per the DiskID selected
buffer[0x1be] = IsChecked(IDC_RUFUS_MBR) ?
(BYTE)ComboBox_GetCurItemData(hDiskID):0x80;
uprintf("Set bootable USB partition as 0x%02X\n", buffer[0x1be]);
uprintf("Set bootable USB partition as 0x%02X", buffer[0x1be]);
}

if (!write_sectors(hPhysicalDrive, SelectedDrive.SectorSize, 0, 1, buffer)) {
uprintf("Could not write MBR\n");
uprintf("Could not write MBR");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
goto out;
}
Expand Down Expand Up @@ -897,7 +898,7 @@ static BOOL WriteMBR(HANDLE hPhysicalDrive)
notify:
// Tell the system we've updated the disk properties
if (!DeviceIoControl(hPhysicalDrive, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &size, NULL))
uprintf("Failed to notify system about disk properties update: %s\n", WindowsErrorString());
uprintf("Failed to notify system about disk properties update: %s", WindowsErrorString());

out:
safe_mm_free(buffer);
Expand Down Expand Up @@ -1022,7 +1023,7 @@ BOOL WritePBR(HANDLE hLogicalVolume)
} else if (boot_type == BT_REACTOS) {
if (!write_fat_16_ros_br(fp, 0)) break;
} else if ((boot_type == BT_IMAGE) && HAS_KOLIBRIOS(img_report)) {
uprintf("FAT16 is not supported for KolibriOS\n"); break;
uprintf("FAT16 is not supported for KolibriOS"); break;
} else {
if (!write_fat_16_br(fp, 0)) break;
}
Expand All @@ -1034,11 +1035,11 @@ BOOL WritePBR(HANDLE hLogicalVolume)
uprintf(using_msg, bt_to_name(), "FAT32");
for (i = 0; i < 2; i++) {
if (!is_fat_32_fs(fp)) {
uprintf("New volume does not have a %s FAT32 boot sector - aborting\n", i?"secondary":"primary");
uprintf("New volume does not have a %s FAT32 boot sector - aborting", i?"secondary":"primary");
break;
}
uprintf("Confirmed new volume has a %s FAT32 boot sector\n", i?"secondary":"primary");
uprintf("Setting %s FAT32 boot sector for boot...\n", i?"secondary":"primary");
uprintf("Confirmed new volume has a %s FAT32 boot sector", i?"secondary":"primary");
uprintf("Setting %s FAT32 boot sector for boot...", i?"secondary":"primary");
if (boot_type == BT_FREEDOS) {
if (!write_fat_32_fd_br(fp, 0)) break;
} else if (boot_type == BT_REACTOS) {
Expand All @@ -1061,10 +1062,10 @@ BOOL WritePBR(HANDLE hLogicalVolume)
case FS_NTFS:
uprintf(using_msg, bt_to_name(), "NTFS");
if (!is_ntfs_fs(fp)) {
uprintf("New volume does not have an NTFS boot sector - aborting\n");
uprintf("New volume does not have an NTFS boot sector - aborting");
break;
}
uprintf("Confirmed new volume has an NTFS boot sector\n");
uprintf("Confirmed new volume has an NTFS boot sector");
if (!write_ntfs_br(fp)) break;
// Note: NTFS requires a full remount after writing the PBR. We dismount when we lock
// and also go through a forced remount, so that shouldn't be an issue.
Expand All @@ -1075,7 +1076,7 @@ BOOL WritePBR(HANDLE hLogicalVolume)
case FS_EXT4:
return TRUE;
default:
uprintf("Unsupported FS for FS BR processing - aborting\n");
uprintf("Unsupported FS for FS BR processing - aborting");
break;
}
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_WRITE_FAULT;
Expand Down Expand Up @@ -1813,7 +1814,7 @@ DWORD WINAPI FormatThread(void* param)
// we forcibly removed them, so add yet another explicit call to RemoveDriveLetters()
RemoveDriveLetters(DriveIndex, FALSE, TRUE);
if (!MountVolume(drive_name, volume_name)) {
uprintf("Could not remount %s as %c: %s\n", volume_name, toupper(drive_name[0]), WindowsErrorString());
uprintf("Could not remount %s as %c: %s", volume_name, toupper(drive_name[0]), WindowsErrorString());
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | APPERR(ERROR_CANT_MOUNT_VOLUME);
goto out;
}
Expand Down Expand Up @@ -1982,6 +1983,14 @@ DWORD WINAPI FormatThread(void* param)
}
}

// Copy any additonal files from an optional zip archive selected by the user
if (!IS_ERROR(FormatStatus)) {
UpdateProgress(OP_EXTRACT_ZIP, 0.0f);
drive_name[2] = 0;
if (fs_type < FS_EXT2 && !ExtractZip(archive_path, drive_name) && !IS_ERROR(FormatStatus))
uprintf("Warning: Could not copy additional files");
}

out:
if ((write_as_esp || write_as_ext) && volume_name != NULL)
AltUnmountVolume(volume_name, TRUE);
Expand Down
51 changes: 21 additions & 30 deletions src/iso.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ typedef struct {
RUFUS_IMG_REPORT img_report;
int64_t iso_blocking_status = -1;
extern BOOL preserve_timestamps, enable_ntfs_compression;
extern char* archive_path;
extern HANDLE format_thread;
BOOL enable_iso = TRUE, enable_joliet = TRUE, enable_rockridge = TRUE, has_ldlinux_c32;
#define ISO_BLOCKING(x) do {x; iso_blocking_status++; } while(0)
Expand Down Expand Up @@ -425,7 +424,7 @@ static void fix_config(const char* psz_fullpath, const char* psz_path, const cha
}
}
if (patched)
uprintf(" Patched %s: '%s' ➔ '%s'\n", src, iso_label, usb_label);
uprintf(" Patched %s: '%s' ➔ '%s'", src, iso_label, usb_label);
// Since version 8.2, and https://github.com/rhinstaller/anaconda/commit/a7661019546ec1d8b0935f9cb0f151015f2e1d95,
// Red Hat derivatives have changed their CD-ROM detection policy which leads to the installation source
// not being found. So we need to use 'inst.repo' instead of 'inst.stage2' in the kernel options.
Expand All @@ -442,7 +441,7 @@ static void fix_config(const char* psz_fullpath, const char* psz_path, const cha
}
}
if (patched)
uprintf(" Patched %s: '%s' ➔ '%s'\n", src, "inst.stage2", "inst.repo");
uprintf(" Patched %s: '%s' ➔ '%s'", src, "inst.stage2", "inst.repo");
}
}
safe_free(iso_label);
Expand All @@ -455,7 +454,7 @@ static void fix_config(const char* psz_fullpath, const char* psz_path, const cha
(!img_report.has_efi_syslinux) && (dst = safe_strdup(src)) ) {
dst[nul_pos-12] = 's'; dst[nul_pos-11] = 'y'; dst[nul_pos-10] = 's';
CopyFileA(src, dst, TRUE);
uprintf("Duplicated %s to %s\n", src, dst);
uprintf("Duplicated %s to %s", src, dst);
free(dst);
}

Expand All @@ -467,7 +466,7 @@ static void fix_config(const char* psz_fullpath, const char* psz_path, const cha
safe_sprintf(iso_label, MAX_PATH, "cd9660:/dev/iso9660/%s", img_report.label);
safe_sprintf(usb_label, MAX_PATH, "msdosfs:/dev/msdosfs/%s", img_report.usb_label);
if (replace_in_token_data(src, "set", iso_label, usb_label, TRUE) != NULL) {
uprintf(" Patched %s: '%s' ➔ '%s'\n", src, iso_label, usb_label);
uprintf(" Patched %s: '%s' ➔ '%s'", src, iso_label, usb_label);
modified = TRUE;
}
}
Expand Down Expand Up @@ -547,7 +546,7 @@ static void print_extracted_file(char* psz_fullpath, uint64_t file_length)
to_windows_path(psz_fullpath);
nul_pos = strlen(psz_fullpath);
safe_sprintf(&psz_fullpath[nul_pos], 24, " (%s)", SizeToHumanReadable(file_length, TRUE, FALSE));
uprintf("Extracting: %s\n", psz_fullpath);
uprintf("Extracting: %s", psz_fullpath);
safe_sprintf(&psz_fullpath[nul_pos], 24, " (%s)", SizeToHumanReadable(file_length, FALSE, FALSE));
PrintStatus(0, MSG_000, psz_fullpath); // MSG_000 is "%s"
// Remove the appended size for extraction
Expand All @@ -556,12 +555,6 @@ static void print_extracted_file(char* psz_fullpath, uint64_t file_length)
to_unix_path(psz_fullpath);
}

static void alt_print_extracted_file(const char* psz_fullpath, uint64_t file_length)
{
uprintf("Extracting: %s (%s)", psz_fullpath, SizeToHumanReadable(file_length, FALSE, FALSE));
PrintStatus(0, MSG_000, psz_fullpath);
}

// Convert from time_t to FILETIME
// Uses 3 static entries so that we can convert 3 concurrent values at the same time
static LPFILETIME __inline to_filetime(time_t t)
Expand Down Expand Up @@ -604,8 +597,10 @@ static int udf_extract_files(udf_t *p_udf, udf_dirent_t *p_udf_dirent, const cha
uint8_t* buf = malloc(ISO_BUFFER_SIZE);
int64_t read, file_length;

if ((p_udf_dirent == NULL) || (psz_path == NULL) || (buf == NULL))
if ((p_udf_dirent == NULL) || (psz_path == NULL) || (buf == NULL)) {
safe_free(buf);
return 1;
}

if (psz_path[0] == 0)
UpdateProgressWithInfoInit(NULL, TRUE);
Expand Down Expand Up @@ -746,8 +741,10 @@ static int iso_extract_files(iso9660_t* p_iso, const char *psz_path)
lsn_t lsn;
int64_t file_length;

if ((p_iso == NULL) || (psz_path == NULL) || (buf == NULL))
if ((p_iso == NULL) || (psz_path == NULL) || (buf == NULL)) {
safe_free(buf);
return 1;
}

length = _snprintf(psz_fullpath, sizeof(psz_fullpath), "%s%s/", psz_extract_dir, psz_path);
if (length < 0)
Expand Down Expand Up @@ -1090,10 +1087,10 @@ BOOL ExtractISO(const char* src_iso, const char* dest_dir, BOOL scan)
StrArrayCreate(&isolinux_path, 8);
PrintInfo(0, MSG_202);
} else {
uprintf("Extracting files...\n");
uprintf("Extracting files...");
IGNORE_RETVAL(_chdirU(app_data_dir));
if (total_blocks == 0) {
uprintf("Error: ISO has not been properly scanned.\n");
uprintf("Error: ISO has not been properly scanned.");
FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|APPERR(ERROR_ISO_SCAN);
goto out;
}
Expand Down Expand Up @@ -1223,7 +1220,7 @@ BOOL ExtractISO(const char* src_iso, const char* dest_dir, BOOL scan)
img_report.sl_version = sl_version;
sl_index = i;
} else if ((img_report.sl_version != sl_version) || (safe_strcmp(img_report.sl_version_ext, ext) != 0)) {
uprintf(" Found conflicting isolinux versions:\n '%s' (%d.%02d%s) vs '%s' (%d.%02d%s)",
uprintf(" Found conflicting isolinux versions:\r\n '%s' (%d.%02d%s) vs '%s' (%d.%02d%s)",
isolinux_path.String[sl_index], SL_MAJOR(img_report.sl_version), SL_MINOR(img_report.sl_version),
img_report.sl_version_ext, isolinux_path.String[i], SL_MAJOR(sl_version), SL_MINOR(sl_version), ext);
// Workaround for Antergos and other ISOs, that have multiple Syslinux versions.
Expand Down Expand Up @@ -1270,7 +1267,7 @@ BOOL ExtractISO(const char* src_iso, const char* dest_dir, BOOL scan)
if (tmp != NULL) {
for (i = 0; i < strlen(tmp); i++)
tmp[i] = (char)tolower(tmp[i]);
uprintf(" Checking txtsetup.sif:\n OsLoadOptions = %s", tmp);
uprintf(" Checking txtsetup.sif:\r\n OsLoadOptions = %s", tmp);
img_report.uses_minint = (strstr(tmp, "/minint") != NULL);
}
DeleteFileU(tmp_sif);
Expand Down Expand Up @@ -1401,12 +1398,6 @@ BOOL ExtractISO(const char* src_iso, const char* dest_dir, BOOL scan)
}
}
update_md5sum();
if (archive_path != NULL) {
uprintf("● Adding files from %s", archive_path);
bled_init(256 * KB, NULL, NULL, NULL, NULL, alt_print_extracted_file, NULL);
bled_uncompress_to_dir(archive_path, dest_dir, BLED_COMPRESSION_ZIP);
bled_exit();
}
}
iso9660_close(p_iso);
udf_close(p_udf);
Expand Down Expand Up @@ -1606,7 +1597,7 @@ int iso9660_readfat(intptr_t pp, void *buf, size_t secsize, libfat_sector_t sec)
if (iso9660_iso_seek_read(p_private->p_iso, p_private->buf,
p_private->lsn + (lsn_t)((p_private->sec_start * secsize) / ISO_BLOCKSIZE), ISO_NB_BLOCKS)
!= ISO_NB_BLOCKS * ISO_BLOCKSIZE) {
uprintf("Error reading ISO-9660 file %s at LSN %lu\n", img_report.efi_img_path,
uprintf("Error reading ISO-9660 file %s at LSN %lu", img_report.efi_img_path,
(long unsigned int)(p_private->lsn + (p_private->sec_start * secsize) / ISO_BLOCKSIZE));
return 0;
}
Expand Down Expand Up @@ -1640,7 +1631,7 @@ BOOL HasEfiImgBootLoaders(void)
}
p_statbuf = iso9660_ifs_stat_translate(p_iso, img_report.efi_img_path);
if (p_statbuf == NULL) {
uprintf("Could not get ISO-9660 file information for file %s\n", img_report.efi_img_path);
uprintf("Could not get ISO-9660 file information for file %s", img_report.efi_img_path);
goto out;
}
p_private = malloc(sizeof(iso9660_readfat_private));
Expand All @@ -1651,7 +1642,7 @@ BOOL HasEfiImgBootLoaders(void)
p_private->sec_start = 0;
// Populate our initial buffer
if (iso9660_iso_seek_read(p_private->p_iso, p_private->buf, p_private->lsn, ISO_NB_BLOCKS) != ISO_NB_BLOCKS * ISO_BLOCKSIZE) {
uprintf("Error reading ISO-9660 file %s at LSN %lu\n", img_report.efi_img_path, (long unsigned int)p_private->lsn);
uprintf("Error reading ISO-9660 file %s at LSN %lu", img_report.efi_img_path, (long unsigned int)p_private->lsn);
goto out;
}
lf_fs = libfat_open(iso9660_readfat, (intptr_t)p_private);
Expand Down Expand Up @@ -1728,7 +1719,7 @@ BOOL DumpFatDir(const char* path, int32_t cluster)
}
p_statbuf = iso9660_ifs_stat_translate(p_iso, img_report.efi_img_path);
if (p_statbuf == NULL) {
uprintf("Could not get ISO-9660 file information for file %s\n", img_report.efi_img_path);
uprintf("Could not get ISO-9660 file information for file %s", img_report.efi_img_path);
goto out;
}
p_private = malloc(sizeof(iso9660_readfat_private));
Expand All @@ -1739,7 +1730,7 @@ BOOL DumpFatDir(const char* path, int32_t cluster)
p_private->sec_start = 0;
// Populate our initial buffer
if (iso9660_iso_seek_read(p_private->p_iso, p_private->buf, p_private->lsn, ISO_NB_BLOCKS) != ISO_NB_BLOCKS * ISO_BLOCKSIZE) {
uprintf("Error reading ISO-9660 file %s at LSN %lu\n", img_report.efi_img_path, (long unsigned int)p_private->lsn);
uprintf("Error reading ISO-9660 file %s at LSN %lu", img_report.efi_img_path, (long unsigned int)p_private->lsn);
goto out;
}
lf_fs = libfat_open(iso9660_readfat, (intptr_t)p_private);
Expand All @@ -1764,7 +1755,7 @@ BOOL DumpFatDir(const char* path, int32_t cluster)
if (diritem.attributes & 0x10) {
// Directory => Create directory
if (!CreateDirectoryU(target, 0) && (GetLastError() != ERROR_ALREADY_EXISTS)) {
uprintf("Could not create directory '%s': %s\n", target, WindowsErrorString());
uprintf("Could not create directory '%s': %s", target, WindowsErrorString());
continue;
}
if (!DumpFatDir(target, dirpos.cluster))
Expand Down
7 changes: 7 additions & 0 deletions src/msapi_utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,13 @@ static __inline const char* _filenameU(const char* path)
return path;
}

static __inline uint64_t _filesizeU(const char* path)
{
struct __stat64 stat64 = { 0 };
_stat64U(path, &stat64);
return stat64.st_size;
}

// returned UTF-8 string must be freed
static __inline char* getenvU(const char* varname)
{
Expand Down
9 changes: 1 addition & 8 deletions src/rufus.c
Original file line number Diff line number Diff line change
Expand Up @@ -2512,17 +2512,10 @@ static INT_PTR CALLBACK MainCallback(HWND hDlg, UINT message, WPARAM wParam, LPA
// For now only zip archives are supported.
if (GetKeyState(VK_CONTROL) & 0x8000) {
EXT_DECL(arch_ext, NULL, __VA_GROUP__("*.zip"), __VA_GROUP__(lmprintf(MSG_309)));
if (image_path == NULL)
break;
archive_path = FileDialog(FALSE, NULL, &arch_ext, NULL);
if (archive_path != NULL) {
struct __stat64 stat64 = { 0 };
_stat64U(archive_path, &stat64);
img_report.projected_size -= img_report.archive_size;
img_report.archive_size = stat64.st_size;
img_report.projected_size += img_report.archive_size;
uprintf("Using archive: %s (%s)", _filenameU(archive_path),
SizeToHumanReadable(img_report.archive_size, FALSE, FALSE));
SizeToHumanReadable(_filesizeU(archive_path), FALSE, FALSE));
}
break;
}
Expand Down
Loading

0 comments on commit 5eae8a6

Please sign in to comment.