From 4340df770ecf5f5865124a297d35edbcb456c42d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Pierre=20Andr=C3=A9?= Date: Fri, 17 Apr 2015 09:15:25 +0200 Subject: [PATCH] Supported the directory separator on Windows in ntfsprogs (cosmetic) On Windows, when an ntfsprogs utility requests a path translation, translate the '\'s to '/'s so that only '/' have to be interpreted in libntfs. --- ntfsprogs/ntfscat.c | 15 ++++++++++++++- ntfsprogs/ntfscp.c | 25 +++++++++++++++++++++++-- ntfsprogs/ntfsfallocate.c | 9 +-------- ntfsprogs/ntfsinfo.c | 12 ++++++++++++ ntfsprogs/utils.c | 24 ++++++++++++++++++++++++ ntfsprogs/utils.h | 1 + 6 files changed, 75 insertions(+), 11 deletions(-) diff --git a/ntfsprogs/ntfscat.c b/ntfsprogs/ntfscat.c index 2ae1f9cd..cbcfb979 100644 --- a/ntfsprogs/ntfscat.c +++ b/ntfsprogs/ntfscat.c @@ -423,8 +423,21 @@ int main(int argc, char *argv[]) if (opts.inode != -1) inode = ntfs_inode_open(vol, opts.inode); - else + else { +#ifdef HAVE_WINDOWS_H + char *unix_name; + + unix_name = ntfs_utils_unix_path(opts.file); + if (unix_name) { + inode = ntfs_pathname_to_inode(vol, NULL, + unix_name); + free(unix_name); + } else + inode = (ntfs_inode*)NULL; +#else inode = ntfs_pathname_to_inode(vol, NULL, opts.file); +#endif + } if (!inode) { ntfs_log_perror("ERROR: Couldn't open inode"); diff --git a/ntfsprogs/ntfscp.c b/ntfsprogs/ntfscp.c index 87a7da30..4cf05301 100644 --- a/ntfsprogs/ntfscp.c +++ b/ntfsprogs/ntfscp.c @@ -834,6 +834,9 @@ int main(int argc, char *argv[]) s64 br, bw; ntfschar *attr_name; int attr_name_len = 0; +#ifdef HAVE_WINDOWS_H + char *unix_name; +#endif ntfs_log_set_handler(ntfs_log_handler_stderr); @@ -900,8 +903,17 @@ int main(int argc, char *argv[]) goto close_src; } out = ntfs_inode_open(vol, inode_num); - } else + } else { +#ifdef HAVE_WINDOWS_H + unix_name = ntfs_utils_unix_path(opts.dest_file); + if (unix_name) { + out = ntfs_pathname_to_inode(vol, NULL, unix_name); + } else + out = (ntfs_inode*)NULL; +#else out = ntfs_pathname_to_inode(vol, NULL, opts.dest_file); +#endif + } if (!out) { /* Copy the file if the dest_file's parent dir can be opened. */ char *parent_dirname; @@ -910,8 +922,13 @@ int main(int argc, char *argv[]) ntfs_inode *ni; char *dirname_last_whack; +#ifdef HAVE_WINDOWS_H + filename = basename(unix_name); + parent_dirname = strdup(unix_name); +#else filename = basename(opts.dest_file); parent_dirname = strdup(opts.dest_file); +#endif if (!parent_dirname) { ntfs_log_perror("strdup() failed"); goto close_src; @@ -983,8 +1000,12 @@ int main(int argc, char *argv[]) ntfs_inode_close(out); goto close_src; } +#ifdef HAVE_WINDOWS_H + strcpy(overwrite_filename, unix_name); +#else strcpy(overwrite_filename, opts.dest_file); - if (opts.dest_file[dest_dirname_len - 1] != '/') { +#endif + if (overwrite_filename[dest_dirname_len - 1] != '/') { strcat(overwrite_filename, "/"); } strcat(overwrite_filename, filename); diff --git a/ntfsprogs/ntfsfallocate.c b/ntfsprogs/ntfsfallocate.c index 1b243ae6..eeb13470 100644 --- a/ntfsprogs/ntfsfallocate.c +++ b/ntfsprogs/ntfsfallocate.c @@ -859,15 +859,8 @@ int main(int argc, char **argv) /* Open the specified inode. */ #ifdef HAVE_WINDOWS_H - unix_name = (char*)malloc(strlen(file_name) + 1); + unix_name = ntfs_utils_unix_path(file_name); if (unix_name) { - int i; - for (i=0; file_name[i]; i++) - if (file_name[i] == '\\') - unix_name[i] = '/'; - else - unix_name[i] = file_name[i]; - unix_name[i] = 0; ni = ntfs_pathname_to_inode(vol, NULL, unix_name); free(unix_name); } else diff --git a/ntfsprogs/ntfsinfo.c b/ntfsprogs/ntfsinfo.c index 817eadc4..eb477037 100644 --- a/ntfsprogs/ntfsinfo.c +++ b/ntfsprogs/ntfsinfo.c @@ -2379,8 +2379,20 @@ int main(int argc, char **argv) ntfs_inode *inode; /* obtain the inode */ if (opts.filename) { +#ifdef HAVE_WINDOWS_H + char *unix_name; + + unix_name = ntfs_utils_unix_path(opts.filename); + if (unix_name) { + inode = ntfs_pathname_to_inode(vol, NULL, + unix_name); + free(unix_name); + } else + inode = (ntfs_inode*)NULL; +#else inode = ntfs_pathname_to_inode(vol, NULL, opts.filename); +#endif } else { inode = ntfs_inode_open(vol, MK_MREF(opts.inode, 0)); } diff --git a/ntfsprogs/utils.c b/ntfsprogs/utils.c index 7ac31163..cdd556e1 100644 --- a/ntfsprogs/utils.c +++ b/ntfsprogs/utils.c @@ -1201,4 +1201,28 @@ char *ntfs_utils_reformat(char *out, int sz, const char *fmt) return (out); } +/* + * Translate paths to files submitted from Windows + * + * Translate Windows directory separators to Unix ones + * + * Returns the translated path, to be freed by caller + * NULL if there was an error, with errno set + */ + +char *ntfs_utils_unix_path(const char *in) +{ + char *out; + int i; + + out = strdup(in); + if (out) { + for (i=0; in[i]; i++) + if (in[i] == '\\') + out[i] = '/'; + } else + errno = ENOMEM; + return (out); +} + #endif diff --git a/ntfsprogs/utils.h b/ntfsprogs/utils.h index 8b6bfae5..396f698e 100644 --- a/ntfsprogs/utils.h +++ b/ntfsprogs/utils.h @@ -107,6 +107,7 @@ int mft_next_record(struct mft_search_ctx *ctx); */ #define MAX_FMT 1536 char *ntfs_utils_reformat(char *out, int sz, const char *fmt); +char *ntfs_utils_unix_path(const char *in); #define ntfs_log_redirect(fn,fi,li,le,d,fmt, args...) \ do { char buf[MAX_FMT]; ntfs_log_redirect(fn,fi,li,le,d, \ ntfs_utils_reformat(buf,MAX_FMT,fmt), args); } while (0) -- 2.11.0