OSDN Git Service

Supported the directory separator on Windows in ntfsprogs (cosmetic)
authorJean-Pierre André <jpandre@users.sourceforge.net>
Fri, 17 Apr 2015 07:15:25 +0000 (09:15 +0200)
committerJean-Pierre André <jpandre@users.sourceforge.net>
Fri, 17 Apr 2015 07:15:25 +0000 (09:15 +0200)
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
ntfsprogs/ntfscp.c
ntfsprogs/ntfsfallocate.c
ntfsprogs/ntfsinfo.c
ntfsprogs/utils.c
ntfsprogs/utils.h

index 2ae1f9c..cbcfb97 100644 (file)
@@ -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");
index 87a7da3..4cf0530 100644 (file)
@@ -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);
index 1b243ae..eeb1347 100644 (file)
@@ -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
index 817eadc..eb47703 100644 (file)
@@ -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));
                }
index 7ac3116..cdd556e 100644 (file)
@@ -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
index 8b6bfae..396f698 100644 (file)
@@ -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)