1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
26 #include <sys/types.h>
29 #include "ignore-value.h"
35 do_realpath (const char *path)
40 ret = realpath (path, NULL);
43 reply_with_perror ("realpath");
47 return ret; /* caller frees */
51 do_case_sensitive_path (const char *path)
53 char ret[PATH_MAX+1] = "/";
56 /* MUST chdir ("/") before leaving this function. */
57 if (chdir (sysroot) == -1) {
58 reply_with_perror ("%s", sysroot);
62 /* First character is a '/'. Take each subsequent path element
66 size_t i = strcspn (path, "/");
73 fprintf (stderr, "case_sensitive_path: path = %s, next = %zu, i = %zu\n",
76 if ((i == 1 && path[0] == '.') ||
77 (i == 2 && path[0] == '.' && path[1] == '.')) {
78 reply_with_error ("case_sensitive_path: path contained . or .. elements");
82 reply_with_error ("case_sensitive_path: path element too long");
86 char name[NAME_MAX+1];
87 memcpy (name, path, i);
90 /* Skip to next element in path (for the next loop iteration). */
93 /* Read the current directory looking (case insensitively) for
94 * this element of the path.
96 DIR *dir = opendir (".");
98 reply_with_perror ("opendir");
102 struct dirent *d = NULL;
105 while ((d = readdir (dir)) != NULL) {
106 if (strcasecmp (d->d_name, name) == 0)
110 if (d == NULL && errno != 0) {
111 reply_with_perror ("readdir");
115 if (closedir (dir) == -1) {
116 reply_with_perror ("closedir");
121 reply_with_error ("%s: no file or directory found with this name", name);
125 /* Add the real name of this path element to the return value. */
129 i = strlen (d->d_name);
130 if (next + i >= PATH_MAX) {
131 reply_with_error ("final path too long");
135 strcpy (&ret[next], d->d_name);
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);
147 reply_with_error ("%s: non-directory element in path", d->d_name);
153 ignore_value (chdir ("/"));
156 char *retp = strdup (ret);
158 reply_with_perror ("strdup");
161 return retp; /* caller frees */
164 ignore_value (chdir ("/"));