1 // Directory utilities. This library contains functions for locating configuration directories, for
2 // testing if a command with a given name can be found in the PATH, and various other path-related
3 // issues.
4 #include "config.h" // IWYU pragma: keep
5
6 #include "path.h"
7
8 #include <errno.h>
9 #include <sys/mount.h>
10 #include <sys/param.h>
11 #include <sys/stat.h>
12 #if defined(__linux__)
13 #include <sys/statfs.h>
14 #endif
15 #include <unistd.h>
16
17 #include <cstring>
18 #include <cwchar>
19 #include <memory>
20 #include <string>
21 #include <type_traits>
22 #include <vector>
23
24 #include "common.h"
25 #include "env.h"
26 #include "expand.h"
27 #include "fallback.h" // IWYU pragma: keep
28 #include "flog.h"
29 #include "wcstringutil.h"
30 #include "wutil.h" // IWYU pragma: keep
31
32 // Note that PREFIX is defined in the `Makefile` and is thus defined when this module is compiled.
33 // This ensures we always default to "/bin", "/usr/bin" and the bin dir defined for the fish
34 // programs. Possibly with a duplicate dir if PREFIX is empty, "/", "/usr" or "/usr/". If the PREFIX
35 // duplicates /bin or /usr/bin that is harmless other than a trivial amount of time testing a path
36 // we've already tested.
37 const wcstring_list_t dflt_pathsv({L"/bin", L"/usr/bin", PREFIX L"/bin"});
38
path_get_path_core(const wcstring & cmd,wcstring * out_path,const maybe_t<env_var_t> & bin_path_var)39 static bool path_get_path_core(const wcstring &cmd, wcstring *out_path,
40 const maybe_t<env_var_t> &bin_path_var) {
41 // If the command has a slash, it must be an absolute or relative path and thus we don't bother
42 // looking for a matching command.
43 if (cmd.find(L'/') != wcstring::npos) {
44 std::string narrow = wcs2string(cmd);
45 if (access(narrow.c_str(), X_OK) != 0) {
46 return false;
47 }
48
49 struct stat buff;
50 if (stat(narrow.c_str(), &buff)) {
51 return false;
52 }
53 if (S_ISREG(buff.st_mode)) {
54 if (out_path) out_path->assign(cmd);
55 return true;
56 }
57 errno = EACCES;
58 return false;
59 }
60
61 const wcstring_list_t *pathsv;
62 if (bin_path_var) {
63 pathsv = &bin_path_var->as_list();
64 } else {
65 pathsv = &dflt_pathsv;
66 }
67
68 int err = ENOENT;
69 for (auto next_path : *pathsv) {
70 if (next_path.empty()) continue;
71 append_path_component(next_path, cmd);
72 std::string narrow = wcs2string(next_path);
73 if (access(narrow.c_str(), X_OK) == 0) {
74 struct stat buff;
75 if (stat(narrow.c_str(), &buff) == -1) {
76 if (errno != EACCES) {
77 wperror(L"stat");
78 }
79 continue;
80 }
81 if (S_ISREG(buff.st_mode)) {
82 if (out_path) *out_path = std::move(next_path);
83 return true;
84 }
85 err = EACCES;
86 }
87 }
88
89 errno = err;
90 return false;
91 }
92
path_get_path(const wcstring & cmd,wcstring * out_path,const environment_t & vars)93 bool path_get_path(const wcstring &cmd, wcstring *out_path, const environment_t &vars) {
94 return path_get_path_core(cmd, out_path, vars.get(L"PATH"));
95 }
96
path_is_executable(const std::string & path)97 bool path_is_executable(const std::string &path) {
98 if (access(path.c_str(), X_OK)) return false;
99 struct stat buff;
100 if (stat(path.c_str(), &buff) == -1) {
101 if (errno != EACCES) wperror(L" stat");
102 return false;
103 }
104 if (!S_ISREG(buff.st_mode)) return false;
105 return true;
106 }
107
108 /// \return 1 if the path is remote, 0 if local, -1 if unknown.
path_is_remote(const wcstring & path)109 static int path_is_remote(const wcstring &path) {
110 std::string narrow = wcs2string(path);
111 #if defined(__linux__)
112 struct statfs buf {};
113 if (statfs(narrow.c_str(), &buf) < 0) {
114 return -1;
115 }
116 // Linux has constants for these like NFS_SUPER_MAGIC, SMB_SUPER_MAGIC, CIFS_MAGIC_NUMBER but
117 // these are in varying headers. Simply hard code them.
118 // NOTE: The cast is necessary for 32-bit systems because of the 4-byte CIFS_MAGIC_NUMBER
119 switch (static_cast<unsigned int>(buf.f_type)) {
120 case 0x6969: // NFS_SUPER_MAGIC
121 case 0x517B: // SMB_SUPER_MAGIC
122 case 0xFE534D42U: // SMB2_MAGIC_NUMBER - not in the manpage
123 case 0xFF534D42U: // CIFS_MAGIC_NUMBER
124 return 1;
125 default:
126 // Other FSes are assumed local.
127 return 0;
128 }
129 #elif defined(ST_LOCAL)
130 // ST_LOCAL is a flag to statvfs, which is itself standardized.
131 // In practice the only system to use this path is NetBSD.
132 struct statvfs buf {};
133 if (statvfs(narrow.c_str(), &buf) < 0) return -1;
134 return (buf.f_flag & ST_LOCAL) ? 0 : 1;
135 #elif defined(MNT_LOCAL)
136 struct statfs buf {};
137 if (statfs(narrow.c_str(), &buf) < 0) return -1;
138 return (buf.f_flags & MNT_LOCAL) ? 0 : 1;
139 #else
140 return -1;
141 #endif
142 }
143
path_get_paths(const wcstring & cmd,const environment_t & vars)144 wcstring_list_t path_get_paths(const wcstring &cmd, const environment_t &vars) {
145 FLOGF(path, L"path_get_paths('%ls')", cmd.c_str());
146 wcstring_list_t paths;
147
148 // If the command has a slash, it must be an absolute or relative path and thus we don't bother
149 // looking for matching commands in the PATH var.
150 if (cmd.find(L'/') != wcstring::npos) {
151 std::string narrow = wcs2string(cmd);
152 if (path_is_executable(narrow)) paths.push_back(cmd);
153 return paths;
154 }
155
156 auto path_var = vars.get(L"PATH");
157 if (!path_var) return paths;
158
159 const wcstring_list_t &pathsv = path_var->as_list();
160 for (auto path : pathsv) {
161 if (path.empty()) continue;
162 append_path_component(path, cmd);
163 std::string narrow = wcs2string(path);
164 if (path_is_executable(narrow)) paths.push_back(path);
165 }
166
167 return paths;
168 }
169
path_apply_cdpath(const wcstring & dir,const wcstring & wd,const environment_t & env_vars)170 wcstring_list_t path_apply_cdpath(const wcstring &dir, const wcstring &wd,
171 const environment_t &env_vars) {
172 wcstring_list_t paths;
173 if (dir.at(0) == L'/') {
174 // Absolute path.
175 paths.push_back(dir);
176 } else if (string_prefixes_string(L"./", dir) || string_prefixes_string(L"../", dir) ||
177 dir == L"." || dir == L"..") {
178 // Path is relative to the working directory.
179 paths.push_back(path_normalize_for_cd(wd, dir));
180 } else {
181 // Respect CDPATH.
182 wcstring_list_t cdpathsv;
183 if (auto cdpaths = env_vars.get(L"CDPATH")) {
184 cdpathsv = cdpaths->as_list();
185 }
186 // Always append $PWD
187 cdpathsv.push_back(L".");
188 for (wcstring next_path : cdpathsv) {
189 if (next_path.empty()) next_path = L".";
190 if (next_path == L".") {
191 // next_path is just '.', and we have a working directory, so use the wd instead.
192 next_path = wd;
193 }
194
195 // We want to return an absolute path (see issue 6220)
196 if (string_prefixes_string(L"./", next_path)) {
197 next_path = next_path.replace(0, 2, wd);
198 } else if (string_prefixes_string(L"../", next_path) || next_path == L"..") {
199 next_path = next_path.insert(0, wd);
200 }
201
202 expand_tilde(next_path, env_vars);
203 if (next_path.empty()) continue;
204
205 wcstring whole_path = std::move(next_path);
206 append_path_component(whole_path, dir);
207 paths.push_back(whole_path);
208 }
209 }
210
211 return paths;
212 }
213
path_get_cdpath(const wcstring & dir,const wcstring & wd,const environment_t & env_vars)214 maybe_t<wcstring> path_get_cdpath(const wcstring &dir, const wcstring &wd,
215 const environment_t &env_vars) {
216 int err = ENOENT;
217 if (dir.empty()) return none();
218 assert(!wd.empty() && wd.back() == L'/');
219 auto paths = path_apply_cdpath(dir, wd, env_vars);
220
221 for (const wcstring &dir : paths) {
222 struct stat buf;
223 if (wstat(dir, &buf) == 0) {
224 if (S_ISDIR(buf.st_mode)) {
225 return dir;
226 }
227 err = ENOTDIR;
228 }
229 }
230
231 errno = err;
232 return none();
233 }
234
path_as_implicit_cd(const wcstring & path,const wcstring & wd,const environment_t & vars)235 maybe_t<wcstring> path_as_implicit_cd(const wcstring &path, const wcstring &wd,
236 const environment_t &vars) {
237 wcstring exp_path = path;
238 expand_tilde(exp_path, vars);
239 if (string_prefixes_string(L"/", exp_path) || string_prefixes_string(L"./", exp_path) ||
240 string_prefixes_string(L"../", exp_path) || string_suffixes_string(L"/", exp_path) ||
241 exp_path == L"..") {
242 // These paths can be implicit cd, so see if you cd to the path. Note that a single period
243 // cannot (that's used for sourcing files anyways).
244 return path_get_cdpath(exp_path, wd, vars);
245 }
246 return none();
247 }
248
249 // If the given path looks like it's relative to the working directory, then prepend that working
250 // directory. This operates on unescaped paths only (so a ~ means a literal ~).
path_apply_working_directory(const wcstring & path,const wcstring & working_directory)251 wcstring path_apply_working_directory(const wcstring &path, const wcstring &working_directory) {
252 if (path.empty() || working_directory.empty()) return path;
253
254 // We're going to make sure that if we want to prepend the wd, that the string has no leading
255 // "/".
256 bool prepend_wd = path.at(0) != L'/' && path.at(0) != HOME_DIRECTORY;
257 if (!prepend_wd) {
258 // No need to prepend the wd, so just return the path we were given.
259 return path;
260 }
261
262 // Remove up to one "./".
263 wcstring path_component = path;
264 if (string_prefixes_string(L"./", path_component)) {
265 path_component.erase(0, 2);
266 }
267
268 // Removing leading /s.
269 while (string_prefixes_string(L"/", path_component)) {
270 path_component.erase(0, 1);
271 }
272
273 // Construct and return a new path.
274 wcstring new_path = working_directory;
275 append_path_component(new_path, path_component);
276 return new_path;
277 }
278
279 /// We separate this from path_create() for two reasons. First it's only caused if there is a
280 /// problem, and thus is not central to the behavior of that function. Second, we only want to issue
281 /// the message once. If the current shell starts a new fish shell (e.g., by running `fish -c` from
282 /// a function) we don't want that subshell to issue the same warnings.
maybe_issue_path_warning(const wcstring & which_dir,const wcstring & custom_error_msg,bool using_xdg,const wcstring & xdg_var,const wcstring & path,int saved_errno,env_stack_t & vars)283 static void maybe_issue_path_warning(const wcstring &which_dir, const wcstring &custom_error_msg,
284 bool using_xdg, const wcstring &xdg_var, const wcstring &path,
285 int saved_errno, env_stack_t &vars) {
286 wcstring warning_var_name = L"_FISH_WARNED_" + which_dir;
287 if (vars.get(warning_var_name, ENV_GLOBAL | ENV_EXPORT)) {
288 return;
289 }
290 vars.set_one(warning_var_name, ENV_GLOBAL | ENV_EXPORT, L"1");
291
292 FLOG(error, custom_error_msg.c_str());
293 if (path.empty()) {
294 FLOGF(warning_path, _(L"Unable to locate the %ls directory."), which_dir.c_str());
295 FLOGF(warning_path,
296 _(L"Please set the %ls or HOME environment variable before starting fish."),
297 xdg_var.c_str());
298 } else {
299 const wchar_t *env_var = using_xdg ? xdg_var.c_str() : L"HOME";
300 FLOGF(warning_path, _(L"Unable to locate %ls directory derived from $%ls: '%ls'."),
301 which_dir.c_str(), env_var, path.c_str());
302 FLOGF(warning_path, _(L"The error was '%s'."), std::strerror(saved_errno));
303 FLOGF(warning_path, _(L"Please set $%ls to a directory where you have write access."),
304 env_var);
305 }
306 ignore_result(write(STDERR_FILENO, "\n", 1));
307 }
308
309 /// Make sure the specified directory exists. If needed, try to create it and any currently not
310 /// existing parent directories, like mkdir -p,.
311 ///
312 /// \return 0 if, at the time of function return the directory exists, -1 otherwise.
create_directory(const wcstring & d)313 static int create_directory(const wcstring &d) {
314 bool ok = false;
315 struct stat buf;
316 int stat_res = 0;
317
318 while ((stat_res = wstat(d, &buf)) != 0) {
319 if (errno != EAGAIN) break;
320 }
321
322 if (stat_res == 0) {
323 if (S_ISDIR(buf.st_mode)) ok = true;
324 } else if (errno == ENOENT) {
325 wcstring dir = wdirname(d);
326 if (!create_directory(dir) && !wmkdir(d, 0700)) ok = true;
327 }
328
329 return ok ? 0 : -1;
330 }
331
332 /// The following type wraps up a user's "base" directories, corresponding (conceptually if not
333 /// actually) to XDG spec.
334 struct base_directory_t {
335 wcstring path{}; /// the path where we attempted to create the directory.
336 int err{0}; /// the error code if creating the directory failed, or 0 on success.
337 int is_remote{-1}; /// 1 if the directory is remote (e.g. NFS), 0 if local, -1 if unknown.
338 bool used_xdg{false}; /// whether an XDG variable was used in resolving the directory.
339
successbase_directory_t340 bool success() const { return err == 0; }
341 };
342
343 /// Attempt to get a base directory, creating it if necessary. If a variable named \p xdg_var is
344 /// set, use that directory; otherwise use the path \p non_xdg_homepath rooted in $HOME. \return the
345 /// result; see the base_directory_t fields.
make_base_directory(const wcstring & xdg_var,const wchar_t * non_xdg_homepath)346 static base_directory_t make_base_directory(const wcstring &xdg_var,
347 const wchar_t *non_xdg_homepath) {
348 // The vars we fetch must be exported. Allowing them to be universal doesn't make sense and
349 // allowing that creates a lock inversion that deadlocks the shell since we're called before
350 // uvars are available.
351 const auto &vars = env_stack_t::globals();
352 base_directory_t result{};
353 const auto xdg_dir = vars.get(xdg_var, ENV_GLOBAL | ENV_EXPORT);
354 if (!xdg_dir.missing_or_empty()) {
355 result.path = xdg_dir->as_string() + L"/fish";
356 result.used_xdg = true;
357 } else {
358 const auto home = vars.get(L"HOME", ENV_GLOBAL | ENV_EXPORT);
359 if (!home.missing_or_empty()) {
360 result.path = home->as_string() + non_xdg_homepath;
361 }
362 }
363
364 errno = 0;
365 if (result.path.empty()) {
366 result.err = ENOENT;
367 } else if (create_directory(result.path) < 0) {
368 result.err = errno;
369 } else {
370 result.err = 0;
371 // Need to append a trailing slash to check the contents of the directory, not its parent.
372 result.is_remote = path_is_remote(result.path + L'/');
373 }
374 return result;
375 }
376
get_data_directory()377 static const base_directory_t &get_data_directory() {
378 static base_directory_t s_dir = make_base_directory(L"XDG_DATA_HOME", L"/.local/share/fish");
379 return s_dir;
380 }
381
get_config_directory()382 static const base_directory_t &get_config_directory() {
383 static base_directory_t s_dir = make_base_directory(L"XDG_CONFIG_HOME", L"/.config/fish");
384 return s_dir;
385 }
386
path_emit_config_directory_messages(env_stack_t & vars)387 void path_emit_config_directory_messages(env_stack_t &vars) {
388 const auto &data = get_data_directory();
389 if (!data.success()) {
390 maybe_issue_path_warning(L"data", _(L"Your history will not be saved."), data.used_xdg,
391 L"XDG_DATA_HOME", data.path, data.err, vars);
392 }
393 if (data.is_remote > 0) {
394 FLOG(path, "data path appears to be on a network volume");
395 }
396
397 const auto &config = get_config_directory();
398 if (!config.success()) {
399 maybe_issue_path_warning(L"config", _(L"Your personal settings will not be saved."),
400 config.used_xdg, L"XDG_CONFIG_HOME", config.path, config.err,
401 vars);
402 }
403 if (config.is_remote > 0) {
404 FLOG(path, "config path appears to be on a network volume");
405 }
406 }
407
path_get_config(wcstring & path)408 bool path_get_config(wcstring &path) {
409 const auto &dir = get_config_directory();
410 path = dir.success() ? dir.path : L"";
411 return dir.success();
412 }
413
path_get_data(wcstring & path)414 bool path_get_data(wcstring &path) {
415 const auto &dir = get_data_directory();
416 path = dir.success() ? dir.path : L"";
417 return dir.success();
418 }
419
path_get_data_is_remote()420 int path_get_data_is_remote() { return get_data_directory().is_remote; }
421
path_get_config_is_remote()422 int path_get_config_is_remote() { return get_config_directory().is_remote; }
423
path_make_canonical(wcstring & path)424 void path_make_canonical(wcstring &path) {
425 // Ignore trailing slashes, unless it's the first character.
426 size_t len = path.size();
427 while (len > 1 && path.at(len - 1) == L'/') len--;
428
429 // Turn runs of slashes into a single slash.
430 size_t trailing = 0;
431 bool prev_was_slash = false;
432 for (size_t leading = 0; leading < len; leading++) {
433 wchar_t c = path.at(leading);
434 bool is_slash = (c == '/');
435 if (!prev_was_slash || !is_slash) {
436 // This is either the first slash in a run, or not a slash at all.
437 path.at(trailing++) = c;
438 }
439 prev_was_slash = is_slash;
440 }
441 assert(trailing <= len);
442 if (trailing < len) path.resize(trailing);
443 }
444
paths_are_equivalent(const wcstring & p1,const wcstring & p2)445 bool paths_are_equivalent(const wcstring &p1, const wcstring &p2) {
446 if (p1 == p2) return true;
447
448 size_t len1 = p1.size(), len2 = p2.size();
449
450 // Ignore trailing slashes after the first character.
451 while (len1 > 1 && p1.at(len1 - 1) == L'/') len1--;
452 while (len2 > 1 && p2.at(len2 - 1) == L'/') len2--;
453
454 // Start walking
455 size_t idx1 = 0, idx2 = 0;
456 while (idx1 < len1 && idx2 < len2) {
457 wchar_t c1 = p1.at(idx1), c2 = p2.at(idx2);
458
459 // If the characters are different, the strings are not equivalent.
460 if (c1 != c2) break;
461
462 idx1++;
463 idx2++;
464
465 // If the character was a slash, walk forwards until we hit the end of the string, or a
466 // non-slash. Note the first condition is invariant within the loop.
467 while (c1 == L'/' && idx1 < len1 && p1.at(idx1) == L'/') idx1++;
468 while (c2 == L'/' && idx2 < len2 && p2.at(idx2) == L'/') idx2++;
469 }
470
471 // We matched if we consumed all of the characters in both strings.
472 return idx1 == len1 && idx2 == len2;
473 }
474
path_is_valid(const wcstring & path,const wcstring & working_directory)475 bool path_is_valid(const wcstring &path, const wcstring &working_directory) {
476 bool path_is_valid;
477 // Some special paths are always valid.
478 if (path.empty()) {
479 path_is_valid = false;
480 } else if (path == L"." || path == L"./") {
481 path_is_valid = true;
482 } else if (path == L".." || path == L"../") {
483 path_is_valid = (!working_directory.empty() && working_directory != L"/");
484 } else if (path.at(0) != '/') {
485 // Prepend the working directory. Note that we know path is not empty here.
486 wcstring tmp = working_directory;
487 tmp.append(path);
488 path_is_valid = (0 == waccess(tmp, F_OK));
489 } else {
490 // Simple check.
491 path_is_valid = (0 == waccess(path, F_OK));
492 }
493 return path_is_valid;
494 }
495
paths_are_same_file(const wcstring & path1,const wcstring & path2)496 bool paths_are_same_file(const wcstring &path1, const wcstring &path2) {
497 if (paths_are_equivalent(path1, path2)) return true;
498
499 struct stat s1, s2;
500 if (wstat(path1, &s1) == 0 && wstat(path2, &s2) == 0) {
501 return s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
502 }
503
504 return false;
505 }
506
append_path_component(wcstring & path,const wcstring & component)507 void append_path_component(wcstring &path, const wcstring &component) {
508 if (path.empty() || component.empty()) {
509 path.append(component);
510 } else {
511 size_t path_len = path.size();
512 bool path_slash = path.at(path_len - 1) == L'/';
513 bool comp_slash = component.at(0) == L'/';
514 if (!path_slash && !comp_slash) {
515 // Need a slash
516 path.push_back(L'/');
517 } else if (path_slash && comp_slash) {
518 // Too many slashes.
519 path.erase(path_len - 1, 1);
520 }
521 path.append(component);
522 }
523 }
524