[53900ab] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
[4f4b4e7] | 3 | * Copyright (c) 2011 Petr Koupy
|
---|
[53900ab] | 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 |
|
---|
[4f4b4e7] | 30 | /** @addtogroup libposix
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
[4cf8ca6] | 33 | /** @file File status handling.
|
---|
[4f4b4e7] | 34 | */
|
---|
| 35 |
|
---|
[9b1503e] | 36 | #include "../internal/common.h"
|
---|
[a3da2b2] | 37 | #include "posix/sys/stat.h"
|
---|
[23a0368] | 38 | #include "libc/vfs/vfs.h"
|
---|
[a6d908c1] | 39 |
|
---|
[0d0b319] | 40 | #include <errno.h>
|
---|
[a3da2b2] | 41 | #include "libc/mem.h"
|
---|
[53900ab] | 42 |
|
---|
[4f4b4e7] | 43 | /**
|
---|
[3daf2c31] | 44 | * Convert HelenOS stat struct into POSIX stat struct (if possible).
|
---|
[4f4b4e7] | 45 | *
|
---|
[3daf2c31] | 46 | * @param dest POSIX stat struct.
|
---|
| 47 | * @param src HelenOS stat struct.
|
---|
[59f388a] | 48 | *
|
---|
| 49 | * @return 0 on success, -1 on error.
|
---|
[53900ab] | 50 | */
|
---|
[7f9df7b9] | 51 | static int stat_to_posix(struct stat *dest, vfs_stat_t *src)
|
---|
[53900ab] | 52 | {
|
---|
[7f9df7b9] | 53 | memset(dest, 0, sizeof(struct stat));
|
---|
[a35b458] | 54 |
|
---|
[49160c4] | 55 | dest->st_dev = src->service;
|
---|
[eca52a8] | 56 | dest->st_ino = src->index;
|
---|
[a35b458] | 57 |
|
---|
[53900ab] | 58 | /* HelenOS doesn't support permissions, so we set them all */
|
---|
| 59 | dest->st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
|
---|
[4f4b4e7] | 60 | if (src->is_file) {
|
---|
[53900ab] | 61 | dest->st_mode |= S_IFREG;
|
---|
[4f4b4e7] | 62 | }
|
---|
| 63 | if (src->is_directory) {
|
---|
[53900ab] | 64 | dest->st_mode |= S_IFDIR;
|
---|
[4f4b4e7] | 65 | }
|
---|
[a35b458] | 66 |
|
---|
[53900ab] | 67 | dest->st_nlink = src->lnkcnt;
|
---|
| 68 | dest->st_size = src->size;
|
---|
[59f388a] | 69 |
|
---|
| 70 | if (src->size > INT64_MAX) {
|
---|
| 71 | errno = ERANGE;
|
---|
| 72 | return -1;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return 0;
|
---|
[53900ab] | 76 | }
|
---|
| 77 |
|
---|
[4f4b4e7] | 78 | /**
|
---|
[3daf2c31] | 79 | * Retrieve file status for file associated with file descriptor.
|
---|
[4f4b4e7] | 80 | *
|
---|
[3daf2c31] | 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.
|
---|
[4f4b4e7] | 84 | */
|
---|
[7f9df7b9] | 85 | int fstat(int fd, struct stat *st)
|
---|
[53900ab] | 86 | {
|
---|
[39330200] | 87 | vfs_stat_t hst;
|
---|
[0d0b319] | 88 | if (failed(vfs_stat(fd, &hst)))
|
---|
[23a0368] | 89 | return -1;
|
---|
[59f388a] | 90 | return stat_to_posix(st, &hst);
|
---|
[53900ab] | 91 | }
|
---|
| 92 |
|
---|
[39a7bfa] | 93 | /**
|
---|
[3daf2c31] | 94 | * Retrieve file status for symbolic link.
|
---|
[1b20da0] | 95 | *
|
---|
[3daf2c31] | 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.
|
---|
[39a7bfa] | 99 | */
|
---|
[7f9df7b9] | 100 | int lstat(const char *restrict path, struct stat *restrict st)
|
---|
[39a7bfa] | 101 | {
|
---|
[3daf2c31] | 102 | /* There are currently no symbolic links in HelenOS. */
|
---|
[7f9df7b9] | 103 | return stat(path, st);
|
---|
[39a7bfa] | 104 | }
|
---|
| 105 |
|
---|
[4f4b4e7] | 106 | /**
|
---|
[3daf2c31] | 107 | * Retrieve file status for regular file (or symbolic link target).
|
---|
[4f4b4e7] | 108 | *
|
---|
[3daf2c31] | 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.
|
---|
[4f4b4e7] | 112 | */
|
---|
[7f9df7b9] | 113 | int stat(const char *restrict path, struct stat *restrict st)
|
---|
[53900ab] | 114 | {
|
---|
[39330200] | 115 | vfs_stat_t hst;
|
---|
[0d0b319] | 116 | if (failed(vfs_stat_path(path, &hst)))
|
---|
[23a0368] | 117 | return -1;
|
---|
[59f388a] | 118 | return stat_to_posix(st, &hst);
|
---|
[53900ab] | 119 | }
|
---|
| 120 |
|
---|
[39a7bfa] | 121 | /**
|
---|
[3daf2c31] | 122 | * Change permission bits for the file if possible.
|
---|
[1b20da0] | 123 | *
|
---|
[3daf2c31] | 124 | * @param path Path to the file.
|
---|
| 125 | * @param mode Permission bits to be set.
|
---|
| 126 | * @return Zero on success, -1 otherwise.
|
---|
[39a7bfa] | 127 | */
|
---|
[7f9df7b9] | 128 | int chmod(const char *path, mode_t mode)
|
---|
[39a7bfa] | 129 | {
|
---|
[3daf2c31] | 130 | /* HelenOS doesn't support permissions, return success. */
|
---|
| 131 | return 0;
|
---|
[39a7bfa] | 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
[3daf2c31] | 135 | * Set the file mode creation mask of the process.
|
---|
[1b20da0] | 136 | *
|
---|
[3daf2c31] | 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.
|
---|
[39a7bfa] | 140 | */
|
---|
[7f9df7b9] | 141 | mode_t umask(mode_t mask)
|
---|
[39a7bfa] | 142 | {
|
---|
[3daf2c31] | 143 | /* HelenOS doesn't support permissions, return empty mask. */
|
---|
| 144 | return 0;
|
---|
[39a7bfa] | 145 | }
|
---|
| 146 |
|
---|
[6e5562a] | 147 | /**
|
---|
| 148 | * Create a directory.
|
---|
[1b20da0] | 149 | *
|
---|
[6e5562a] | 150 | * @param path Path to the new directory.
|
---|
| 151 | * @param mode Permission bits to be set.
|
---|
| 152 | * @return Zero on success, -1 otherwise.
|
---|
| 153 | */
|
---|
[7f9df7b9] | 154 | int mkdir(const char *path, mode_t mode)
|
---|
[6e5562a] | 155 | {
|
---|
[0d0b319] | 156 | if (failed(vfs_link_path(path, KIND_DIRECTORY, NULL)))
|
---|
[6e5562a] | 157 | return -1;
|
---|
| 158 | else
|
---|
| 159 | return 0;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[4f4b4e7] | 162 | /** @}
|
---|
| 163 | */
|
---|