Fix FreeBSD build.
[haiku.git] / src / tools / fs_shell / stat.cpp
blob8de69a675e6cdcefe9daff26e2249b0e3255eb6f
1 /*
2 * Copyright 2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "compatibility.h"
8 #include "fssh_stat.h"
10 #include <SupportDefs.h>
12 #include <sys/stat.h>
14 #include "fssh_errno.h"
15 #include "partition_support.h"
16 #include "stat_priv.h"
17 #include "stat_util.h"
20 using FSShell::from_platform_stat;
21 using FSShell::to_platform_mode;
24 #if (!defined(__BEOS__) && !defined(__HAIKU__))
25 // The _kern_read_stat() defined in libroot_build.so.
26 # define _kern_read_stat _kernbuild_read_stat
27 extern "C" status_t _kern_read_stat(int fd, const char *path,
28 bool traverseLink, struct stat *st, size_t statSize);
29 #endif
32 int
33 FSShell::unrestricted_stat(const char *path, struct fssh_stat *fsshStat)
35 struct stat st;
37 // Use the _kern_read_stat() defined in libroot on BeOS incompatible
38 // systems. Required for support for opening symlinks.
39 #if (defined(__BEOS__) || defined(__HAIKU__))
40 if (::stat(path, &st) < 0)
41 return -1;
42 #else
43 status_t error = _kern_read_stat(-1, path, true, &st, sizeof(st));
44 if (error < 0) {
45 fssh_set_errno(error);
46 return -1;
48 #endif
50 from_platform_stat(&st, fsshStat);
52 return 0;
56 int
57 FSShell::unrestricted_fstat(int fd, struct fssh_stat *fsshStat)
59 struct stat st;
61 // Use the _kern_read_stat() defined in libroot on BeOS incompatible
62 // systems. Required for support for opening symlinks.
63 #if (defined(__BEOS__) || defined(__HAIKU__))
64 if (fstat(fd, &st) < 0)
65 return -1;
66 #else
67 status_t error = _kern_read_stat(fd, NULL, false, &st, sizeof(st));
68 if (error < 0) {
69 fssh_set_errno(error);
70 return -1;
72 #endif
74 from_platform_stat(&st, fsshStat);
76 return 0;
80 int
81 FSShell::unrestricted_lstat(const char *path, struct fssh_stat *fsshStat)
83 struct stat st;
85 // Use the _kern_read_stat() defined in libroot on BeOS incompatible
86 // systems. Required for support for opening symlinks.
87 #if (defined(__BEOS__) || defined(__HAIKU__))
88 if (lstat(path, &st) < 0)
89 return -1;
90 #else
91 status_t error = _kern_read_stat(-1, path, false, &st, sizeof(st));
92 if (error < 0) {
93 fssh_set_errno(error);
94 return -1;
96 #endif
98 from_platform_stat(&st, fsshStat);
100 return 0;
103 // #pragma mark -
106 fssh_mkdir(const char *path, fssh_mode_t mode)
108 return mkdir(path, to_platform_mode(mode));
113 fssh_stat(const char *path, struct fssh_stat *fsshStat)
115 if (FSShell::unrestricted_stat(path, fsshStat) < 0)
116 return -1;
118 FSShell::restricted_file_restrict_stat(fsshStat);
120 return 0;
125 fssh_fstat(int fd, struct fssh_stat *fsshStat)
127 if (FSShell::unrestricted_fstat(fd, fsshStat) < 0)
128 return -1;
130 FSShell::restricted_file_restrict_stat(fsshStat);
132 return 0;
137 fssh_lstat(const char *path, struct fssh_stat *fsshStat)
139 if (FSShell::unrestricted_lstat(path, fsshStat) < 0)
140 return -1;
142 FSShell::restricted_file_restrict_stat(fsshStat);
144 return 0;