xref: /haiku/src/tools/fs_shell/stat.cpp (revision d9cebac2b77547b7064f22497514eecd2d047160)
1 /*
2  * Copyright 2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "compatibility.h"
7 
8 #include "fssh_stat.h"
9 
10 #include <SupportDefs.h>
11 
12 #include <sys/stat.h>
13 
14 #include "fssh_errno.h"
15 #include "stat_util.h"
16 
17 
18 using FSShell::from_platform_stat;
19 using FSShell::to_platform_mode;
20 
21 
22 #ifndef __BEOS__
23 	// The _kern_read_stat() defined in libroot_build.so.
24 	extern "C" status_t _kern_read_stat(int fd, const char *path,
25 		bool traverseLink, struct stat *st, size_t statSize);
26 #endif
27 
28 
29 int
30 fssh_mkdir(const char *path, fssh_mode_t mode)
31 {
32 	return mkdir(path, to_platform_mode(mode));
33 }
34 
35 
36 int
37 fssh_stat(const char *path, struct fssh_stat *fsshStat)
38 {
39 	struct stat st;
40 
41 	// Use the _kern_read_stat() defined in libroot on BeOS incompatible
42 	// systems. Required for support for opening symlinks.
43 #if __BEOS__
44 	if (stat(path, &st) < 0)
45 		return -1;
46 #else
47 	status_t error = _kern_read_stat(-1, path, true, &st, sizeof(st));
48 	if (error < 0) {
49 		fssh_set_errno(error);
50 		return -1;
51 	}
52 #endif
53 
54 	from_platform_stat(&st, fsshStat);
55 
56 	return 0;
57 }
58 
59 
60 int
61 fssh_fstat(int fd, struct fssh_stat *fsshStat)
62 {
63 	struct stat st;
64 
65 	// Use the _kern_read_stat() defined in libroot on BeOS incompatible
66 	// systems. Required for support for opening symlinks.
67 #if __BEOS__
68 	if (fstat(fd, &st) < 0)
69 		return -1;
70 #else
71 	status_t error = _kern_read_stat(fd, NULL, false, &st, sizeof(st));
72 	if (error < 0) {
73 		fssh_set_errno(error);
74 		return -1;
75 	}
76 #endif
77 
78 	from_platform_stat(&st, fsshStat);
79 
80 	return 0;
81 }
82 
83 
84 int
85 fssh_lstat(const char *path, struct fssh_stat *fsshStat)
86 {
87 	struct stat st;
88 
89 	// Use the _kern_read_stat() defined in libroot on BeOS incompatible
90 	// systems. Required for support for opening symlinks.
91 #if __BEOS__
92 	if (lstat(path, &st) < 0)
93 		return -1;
94 #else
95 	status_t error = _kern_read_stat(-1, path, false, &st, sizeof(st));
96 	if (error < 0) {
97 		fssh_set_errno(error);
98 		return -1;
99 	}
100 #endif
101 
102 	from_platform_stat(&st, fsshStat);
103 
104 	return 0;
105 }
106