New API: case-sensitive-path to return case sensitive path on NTFS 3g fs
[libguestfs.git] / daemon / realpath.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28
29 #include "ignore-value.h"
30
31 #include "daemon.h"
32 #include "actions.h"
33
34 char *
35 do_realpath (const char *path)
36 {
37   char *ret;
38
39   CHROOT_IN;
40   ret = realpath (path, NULL);
41   CHROOT_OUT;
42   if (ret == NULL) {
43     reply_with_perror ("realpath");
44     return NULL;
45   }
46
47   return ret;                   /* caller frees */
48 }
49
50 char *
51 do_case_sensitive_path (const char *path)
52 {
53   char ret[PATH_MAX+1] = "/";
54   size_t next = 1;
55
56   /* MUST chdir ("/") before leaving this function. */
57   if (chdir (sysroot) == -1) {
58     reply_with_perror ("%s", sysroot);
59     return NULL;
60   }
61
62   /* First character is a '/'.  Take each subsequent path element
63    * and follow it.
64    */
65   while (*path) {
66     size_t i = strcspn (path, "/");
67     if (i == 0) {
68       path++;
69       continue;
70     }
71
72     if (verbose)
73       fprintf (stderr, "case_sensitive_path: path = %s, next = %zu, i = %zu\n",
74                path, next, i);
75
76     if ((i == 1 && path[0] == '.') ||
77         (i == 2 && path[0] == '.' && path[1] == '.')) {
78       reply_with_error ("case_sensitive_path: path contained . or .. elements");
79       goto error;
80     }
81     if (i > NAME_MAX) {
82       reply_with_error ("case_sensitive_path: path element too long");
83       goto error;
84     }
85
86     char name[NAME_MAX+1];
87     memcpy (name, path, i);
88     name[i] = '\0';
89
90     /* Skip to next element in path (for the next loop iteration). */
91     path += i;
92
93     /* Read the current directory looking (case insensitively) for
94      * this element of the path.
95      */
96     DIR *dir = opendir (".");
97     if (dir == NULL) {
98       reply_with_perror ("opendir");
99       goto error;
100     }
101
102     struct dirent *d = NULL;
103
104     errno = 0;
105     while ((d = readdir (dir)) != NULL) {
106       if (strcasecmp (d->d_name, name) == 0)
107         break;
108     }
109
110     if (d == NULL && errno != 0) {
111       reply_with_perror ("readdir");
112       goto error;
113     }
114
115     if (closedir (dir) == -1) {
116       reply_with_perror ("closedir");
117       goto error;
118     }
119
120     if (d == NULL) {
121       reply_with_error ("%s: no file or directory found with this name", name);
122       goto error;
123     }
124
125     /* Add the real name of this path element to the return value. */
126     if (next > 1)
127       ret[next++] = '/';
128
129     i = strlen (d->d_name);
130     if (next + i >= PATH_MAX) {
131       reply_with_error ("final path too long");
132       goto error;
133     }
134
135     strcpy (&ret[next], d->d_name);
136     next += i;
137
138     /* Is it a directory?  Try going into it. */
139     if (chdir (d->d_name) == -1) {
140       /* ENOTDIR is OK provided we've reached the end of the path. */
141       if (errno != ENOTDIR) {
142         reply_with_perror ("chdir: %s", d->d_name);
143         goto error;
144       }
145
146       if (*path) {
147         reply_with_error ("%s: non-directory element in path", d->d_name);
148         goto error;
149       }
150     }
151   }
152
153   ignore_value (chdir ("/"));
154
155   ret[next] = '\0';
156   char *retp = strdup (ret);
157   if (retp == NULL) {
158     reply_with_perror ("strdup");
159     return NULL;
160   }
161   return retp;                  /* caller frees */
162
163  error:
164   ignore_value (chdir ("/"));
165   return NULL;
166 }