1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
31 #include "optgroups.h"
34 /* On Windows, NAME_MAX is not defined. */
36 #define NAME_MAX FILENAME_MAX
40 optgroup_realpath_available (void)
50 do_realpath (const char *path)
56 ret = realpath (path, NULL);
59 reply_with_perror ("%s", path);
63 return ret; /* caller frees */
70 do_case_sensitive_path (const char *path)
72 char ret[PATH_MAX+1] = "/";
76 /* 'fd_cwd' here is a surrogate for the current working directory, so
77 * that we don't have to actually call chdir(2).
79 fd_cwd = open (sysroot, O_RDONLY | O_DIRECTORY);
81 reply_with_perror ("%s", sysroot);
85 /* First character is a '/'. Take each subsequent path element
89 size_t i = strcspn (path, "/");
95 if ((i == 1 && path[0] == '.') ||
96 (i == 2 && path[0] == '.' && path[1] == '.')) {
97 reply_with_error ("path contained . or .. elements");
101 reply_with_error ("path element too long");
105 char name[NAME_MAX+1];
106 memcpy (name, path, i);
109 /* Skip to next element in path (for the next loop iteration). */
112 /* Read the current directory looking (case insensitively) for
113 * this element of the path.
115 int fd2 = dup (fd_cwd); /* because closedir will close it */
117 reply_with_perror ("dup");
120 DIR *dir = fdopendir (fd2);
122 reply_with_perror ("opendir");
126 struct dirent *d = NULL;
129 while ((d = readdir (dir)) != NULL) {
130 if (STRCASEEQ (d->d_name, name))
134 if (d == NULL && errno != 0) {
135 reply_with_perror ("readdir");
139 if (closedir (dir) == -1) {
140 reply_with_perror ("closedir");
145 reply_with_error ("%s: no file or directory found with this name", name);
149 /* Add the real name of this path element to the return value. */
153 i = strlen (d->d_name);
154 if (next + i >= PATH_MAX) {
155 reply_with_error ("final path too long");
159 strcpy (&ret[next], d->d_name);
162 /* Is it a directory? Try going into it. */
163 fd2 = openat (fd_cwd, d->d_name, O_RDONLY | O_DIRECTORY);
169 /* ENOTDIR is OK provided we've reached the end of the path. */
170 if (errno != ENOTDIR) {
171 reply_with_perror ("openat: %s", d->d_name);
176 reply_with_error ("%s: non-directory element in path", d->d_name);
186 char *retp = strdup (ret);
188 reply_with_perror ("strdup");
191 return retp; /* caller frees */