[ad28599] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Petr Koupy
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libposix
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
[4cf8ca6] | 32 | /** @file File control.
|
---|
[ad28599] | 33 | */
|
---|
| 34 |
|
---|
[9b1503e] | 35 | #include "internal/common.h"
|
---|
[a3da2b2] | 36 | #include "posix/fcntl.h"
|
---|
[ec18957a] | 37 |
|
---|
[4ec6820] | 38 | #include "libc/vfs/vfs.h"
|
---|
[0d0b319] | 39 |
|
---|
| 40 | #include <errno.h>
|
---|
[ad28599] | 41 |
|
---|
| 42 | /**
|
---|
[4ec6820] | 43 | * Performs set of operations on the opened files.
|
---|
| 44 | *
|
---|
| 45 | * @param fd File descriptor of the opened file.
|
---|
| 46 | * @param cmd Operation to carry out.
|
---|
| 47 | * @return Non-negative on success. Might have special meaning corresponding
|
---|
| 48 | * to the requested operation.
|
---|
[ad28599] | 49 | */
|
---|
[7f9df7b9] | 50 | int fcntl(int fd, int cmd, ...)
|
---|
[ad28599] | 51 | {
|
---|
[4ec6820] | 52 | int flags;
|
---|
| 53 |
|
---|
| 54 | switch (cmd) {
|
---|
| 55 | case F_DUPFD:
|
---|
[f00af83] | 56 | case F_DUPFD_CLOEXEC:
|
---|
[7c3fb9b] | 57 | /*
|
---|
| 58 | * VFS currently does not provide the functionality to duplicate
|
---|
| 59 | * opened file descriptor.
|
---|
| 60 | */
|
---|
[f00af83] | 61 | // FIXME: implement this once dup() is available
|
---|
| 62 | errno = ENOTSUP;
|
---|
| 63 | return -1;
|
---|
[4ec6820] | 64 | case F_GETFD:
|
---|
| 65 | /* FD_CLOEXEC is not supported. There are no other flags. */
|
---|
| 66 | return 0;
|
---|
| 67 | case F_SETFD:
|
---|
| 68 | /* FD_CLOEXEC is not supported. Ignore arguments and report success. */
|
---|
| 69 | return 0;
|
---|
| 70 | case F_GETFL:
|
---|
[7c3fb9b] | 71 | /*
|
---|
| 72 | * File status flags (i.e. O_APPEND) are currently private to the
|
---|
| 73 | * VFS server so it cannot be easily retrieved.
|
---|
| 74 | */
|
---|
[4ec6820] | 75 | flags = 0;
|
---|
[7c3fb9b] | 76 | /*
|
---|
| 77 | * File access flags are currently not supported for file descriptors.
|
---|
| 78 | * Provide full access.
|
---|
| 79 | */
|
---|
[4ec6820] | 80 | flags |= O_RDWR;
|
---|
| 81 | return flags;
|
---|
| 82 | case F_SETFL:
|
---|
[7c3fb9b] | 83 | /*
|
---|
| 84 | * File access flags are currently not supported for file descriptors.
|
---|
| 85 | * Ignore arguments and report success.
|
---|
| 86 | */
|
---|
[4ec6820] | 87 | return 0;
|
---|
| 88 | case F_GETOWN:
|
---|
| 89 | case F_SETOWN:
|
---|
| 90 | case F_GETLK:
|
---|
| 91 | case F_SETLK:
|
---|
| 92 | case F_SETLKW:
|
---|
| 93 | /* Signals (SIGURG) and file locks are not supported. */
|
---|
| 94 | errno = ENOTSUP;
|
---|
| 95 | return -1;
|
---|
| 96 | default:
|
---|
| 97 | /* Unknown command */
|
---|
| 98 | errno = EINVAL;
|
---|
| 99 | return -1;
|
---|
| 100 | }
|
---|
[ad28599] | 101 | }
|
---|
| 102 |
|
---|
[ca4c6ec1] | 103 | /**
|
---|
| 104 | * Open, possibly create, a file.
|
---|
| 105 | *
|
---|
| 106 | * @param pathname Path to the file.
|
---|
[b19e892] | 107 | * @param posix_flags Access mode flags.
|
---|
[ca4c6ec1] | 108 | */
|
---|
[7f9df7b9] | 109 | int open(const char *pathname, int posix_flags, ...)
|
---|
[ca4c6ec1] | 110 | {
|
---|
[7f9df7b9] | 111 | mode_t posix_mode = 0;
|
---|
[b19e892] | 112 | if (posix_flags & O_CREAT) {
|
---|
[ca4c6ec1] | 113 | va_list args;
|
---|
[b19e892] | 114 | va_start(args, posix_flags);
|
---|
[7f9df7b9] | 115 | posix_mode = va_arg(args, mode_t);
|
---|
[ca4c6ec1] | 116 | va_end(args);
|
---|
[b19e892] | 117 | (void) posix_mode;
|
---|
[ca4c6ec1] | 118 | }
|
---|
| 119 |
|
---|
[b19e892] | 120 | if (((posix_flags & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||
|
---|
| 121 | ((posix_flags & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||
|
---|
| 122 | ((posix_flags & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||
|
---|
| 123 | ((posix_flags & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {
|
---|
| 124 | errno = EINVAL;
|
---|
| 125 | return -1;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | int flags = WALK_REGULAR;
|
---|
| 129 | if (posix_flags & O_CREAT) {
|
---|
| 130 | if (posix_flags & O_EXCL)
|
---|
| 131 | flags |= WALK_MUST_CREATE;
|
---|
| 132 | else
|
---|
| 133 | flags |= WALK_MAY_CREATE;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | int mode =
|
---|
| 137 | ((posix_flags & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |
|
---|
| 138 | ((posix_flags & O_RDONLY) ? MODE_READ : 0) |
|
---|
| 139 | ((posix_flags & O_WRONLY) ? MODE_WRITE : 0) |
|
---|
| 140 | ((posix_flags & O_APPEND) ? MODE_APPEND : 0);
|
---|
| 141 |
|
---|
[f77c1c9] | 142 | int file;
|
---|
[0d0b319] | 143 |
|
---|
| 144 | if (failed(vfs_lookup(pathname, flags, &file)))
|
---|
[b19e892] | 145 | return -1;
|
---|
| 146 |
|
---|
[0d0b319] | 147 | if (failed(vfs_open(file, mode))) {
|
---|
[9c4cf0d] | 148 | vfs_put(file);
|
---|
[b19e892] | 149 | return -1;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | if (posix_flags & O_TRUNC) {
|
---|
| 153 | if (posix_flags & (O_RDWR | O_WRONLY)) {
|
---|
[0d0b319] | 154 | if (failed(vfs_resize(file, 0))) {
|
---|
[9c4cf0d] | 155 | vfs_put(file);
|
---|
[b19e892] | 156 | return -1;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | return file;
|
---|
[ca4c6ec1] | 162 | }
|
---|
| 163 |
|
---|
[ad28599] | 164 | /** @}
|
---|
| 165 | */
|
---|