summaryrefslogtreecommitdiffstats
path: root/daemon/realpath.c
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2009-10-26 08:20:00 +0000
committerRichard Jones <rjones@redhat.com>2009-10-26 13:05:07 +0000
commit9add3c10a3b769e309f476bd0fd05e2a7126d31d (patch)
treec82da16fb1d6ffe328ad2da9b6559688965ad81a /daemon/realpath.c
parenta4217192672fb4f2ff60efcacc59a202d9f77bd9 (diff)
downloadlibguestfs-9add3c10a3b769e309f476bd0fd05e2a7126d31d.tar.gz
libguestfs-9add3c10a3b769e309f476bd0fd05e2a7126d31d.tar.xz
libguestfs-9add3c10a3b769e309f476bd0fd05e2a7126d31d.zip
New API: case-sensitive-path to return case sensitive path on NTFS 3g fs
This function handles an annoyance/peculiarity of the Linux NTFS 3g driver, which is that it exports NTFS filesystems with names case sensitive, even though under Windows they would be case insensitive. This causes problems because the location of (eg.) c:\windows might appear as /windows or /WINDOWS (etc) depending on the inconsequential details of how it was originally created. Example of this problem on a real Windows guest: ><fs> file /windows/system32/config/system.log libguestfs: error: file: access: /windows/system32/config/system.log: No such file or directory ><fs> case-sensitive-path /windows/system32/config/system.log /WINDOWS/system32/config/system.LOG ><fs> file /WINDOWS/system32/config/system.LOG MS Windows registry file, NT/2000 or above
Diffstat (limited to 'daemon/realpath.c')
-rw-r--r--daemon/realpath.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/daemon/realpath.c b/daemon/realpath.c
index 706af427..bfe8e676 100644
--- a/daemon/realpath.c
+++ b/daemon/realpath.c
@@ -23,6 +23,10 @@
#include <string.h>
#include <unistd.h>
#include <limits.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+#include "ignore-value.h"
#include "daemon.h"
#include "actions.h"
@@ -42,3 +46,121 @@ do_realpath (const char *path)
return ret; /* caller frees */
}
+
+char *
+do_case_sensitive_path (const char *path)
+{
+ char ret[PATH_MAX+1] = "/";
+ size_t next = 1;
+
+ /* MUST chdir ("/") before leaving this function. */
+ if (chdir (sysroot) == -1) {
+ reply_with_perror ("%s", sysroot);
+ return NULL;
+ }
+
+ /* First character is a '/'. Take each subsequent path element
+ * and follow it.
+ */
+ while (*path) {
+ size_t i = strcspn (path, "/");
+ if (i == 0) {
+ path++;
+ continue;
+ }
+
+ if (verbose)
+ fprintf (stderr, "case_sensitive_path: path = %s, next = %zu, i = %zu\n",
+ path, next, i);
+
+ if ((i == 1 && path[0] == '.') ||
+ (i == 2 && path[0] == '.' && path[1] == '.')) {
+ reply_with_error ("case_sensitive_path: path contained . or .. elements");
+ goto error;
+ }
+ if (i > NAME_MAX) {
+ reply_with_error ("case_sensitive_path: path element too long");
+ goto error;
+ }
+
+ char name[NAME_MAX+1];
+ memcpy (name, path, i);
+ name[i] = '\0';
+
+ /* Skip to next element in path (for the next loop iteration). */
+ path += i;
+
+ /* Read the current directory looking (case insensitively) for
+ * this element of the path.
+ */
+ DIR *dir = opendir (".");
+ if (dir == NULL) {
+ reply_with_perror ("opendir");
+ goto error;
+ }
+
+ struct dirent *d = NULL;
+
+ errno = 0;
+ while ((d = readdir (dir)) != NULL) {
+ if (strcasecmp (d->d_name, name) == 0)
+ break;
+ }
+
+ if (d == NULL && errno != 0) {
+ reply_with_perror ("readdir");
+ goto error;
+ }
+
+ if (closedir (dir) == -1) {
+ reply_with_perror ("closedir");
+ goto error;
+ }
+
+ if (d == NULL) {
+ reply_with_error ("%s: no file or directory found with this name", name);
+ goto error;
+ }
+
+ /* Add the real name of this path element to the return value. */
+ if (next > 1)
+ ret[next++] = '/';
+
+ i = strlen (d->d_name);
+ if (next + i >= PATH_MAX) {
+ reply_with_error ("final path too long");
+ goto error;
+ }
+
+ strcpy (&ret[next], d->d_name);
+ next += i;
+
+ /* Is it a directory? Try going into it. */
+ if (chdir (d->d_name) == -1) {
+ /* ENOTDIR is OK provided we've reached the end of the path. */
+ if (errno != ENOTDIR) {
+ reply_with_perror ("chdir: %s", d->d_name);
+ goto error;
+ }
+
+ if (*path) {
+ reply_with_error ("%s: non-directory element in path", d->d_name);
+ goto error;
+ }
+ }
+ }
+
+ ignore_value (chdir ("/"));
+
+ ret[next] = '\0';
+ char *retp = strdup (ret);
+ if (retp == NULL) {
+ reply_with_perror ("strdup");
+ return NULL;
+ }
+ return retp; /* caller frees */
+
+ error:
+ ignore_value (chdir ("/"));
+ return NULL;
+}