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.
27 #include <sys/types.h>
36 do_realpath (const char *path)
41 ret = realpath (path, NULL);
44 reply_with_perror ("realpath");
48 return ret; /* caller frees */
52 do_case_sensitive_path (const char *path)
54 char ret[PATH_MAX+1] = "/";
58 /* 'fd_cwd' here is a surrogate for the current working directory, so
59 * that we don't have to actually call chdir(2).
61 fd_cwd = open (sysroot, O_RDONLY | O_DIRECTORY);
63 reply_with_perror ("%s", sysroot);
67 /* First character is a '/'. Take each subsequent path element
71 size_t i = strcspn (path, "/");
78 fprintf (stderr, "case_sensitive_path: path = %s, next = %zu, i = %zu\n",
81 if ((i == 1 && path[0] == '.') ||
82 (i == 2 && path[0] == '.' && path[1] == '.')) {
83 reply_with_error ("case_sensitive_path: path contained . or .. elements");
87 reply_with_error ("case_sensitive_path: path element too long");
91 char name[NAME_MAX+1];
92 memcpy (name, path, i);
95 /* Skip to next element in path (for the next loop iteration). */
98 /* Read the current directory looking (case insensitively) for
99 * this element of the path.
101 int fd2 = dup (fd_cwd); /* because closedir will close it */
103 reply_with_perror ("dup");
106 DIR *dir = fdopendir (fd2);
108 reply_with_perror ("opendir");
112 struct dirent *d = NULL;
115 while ((d = readdir (dir)) != NULL) {
116 if (strcasecmp (d->d_name, name) == 0)
120 if (d == NULL && errno != 0) {
121 reply_with_perror ("readdir");
125 if (closedir (dir) == -1) {
126 reply_with_perror ("closedir");
131 reply_with_error ("%s: no file or directory found with this name", name);
135 /* Add the real name of this path element to the return value. */
139 i = strlen (d->d_name);
140 if (next + i >= PATH_MAX) {
141 reply_with_error ("final path too long");
145 strcpy (&ret[next], d->d_name);
148 /* Is it a directory? Try going into it. */
149 fd2 = openat (fd_cwd, d->d_name, O_RDONLY | O_DIRECTORY);
155 /* ENOTDIR is OK provided we've reached the end of the path. */
156 if (errno != ENOTDIR) {
157 reply_with_perror ("openat: %s", d->d_name);
162 reply_with_error ("%s: non-directory element in path", d->d_name);
171 char *retp = strdup (ret);
173 reply_with_perror ("strdup");
176 return retp; /* caller frees */