1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
3 | * Copyright (c) 2011 Petr Koupy
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libposix
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file File status handling.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include "../internal/common.h"
|
---|
37 | #include <sys/stat.h>
|
---|
38 | #include <vfs/vfs.h>
|
---|
39 |
|
---|
40 | #include <errno.h>
|
---|
41 | #include <mem.h>
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Convert HelenOS stat struct into POSIX stat struct (if possible).
|
---|
45 | *
|
---|
46 | * @param dest POSIX stat struct.
|
---|
47 | * @param src HelenOS stat struct.
|
---|
48 | *
|
---|
49 | * @return 0 on success, -1 on error.
|
---|
50 | */
|
---|
51 | static int stat_to_posix(struct stat *dest, vfs_stat_t *src)
|
---|
52 | {
|
---|
53 | memset(dest, 0, sizeof(struct stat));
|
---|
54 |
|
---|
55 | dest->st_dev = src->service;
|
---|
56 | dest->st_ino = src->index;
|
---|
57 |
|
---|
58 | /* HelenOS doesn't support permissions, so we set them all */
|
---|
59 | dest->st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
|
---|
60 | if (src->is_file) {
|
---|
61 | dest->st_mode |= S_IFREG;
|
---|
62 | }
|
---|
63 | if (src->is_directory) {
|
---|
64 | dest->st_mode |= S_IFDIR;
|
---|
65 | }
|
---|
66 |
|
---|
67 | dest->st_nlink = src->lnkcnt;
|
---|
68 | dest->st_size = src->size;
|
---|
69 |
|
---|
70 | if (src->size > INT64_MAX) {
|
---|
71 | errno = ERANGE;
|
---|
72 | return -1;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Retrieve file status for file associated with file descriptor.
|
---|
80 | *
|
---|
81 | * @param fd File descriptor of the opened file.
|
---|
82 | * @param st Status structure to be filled with information.
|
---|
83 | * @return Zero on success, -1 otherwise.
|
---|
84 | */
|
---|
85 | int fstat(int fd, struct stat *st)
|
---|
86 | {
|
---|
87 | vfs_stat_t hst;
|
---|
88 | if (failed(vfs_stat(fd, &hst)))
|
---|
89 | return -1;
|
---|
90 | return stat_to_posix(st, &hst);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Retrieve file status for symbolic link.
|
---|
95 | *
|
---|
96 | * @param path Path to the symbolic link.
|
---|
97 | * @param st Status structure to be filled with information.
|
---|
98 | * @return Zero on success, -1 otherwise.
|
---|
99 | */
|
---|
100 | int lstat(const char *restrict path, struct stat *restrict st)
|
---|
101 | {
|
---|
102 | /* There are currently no symbolic links in HelenOS. */
|
---|
103 | return stat(path, st);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Retrieve file status for regular file (or symbolic link target).
|
---|
108 | *
|
---|
109 | * @param path Path to the file/link.
|
---|
110 | * @param st Status structure to be filled with information.
|
---|
111 | * @return Zero on success, -1 otherwise.
|
---|
112 | */
|
---|
113 | int stat(const char *restrict path, struct stat *restrict st)
|
---|
114 | {
|
---|
115 | vfs_stat_t hst;
|
---|
116 | if (failed(vfs_stat_path(path, &hst)))
|
---|
117 | return -1;
|
---|
118 | return stat_to_posix(st, &hst);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Change permission bits for the file if possible.
|
---|
123 | *
|
---|
124 | * @param path Path to the file.
|
---|
125 | * @param mode Permission bits to be set.
|
---|
126 | * @return Zero on success, -1 otherwise.
|
---|
127 | */
|
---|
128 | int chmod(const char *path, mode_t mode)
|
---|
129 | {
|
---|
130 | /* HelenOS doesn't support permissions, return success. */
|
---|
131 | return 0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Set the file mode creation mask of the process.
|
---|
136 | *
|
---|
137 | * @param mask Set permission bits are cleared in the related creation
|
---|
138 | * functions. Non-permission bits are ignored.
|
---|
139 | * @return Previous file mode creation mask.
|
---|
140 | */
|
---|
141 | mode_t umask(mode_t mask)
|
---|
142 | {
|
---|
143 | /* HelenOS doesn't support permissions, return empty mask. */
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Create a directory.
|
---|
149 | *
|
---|
150 | * @param path Path to the new directory.
|
---|
151 | * @param mode Permission bits to be set.
|
---|
152 | * @return Zero on success, -1 otherwise.
|
---|
153 | */
|
---|
154 | int mkdir(const char *path, mode_t mode)
|
---|
155 | {
|
---|
156 | if (failed(vfs_link_path(path, KIND_DIRECTORY, NULL)))
|
---|
157 | return -1;
|
---|
158 | else
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** @}
|
---|
163 | */
|
---|