# include "imagex-win32.h"
# define tbasename win32_wbasename
# define tglob win32_wglob
+# define OS_PREFERRED_PATH_SEPARATOR L'\\'
+# define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
#else /* __WIN32__ */
# include <glob.h>
# include <getopt.h>
# include <langinfo.h>
# define tbasename basename
# define tglob glob
+# define OS_PREFERRED_PATH_SEPARATOR '/'
+# define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
#endif /* !__WIN32 */
return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
}
-/* Return 0 if a path names a file to which the current user has write access;
- * -1 otherwise (and print an error message). */
-static int
-file_writable(const tchar *path)
-{
- int ret;
- ret = taccess(path, W_OK);
- if (ret != 0)
- imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
- return ret;
-}
-
#define TO_PERCENT(numerator, denominator) \
(((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
tprintf(T("Scanning \"%"TS"\""), info->scan.source);
if (*info->scan.wim_target_path) {
- tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
+ tprintf(T(" (loading as WIM path: "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
info->scan.wim_target_path);
} else {
tprintf(T(" (loading as root of WIM image)...\n"));
unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
percent_done = TO_PERCENT(info->integrity.completed_bytes,
info->integrity.total_bytes);
- tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" "TS" "
- "of %"PRIu64" "TS" (%u%%) done"),
+ tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
+ "of %"PRIu64" %"TS" (%u%%) done"),
info->integrity.filename,
info->integrity.completed_bytes >> unit_shift,
unit_name,
info->extract.target);
break;
case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
- tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
+ tprintf(T("Extracting "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
"in \"%"TS"\" to \"%"TS"\"\n"),
info->extract.extract_root_wim_source_path,
info->extract.image,
info->split.cur_part_number);
}
break;
+ case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
+ switch (info->update.command->op) {
+ case WIMLIB_UPDATE_OP_DELETE:
+ tprintf(T("Deleted WIM path "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
+ info->update.command->delete.wim_path);
+ break;
+ case WIMLIB_UPDATE_OP_RENAME:
+ tprintf(T("Renamed WIM path "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
+ "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
+ info->update.command->rename.wim_source_path,
+ info->update.command->rename.wim_target_path);
+ break;
+ case WIMLIB_UPDATE_OP_ADD:
+ default:
+ break;
+ }
+ break;
default:
break;
}
imagex_capture_or_append(int argc, tchar **argv)
{
int c;
- int open_flags = 0;
+ int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
int write_flags = 0;
int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
bool capture_sources_malloced;
struct wimlib_capture_source *capture_sources;
size_t num_sources;
+ bool name_defaulted;
for_opt(c, capture_or_append_options) {
switch (c) {
if (argc >= 3) {
name = argv[2];
+ name_defaulted = false;
} else {
/* Set default name to SOURCE argument, omitting any directory
* prefixes and trailing slashes. This requires making a copy
- * of @source. */
+ * of @source. Leave some free characters at the end in case we
+ * append a number to keep the name unique. */
source_name_len = tstrlen(source);
- source_copy = alloca((source_name_len + 1) * sizeof(tchar));
+ source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
name = tbasename(tstrcpy(source_copy, source));
+ name_defaulted = true;
}
/* Image description defaults to NULL if not given. */
desc = (argc >= 4) ? argv[3] : NULL;
}
}
}
+
+ if (cmd == APPEND && name_defaulted) {
+ /* If the user did not specify an image name, and the basename
+ * of the source already exists as an image name in the WIM
+ * file, append a suffix to make it unique. */
+ unsigned long conflict_idx;
+ tchar *name_end = tstrchr(name, T('\0'));
+ for (conflict_idx = 1;
+ wimlib_image_name_in_use(w, name);
+ conflict_idx++)
+ {
+ tsprintf(name_end, T(" (%lu)"), conflict_idx);
+ }
+ }
#ifdef __WIN32__
win32_acquire_capture_privileges();
#endif
ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
num_threads, imagex_progress_func);
}
- if (ret == WIMLIB_ERR_REOPEN)
- ret = 0;
if (ret != 0)
imagex_error(T("Failed to write the WIM file \"%"TS"\""),
wimfile);
imagex_delete(int argc, tchar **argv)
{
int c;
- int open_flags = 0;
+ int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
int write_flags = 0;
const tchar *wimfile;
const tchar *image_num_or_name;
wimfile = argv[0];
image_num_or_name = argv[1];
- ret = file_writable(wimfile);
- if (ret != 0)
- return ret;
-
ret = wimlib_open_wim(wimfile, open_flags, &w,
imagex_progress_func);
if (ret != 0)
}
ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
- if (ret == WIMLIB_ERR_REOPEN)
- ret = 0;
if (ret != 0) {
imagex_error(T("Failed to write the file \"%"TS"\" with image "
"deleted"), wimfile);
return ret;
}
+static int
+print_full_path(const struct wimlib_wim_dentry *wdentry, void *_ignore)
+{
+ tprintf(T("%"TS"\n"), wdentry->full_path);
+ return 0;
+}
+
/* Print the files contained in an image(s) in a WIM file. */
static int
imagex_dir(int argc, tchar **argv)
image = 1;
}
- ret = wimlib_print_files(w, image);
+ ret = wimlib_iterate_dir_tree(w, image, T(""),
+ WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
+ print_full_path, NULL);
out:
wimlib_free(w);
return ret;
ret = -1;
goto out;
}
- ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
- imagex_progress_func);
- if (ret != 0)
- goto out;
-
- ret = file_writable(dest_wimfile);
+ ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
+ &dest_w, imagex_progress_func);
if (ret != 0)
goto out;
ret = wimlib_overwrite(dest_w, write_flags, num_threads,
imagex_progress_func);
out:
- if (ret == WIMLIB_ERR_REOPEN)
- ret = 0;
wimlib_free(src_w);
wimlib_free(dest_w);
if (additional_swms) {
free_extract_commands(cmds, num_cmds, dest_dir);
return NULL;
}
- tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
- tbasename(paths[i]));
+ tsprintf(cmds[i].fs_dest_path, T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
+ dest_dir, tbasename(paths[i]));
}
}
*num_cmds_ret = num_cmds;
} else {
/* Modification operations */
- if (total_parts != 1) {
- imagex_error(T("Modifying a split WIM is not supported."));
- ret = -1;
- goto out;
- }
+
if (image == WIMLIB_ALL_IMAGES)
image = 1;
} else {
tprintf(T("Marking image %d as bootable.\n"),
image);
- wimlib_set_boot_idx(w, image);
+ ret = wimlib_set_boot_idx(w, image);
+ if (ret)
+ goto out;
}
}
if (new_name) {
if (boot || new_name || new_desc ||
(check && !wimlib_has_integrity_table(w)))
{
- int write_flags;
-
- ret = file_writable(wimfile);
- if (ret != 0)
- goto out;
+ int write_flags = 0;
if (check)
- write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
- else
- write_flags = 0;
-
+ write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
ret = wimlib_overwrite(w, write_flags, 1,
imagex_progress_func);
- if (ret == WIMLIB_ERR_REOPEN)
- ret = 0;
} else {
tprintf(T("The file \"%"TS"\" was not modified because nothing "
"needed to be done.\n"), wimfile);
unsigned num_additional_swms = 0;
const tchar *staging_dir = NULL;
- if (!tstrcmp(argv[0], T("mountrw")))
+ if (!tstrcmp(argv[0], T("mountrw"))) {
mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
+ open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
+ }
for_opt(c, mount_options) {
switch (c) {
goto out;
}
- if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
- ret = file_writable(wimfile);
- if (ret != 0)
- goto out;
- }
-
ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
num_additional_swms, staging_dir);
if (ret != 0) {
imagex_optimize(int argc, tchar **argv)
{
int c;
- int open_flags = 0;
+ int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
int ret;
WIMStruct *w;
wimfile = argv[0];
- ret = file_writable(wimfile);
- if (ret)
- return ret;
-
ret = wimlib_open_wim(wimfile, open_flags, &w,
imagex_progress_func);
if (ret)
int image;
WIMStruct *wim;
int ret;
- int open_flags = 0;
+ int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
int write_flags = 0;
- int update_flags = 0;
+ int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
int default_delete_flags = 0;
unsigned num_threads = 0;
goto out_usage;
wimfile = argv[0];
- ret = file_writable(wimfile);
- if (ret)
- goto out;
-
ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
if (ret)
goto out;
}
/* Set default flags and capture config on the update commands */
+ bool have_add_command = false;
for (size_t i = 0; i < num_cmds; i++) {
switch (cmds[i].op) {
case WIMLIB_UPDATE_OP_ADD:
cmds[i].add.add_flags |= default_add_flags;
cmds[i].add.config = config;
+ have_add_command = true;
break;
case WIMLIB_UPDATE_OP_DELETE:
cmds[i].delete.delete_flags |= default_delete_flags;
}
#ifdef __WIN32__
- win32_acquire_capture_privileges();
+ if (have_add_command)
+ win32_acquire_capture_privileges();
#endif
/* Execute the update commands */
imagex_progress_func);
out_release_privs:
#ifdef __WIN32__
- win32_release_capture_privileges();
+ if (have_add_command)
+ win32_release_capture_privileges();
#endif
-out_free_cmds:
free(cmds);
out_free_cmd_file_contents:
free(cmd_file_contents);