| 1 | /* $Id: shfile.c 3451 2020-09-13 11:21:43Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * File management.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kBuild.
|
|---|
| 10 | *
|
|---|
| 11 | * kBuild is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kBuild is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kBuild; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | /*******************************************************************************
|
|---|
| 28 | * Header Files *
|
|---|
| 29 | *******************************************************************************/
|
|---|
| 30 | #include "shfile.h"
|
|---|
| 31 | #include "shinstance.h" /* TRACE2 */
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <string.h>
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 |
|
|---|
| 37 | #if K_OS == K_OS_WINDOWS
|
|---|
| 38 | # include <limits.h>
|
|---|
| 39 | # ifndef PIPE_BUF
|
|---|
| 40 | # define PIPE_BUF 512
|
|---|
| 41 | # endif
|
|---|
| 42 | # include <ntstatus.h>
|
|---|
| 43 | # define WIN32_NO_STATUS
|
|---|
| 44 | # include <Windows.h>
|
|---|
| 45 | # if !defined(_WIN32_WINNT)
|
|---|
| 46 | # define _WIN32_WINNT 0x0502 /* Windows Server 2003 */
|
|---|
| 47 | # endif
|
|---|
| 48 | # include <winternl.h> //NTSTATUS
|
|---|
| 49 | #else
|
|---|
| 50 | # include <unistd.h>
|
|---|
| 51 | # include <fcntl.h>
|
|---|
| 52 | # include <dirent.h>
|
|---|
| 53 | #endif
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | /*******************************************************************************
|
|---|
| 57 | * Defined Constants And Macros *
|
|---|
| 58 | *******************************************************************************/
|
|---|
| 59 | /** @def SHFILE_IN_USE
|
|---|
| 60 | * Whether the file descriptor table stuff is actually in use or not.
|
|---|
| 61 | */
|
|---|
| 62 | #if K_OS == K_OS_WINDOWS \
|
|---|
| 63 | || K_OS == K_OS_OPENBSD /* because of ugly pthread library pipe hacks */ \
|
|---|
| 64 | || !defined(SH_FORKED_MODE)
|
|---|
| 65 | # define SHFILE_IN_USE
|
|---|
| 66 | #endif
|
|---|
| 67 | /** The max file table size. */
|
|---|
| 68 | #define SHFILE_MAX 1024
|
|---|
| 69 | /** The file table growth rate. */
|
|---|
| 70 | #define SHFILE_GROW 64
|
|---|
| 71 | /** The min native unix file descriptor. */
|
|---|
| 72 | #define SHFILE_UNIX_MIN_FD 32
|
|---|
| 73 | /** The path buffer size we use. */
|
|---|
| 74 | #define SHFILE_MAX_PATH 4096
|
|---|
| 75 |
|
|---|
| 76 | /** Set errno and return. Doing a trace in debug build. */
|
|---|
| 77 | #define RETURN_ERROR(rc, err, msg) \
|
|---|
| 78 | do { \
|
|---|
| 79 | TRACE2((NULL, "%s: " ## msg ## " - returning %d / %d\n", __FUNCTION__, (rc), (err))); \
|
|---|
| 80 | errno = (err); \
|
|---|
| 81 | return (rc); \
|
|---|
| 82 | } while (0)
|
|---|
| 83 |
|
|---|
| 84 | #if K_OS == K_OS_WINDOWS
|
|---|
| 85 | /* See msdos.h for description. */
|
|---|
| 86 | # define FOPEN 0x01
|
|---|
| 87 | # define FEOFLAG 0x02
|
|---|
| 88 | # define FCRLF 0x04
|
|---|
| 89 | # define FPIPE 0x08
|
|---|
| 90 | # define FNOINHERIT 0x10
|
|---|
| 91 | # define FAPPEND 0x20
|
|---|
| 92 | # define FDEV 0x40
|
|---|
| 93 | # define FTEXT 0x80
|
|---|
| 94 |
|
|---|
| 95 | # define MY_ObjectBasicInformation 0
|
|---|
| 96 | # define MY_FileNamesInformation 12
|
|---|
| 97 |
|
|---|
| 98 | typedef struct
|
|---|
| 99 | {
|
|---|
| 100 | ULONG Attributes;
|
|---|
| 101 | ACCESS_MASK GrantedAccess;
|
|---|
| 102 | ULONG HandleCount;
|
|---|
| 103 | ULONG PointerCount;
|
|---|
| 104 | ULONG PagedPoolUsage;
|
|---|
| 105 | ULONG NonPagedPoolUsage;
|
|---|
| 106 | ULONG Reserved[3];
|
|---|
| 107 | ULONG NameInformationLength;
|
|---|
| 108 | ULONG TypeInformationLength;
|
|---|
| 109 | ULONG SecurityDescriptorLength;
|
|---|
| 110 | LARGE_INTEGER CreateTime;
|
|---|
| 111 | } MY_OBJECT_BASIC_INFORMATION;
|
|---|
| 112 |
|
|---|
| 113 | #if 0
|
|---|
| 114 | typedef struct
|
|---|
| 115 | {
|
|---|
| 116 | union
|
|---|
| 117 | {
|
|---|
| 118 | LONG Status;
|
|---|
| 119 | PVOID Pointer;
|
|---|
| 120 | };
|
|---|
| 121 | ULONG_PTR Information;
|
|---|
| 122 | } MY_IO_STATUS_BLOCK;
|
|---|
| 123 | #else
|
|---|
| 124 | typedef IO_STATUS_BLOCK MY_IO_STATUS_BLOCK;
|
|---|
| 125 | #endif
|
|---|
| 126 | typedef MY_IO_STATUS_BLOCK *PMY_IO_STATUS_BLOCK;
|
|---|
| 127 |
|
|---|
| 128 | typedef struct
|
|---|
| 129 | {
|
|---|
| 130 | ULONG NextEntryOffset;
|
|---|
| 131 | ULONG FileIndex;
|
|---|
| 132 | ULONG FileNameLength;
|
|---|
| 133 | WCHAR FileName[1];
|
|---|
| 134 | } MY_FILE_NAMES_INFORMATION, *PMY_FILE_NAMES_INFORMATION;
|
|---|
| 135 |
|
|---|
| 136 | typedef NTSTATUS (NTAPI * PFN_NtQueryObject)(HANDLE, int, void *, size_t, size_t *);
|
|---|
| 137 | typedef NTSTATUS (NTAPI * PFN_NtQueryDirectoryFile)(HANDLE, HANDLE, void *, void *, PMY_IO_STATUS_BLOCK, void *,
|
|---|
| 138 | ULONG, int, int, PUNICODE_STRING, int);
|
|---|
| 139 | typedef NTSTATUS (NTAPI * PFN_RtlUnicodeStringToAnsiString)(PANSI_STRING, PCUNICODE_STRING, int);
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | #endif /* K_OS_WINDOWS */
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 | /*******************************************************************************
|
|---|
| 146 | * Global Variables *
|
|---|
| 147 | *******************************************************************************/
|
|---|
| 148 | #if K_OS == K_OS_WINDOWS
|
|---|
| 149 | static int g_shfile_globals_initialized = 0;
|
|---|
| 150 | static PFN_NtQueryObject g_pfnNtQueryObject = NULL;
|
|---|
| 151 | static PFN_NtQueryDirectoryFile g_pfnNtQueryDirectoryFile = NULL;
|
|---|
| 152 | static PFN_RtlUnicodeStringToAnsiString g_pfnRtlUnicodeStringToAnsiString = NULL;
|
|---|
| 153 | #endif /* K_OS_WINDOWS */
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | #ifdef SHFILE_IN_USE
|
|---|
| 157 |
|
|---|
| 158 | /**
|
|---|
| 159 | * Close the specified native handle.
|
|---|
| 160 | *
|
|---|
| 161 | * @param native The native file handle.
|
|---|
| 162 | * @param flags The flags in case they might come in handy later.
|
|---|
| 163 | */
|
|---|
| 164 | static void shfile_native_close(intptr_t native, unsigned flags)
|
|---|
| 165 | {
|
|---|
| 166 | # if K_OS == K_OS_WINDOWS
|
|---|
| 167 | BOOL fRc = CloseHandle((HANDLE)native);
|
|---|
| 168 | assert(fRc); (void)fRc;
|
|---|
| 169 | # else
|
|---|
| 170 | int s = errno;
|
|---|
| 171 | close(native);
|
|---|
| 172 | errno = s;
|
|---|
| 173 | # endif
|
|---|
| 174 | (void)flags;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Grows the descriptor table, making sure that it can hold @a fdMin,
|
|---|
| 179 | *
|
|---|
| 180 | * @returns The max(fdMin, fdFirstNew) on success, -1 on failure.
|
|---|
| 181 | * @param pfdtab The table to grow.
|
|---|
| 182 | * @param fdMin Grow to include this index.
|
|---|
| 183 | */
|
|---|
| 184 | static int shfile_grow_tab_locked(shfdtab *pfdtab, int fdMin)
|
|---|
| 185 | {
|
|---|
| 186 | /*
|
|---|
| 187 | * Grow the descriptor table.
|
|---|
| 188 | */
|
|---|
| 189 | int fdRet = -1;
|
|---|
| 190 | shfile *new_tab;
|
|---|
| 191 | int new_size = pfdtab->size + SHFILE_GROW;
|
|---|
| 192 | while (new_size < fdMin)
|
|---|
| 193 | new_size += SHFILE_GROW;
|
|---|
| 194 | new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile));
|
|---|
| 195 | if (new_tab)
|
|---|
| 196 | {
|
|---|
| 197 | int i;
|
|---|
| 198 | for (i = pfdtab->size; i < new_size; i++)
|
|---|
| 199 | {
|
|---|
| 200 | new_tab[i].fd = -1;
|
|---|
| 201 | new_tab[i].oflags = 0;
|
|---|
| 202 | new_tab[i].shflags = 0;
|
|---|
| 203 | new_tab[i].native = -1;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | fdRet = pfdtab->size;
|
|---|
| 207 | if (fdRet < fdMin)
|
|---|
| 208 | fdRet = fdMin;
|
|---|
| 209 |
|
|---|
| 210 | pfdtab->tab = new_tab;
|
|---|
| 211 | pfdtab->size = new_size;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | return fdRet;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Inserts the file into the descriptor table.
|
|---|
| 219 | *
|
|---|
| 220 | * If we're out of memory and cannot extend the table, we'll close the
|
|---|
| 221 | * file, set errno to EMFILE and return -1.
|
|---|
| 222 | *
|
|---|
| 223 | * @returns The file descriptor number. -1 and errno on failure.
|
|---|
| 224 | * @param pfdtab The file descriptor table.
|
|---|
| 225 | * @param native The native file handle.
|
|---|
| 226 | * @param oflags The flags the it was opened/created with.
|
|---|
| 227 | * @param shflags The shell file flags.
|
|---|
| 228 | * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
|
|---|
| 229 | * @param who Who we're doing this for (for logging purposes).
|
|---|
| 230 | */
|
|---|
| 231 | static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned oflags, unsigned shflags, int fdMin, const char *who)
|
|---|
| 232 | {
|
|---|
| 233 | shmtxtmp tmp;
|
|---|
| 234 | int fd;
|
|---|
| 235 | int i;
|
|---|
| 236 |
|
|---|
| 237 | /*
|
|---|
| 238 | * Fend of bad stuff.
|
|---|
| 239 | */
|
|---|
| 240 | if (fdMin >= SHFILE_MAX)
|
|---|
| 241 | {
|
|---|
| 242 | TRACE2((NULL, "shfile_insert: fdMin=%d is out of bounds; native=%p\n", fdMin, native));
|
|---|
| 243 | shfile_native_close(native, oflags);
|
|---|
| 244 | errno = EMFILE;
|
|---|
| 245 | return -1;
|
|---|
| 246 | }
|
|---|
| 247 | # if K_OS != K_OS_WINDOWS
|
|---|
| 248 | if (fcntl((int)native, F_SETFD, fcntl((int)native, F_GETFD, 0) | FD_CLOEXEC) == -1)
|
|---|
| 249 | {
|
|---|
| 250 | int e = errno;
|
|---|
| 251 | TRACE2((NULL, "shfile_insert: F_SETFD failed %d; native=%p\n", e, native));
|
|---|
| 252 | close((int)native);
|
|---|
| 253 | errno = e;
|
|---|
| 254 | return -1;
|
|---|
| 255 | }
|
|---|
| 256 | # endif
|
|---|
| 257 |
|
|---|
| 258 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 259 |
|
|---|
| 260 | /*
|
|---|
| 261 | * Search for a fitting unused location.
|
|---|
| 262 | */
|
|---|
| 263 | fd = -1;
|
|---|
| 264 | for (i = fdMin >= 0 ? fdMin : 0; (unsigned)i < pfdtab->size; i++)
|
|---|
| 265 | if (pfdtab->tab[i].fd == -1)
|
|---|
| 266 | {
|
|---|
| 267 | fd = i;
|
|---|
| 268 | break;
|
|---|
| 269 | }
|
|---|
| 270 | if (fd == -1)
|
|---|
| 271 | fd = shfile_grow_tab_locked(pfdtab, fdMin);
|
|---|
| 272 |
|
|---|
| 273 | /*
|
|---|
| 274 | * Fill in the entry if we've found one.
|
|---|
| 275 | */
|
|---|
| 276 | if (fd != -1)
|
|---|
| 277 | {
|
|---|
| 278 | pfdtab->tab[fd].fd = fd;
|
|---|
| 279 | pfdtab->tab[fd].oflags = oflags;
|
|---|
| 280 | pfdtab->tab[fd].shflags = shflags;
|
|---|
| 281 | pfdtab->tab[fd].native = native;
|
|---|
| 282 | TRACE2((NULL, "shfile_insert: #%d: native=%p oflags=%#x shflags=%#x\n", fd, native, oflags, shflags));
|
|---|
| 283 | }
|
|---|
| 284 | else
|
|---|
| 285 | shfile_native_close(native, oflags);
|
|---|
| 286 |
|
|---|
| 287 | shmtx_leave(&pfdtab->mtx, &tmp);
|
|---|
| 288 |
|
|---|
| 289 | if (fd == -1)
|
|---|
| 290 | errno = EMFILE;
|
|---|
| 291 | (void)who;
|
|---|
| 292 | return fd;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | # if K_OS != K_OS_WINDOWS
|
|---|
| 296 | /**
|
|---|
| 297 | * Makes a copy of the native file, closes the original, and inserts the copy
|
|---|
| 298 | * into the descriptor table.
|
|---|
| 299 | *
|
|---|
| 300 | * If we're out of memory and cannot extend the table, we'll close the
|
|---|
| 301 | * file, set errno to EMFILE and return -1.
|
|---|
| 302 | *
|
|---|
| 303 | * @returns The file descriptor number. -1 and errno on failure.
|
|---|
| 304 | * @param pfdtab The file descriptor table.
|
|---|
| 305 | * @param pnative The native file handle on input, -1 on output.
|
|---|
| 306 | * @param oflags The flags the it was opened/created with.
|
|---|
| 307 | * @param shflags The shell file flags.
|
|---|
| 308 | * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
|
|---|
| 309 | * @param who Who we're doing this for (for logging purposes).
|
|---|
| 310 | */
|
|---|
| 311 | static int shfile_copy_insert_and_close(shfdtab *pfdtab, int *pnative, unsigned oflags, unsigned shflags, int fdMin, const char *who)
|
|---|
| 312 | {
|
|---|
| 313 | int fd = -1;
|
|---|
| 314 | int s = errno;
|
|---|
| 315 | int native_copy = fcntl(*pnative, F_DUPFD, SHFILE_UNIX_MIN_FD);
|
|---|
| 316 | close(*pnative);
|
|---|
| 317 | *pnative = -1;
|
|---|
| 318 | errno = s;
|
|---|
| 319 |
|
|---|
| 320 | if (native_copy != -1)
|
|---|
| 321 | fd = shfile_insert(pfdtab, native_copy, oflags, shflags, fdMin, who);
|
|---|
| 322 | return fd;
|
|---|
| 323 | }
|
|---|
| 324 | # endif /* !K_OS_WINDOWS */
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * Gets a file descriptor and lock the file descriptor table.
|
|---|
| 328 | *
|
|---|
| 329 | * @returns Pointer to the file and table ownership on success,
|
|---|
| 330 | * NULL and errno set to EBADF on failure.
|
|---|
| 331 | * @param pfdtab The file descriptor table.
|
|---|
| 332 | * @param fd The file descriptor number.
|
|---|
| 333 | * @param ptmp See shmtx_enter.
|
|---|
| 334 | */
|
|---|
| 335 | static shfile *shfile_get(shfdtab *pfdtab, int fd, shmtxtmp *ptmp)
|
|---|
| 336 | {
|
|---|
| 337 | shfile *file = NULL;
|
|---|
| 338 | if ( fd >= 0
|
|---|
| 339 | && (unsigned)fd < pfdtab->size)
|
|---|
| 340 | {
|
|---|
| 341 | shmtx_enter(&pfdtab->mtx, ptmp);
|
|---|
| 342 | if ((unsigned)fd < pfdtab->size
|
|---|
| 343 | && pfdtab->tab[fd].fd != -1)
|
|---|
| 344 | file = &pfdtab->tab[fd];
|
|---|
| 345 | else
|
|---|
| 346 | shmtx_leave(&pfdtab->mtx, ptmp);
|
|---|
| 347 | }
|
|---|
| 348 | if (!file)
|
|---|
| 349 | errno = EBADF;
|
|---|
| 350 | return file;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /**
|
|---|
| 354 | * Puts back a file descriptor and releases the table ownership.
|
|---|
| 355 | *
|
|---|
| 356 | * @param pfdtab The file descriptor table.
|
|---|
| 357 | * @param file The file.
|
|---|
| 358 | * @param ptmp See shmtx_leave.
|
|---|
| 359 | */
|
|---|
| 360 | static void shfile_put(shfdtab *pfdtab, shfile *file, shmtxtmp *ptmp)
|
|---|
| 361 | {
|
|---|
| 362 | shmtx_leave(&pfdtab->mtx, ptmp);
|
|---|
| 363 | assert(file);
|
|---|
| 364 | (void)file;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /**
|
|---|
| 368 | * Constructs a path from the current directory and the passed in path.
|
|---|
| 369 | *
|
|---|
| 370 | * @returns 0 on success, -1 and errno set to ENAMETOOLONG or EINVAL on failure.
|
|---|
| 371 | *
|
|---|
| 372 | * @param pfdtab The file descriptor table
|
|---|
| 373 | * @param path The path the caller supplied.
|
|---|
| 374 | * @param buf Where to put the path. This is assumed to be SHFILE_MAX_PATH
|
|---|
| 375 | * chars in size.
|
|---|
| 376 | */
|
|---|
| 377 | int shfile_make_path(shfdtab *pfdtab, const char *path, char *buf)
|
|---|
| 378 | {
|
|---|
| 379 | size_t path_len = strlen(path);
|
|---|
| 380 | if (path_len == 0)
|
|---|
| 381 | {
|
|---|
| 382 | errno = EINVAL;
|
|---|
| 383 | return -1;
|
|---|
| 384 | }
|
|---|
| 385 | if (path_len >= SHFILE_MAX_PATH)
|
|---|
| 386 | {
|
|---|
| 387 | errno = ENAMETOOLONG;
|
|---|
| 388 | return -1;
|
|---|
| 389 | }
|
|---|
| 390 | if ( *path == '/'
|
|---|
| 391 | # if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
|
|---|
| 392 | || *path == '\\'
|
|---|
| 393 | || ( *path
|
|---|
| 394 | && path[1] == ':'
|
|---|
| 395 | && ( (*path >= 'A' && *path <= 'Z')
|
|---|
| 396 | || (*path >= 'a' && *path <= 'z')))
|
|---|
| 397 | # endif
|
|---|
| 398 | )
|
|---|
| 399 | {
|
|---|
| 400 | memcpy(buf, path, path_len + 1);
|
|---|
| 401 | }
|
|---|
| 402 | else
|
|---|
| 403 | {
|
|---|
| 404 | size_t cwd_len;
|
|---|
| 405 | shmtxtmp tmp;
|
|---|
| 406 |
|
|---|
| 407 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 408 |
|
|---|
| 409 | cwd_len = strlen(pfdtab->cwd);
|
|---|
| 410 | memcpy(buf, pfdtab->cwd, cwd_len);
|
|---|
| 411 |
|
|---|
| 412 | shmtx_leave(&pfdtab->mtx, &tmp);
|
|---|
| 413 |
|
|---|
| 414 | if (cwd_len + path_len + 1 >= SHFILE_MAX_PATH)
|
|---|
| 415 | {
|
|---|
| 416 | errno = ENAMETOOLONG;
|
|---|
| 417 | return -1;
|
|---|
| 418 | }
|
|---|
| 419 | if ( !cwd_len
|
|---|
| 420 | || buf[cwd_len - 1] != '/')
|
|---|
| 421 | buf[cwd_len++] = '/';
|
|---|
| 422 | memcpy(buf + cwd_len, path, path_len + 1);
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | # if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
|
|---|
| 426 | if (!strcmp(buf, "/dev/null"))
|
|---|
| 427 | strcpy(buf, "NUL");
|
|---|
| 428 | # endif
|
|---|
| 429 | return 0;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | # if K_OS == K_OS_WINDOWS
|
|---|
| 433 |
|
|---|
| 434 | /**
|
|---|
| 435 | * Adjusts the file name if it ends with a trailing directory slash.
|
|---|
| 436 | *
|
|---|
| 437 | * Windows APIs doesn't like trailing slashes.
|
|---|
| 438 | *
|
|---|
| 439 | * @returns 1 if it has a directory slash, 0 if not.
|
|---|
| 440 | *
|
|---|
| 441 | * @param abspath The path to adjust (SHFILE_MAX_PATH).
|
|---|
| 442 | */
|
|---|
| 443 | static int shfile_trailing_slash_hack(char *abspath)
|
|---|
| 444 | {
|
|---|
| 445 | /*
|
|---|
| 446 | * Anything worth adjust here?
|
|---|
| 447 | */
|
|---|
| 448 | size_t path_len = strlen(abspath);
|
|---|
| 449 | if ( path_len == 0
|
|---|
| 450 | || ( abspath[path_len - 1] != '/'
|
|---|
| 451 | # if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
|
|---|
| 452 | && abspath[path_len - 1] != '\\'
|
|---|
| 453 | # endif
|
|---|
| 454 | )
|
|---|
| 455 | )
|
|---|
| 456 | return 0;
|
|---|
| 457 |
|
|---|
| 458 | /*
|
|---|
| 459 | * Ok, make the adjustment.
|
|---|
| 460 | */
|
|---|
| 461 | if (path_len + 2 <= SHFILE_MAX_PATH)
|
|---|
| 462 | {
|
|---|
| 463 | /* Add a '.' to the end. */
|
|---|
| 464 | abspath[path_len++] = '.';
|
|---|
| 465 | abspath[path_len] = '\0';
|
|---|
| 466 | }
|
|---|
| 467 | else
|
|---|
| 468 | {
|
|---|
| 469 | /* No space for a dot, remove the slash if it's alone or just remove
|
|---|
| 470 | one and add a dot like above. */
|
|---|
| 471 | if ( abspath[path_len - 2] != '/'
|
|---|
| 472 | # if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
|
|---|
| 473 | && abspath[path_len - 2] != '\\'
|
|---|
| 474 | # endif
|
|---|
| 475 | )
|
|---|
| 476 | abspath[--path_len] = '\0';
|
|---|
| 477 | else
|
|---|
| 478 | abspath[path_len - 1] = '.';
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | return 1;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 | /**
|
|---|
| 486 | * Converts a DOS(/Windows) error code to errno,
|
|---|
| 487 | * assigning it to errno.
|
|---|
| 488 | *
|
|---|
| 489 | * @returns -1
|
|---|
| 490 | * @param err The DOS error.
|
|---|
| 491 | */
|
|---|
| 492 | static int shfile_dos2errno(int err)
|
|---|
| 493 | {
|
|---|
| 494 | switch (err)
|
|---|
| 495 | {
|
|---|
| 496 | case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
|
|---|
| 497 | case ERROR_ACCESS_DENIED: errno = EACCES; break;
|
|---|
| 498 | case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
|
|---|
| 499 | case ERROR_LOCK_VIOLATION: errno = EACCES; break;
|
|---|
| 500 | case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
|
|---|
| 501 | case ERROR_CANNOT_MAKE: errno = EACCES; break;
|
|---|
| 502 | case ERROR_FAIL_I24: errno = EACCES; break;
|
|---|
| 503 | case ERROR_DRIVE_LOCKED: errno = EACCES; break;
|
|---|
| 504 | case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
|
|---|
| 505 | case ERROR_NOT_LOCKED: errno = EACCES; break;
|
|---|
| 506 | case ERROR_LOCK_FAILED: errno = EACCES; break;
|
|---|
| 507 | case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
|
|---|
| 508 | case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
|
|---|
| 509 | case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
|
|---|
| 510 | case ERROR_INVALID_HANDLE: errno = EBADF; break;
|
|---|
| 511 | case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
|
|---|
| 512 | case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
|
|---|
| 513 | case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
|
|---|
| 514 | case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
|
|---|
| 515 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
|---|
| 516 | case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
|
|---|
| 517 | case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
|
|---|
| 518 | case ERROR_INVALID_ACCESS: errno = EINVAL; break;
|
|---|
| 519 | case ERROR_INVALID_DATA: errno = EINVAL; break;
|
|---|
| 520 | case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
|
|---|
| 521 | case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
|
|---|
| 522 | case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
|
|---|
| 523 | case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
|
|---|
| 524 | case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
|
|---|
| 525 | case ERROR_INVALID_DRIVE: errno = ENOENT; break;
|
|---|
| 526 | case ERROR_NO_MORE_FILES: errno = ENOENT; break;
|
|---|
| 527 | case ERROR_BAD_NETPATH: errno = ENOENT; break;
|
|---|
| 528 | case ERROR_BAD_NET_NAME: errno = ENOENT; break;
|
|---|
| 529 | case ERROR_BAD_PATHNAME: errno = ENOENT; break;
|
|---|
| 530 | case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
|
|---|
| 531 | case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
|
|---|
| 532 | case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
|
|---|
| 533 | case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
|
|---|
| 534 | case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
|
|---|
| 535 | case ERROR_NOT_ENOUGH_QUOTA: errno = ENOMEM; break;
|
|---|
| 536 | case ERROR_DISK_FULL: errno = ENOSPC; break;
|
|---|
| 537 | case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
|
|---|
| 538 | case ERROR_BROKEN_PIPE: errno = EPIPE; break;
|
|---|
| 539 | case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
|
|---|
| 540 | default: errno = EINVAL; break;
|
|---|
| 541 | }
|
|---|
| 542 | return -1;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | /**
|
|---|
| 546 | * Converts an NT status code to errno,
|
|---|
| 547 | * assigning it to errno.
|
|---|
| 548 | *
|
|---|
| 549 | * @returns -1
|
|---|
| 550 | * @param rcNt The NT status code.
|
|---|
| 551 | */
|
|---|
| 552 | static int shfile_nt2errno(NTSTATUS rcNt)
|
|---|
| 553 | {
|
|---|
| 554 | switch (rcNt)
|
|---|
| 555 | {
|
|---|
| 556 | default: errno = EINVAL; break;
|
|---|
| 557 | }
|
|---|
| 558 | return -1;
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | DWORD shfile_query_handle_access_mask(HANDLE h, PACCESS_MASK pMask)
|
|---|
| 562 | {
|
|---|
| 563 | MY_OBJECT_BASIC_INFORMATION BasicInfo;
|
|---|
| 564 | NTSTATUS rcNt;
|
|---|
| 565 |
|
|---|
| 566 | if (!g_pfnNtQueryObject)
|
|---|
| 567 | return ERROR_NOT_SUPPORTED;
|
|---|
| 568 |
|
|---|
| 569 | rcNt = g_pfnNtQueryObject(h, MY_ObjectBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
|
|---|
| 570 | if (rcNt >= 0)
|
|---|
| 571 | {
|
|---|
| 572 | *pMask = BasicInfo.GrantedAccess;
|
|---|
| 573 | return NO_ERROR;
|
|---|
| 574 | }
|
|---|
| 575 | if (rcNt != STATUS_INVALID_HANDLE)
|
|---|
| 576 | return ERROR_GEN_FAILURE;
|
|---|
| 577 | return ERROR_INVALID_HANDLE;
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | # endif /* K_OS == K_OS_WINDOWS */
|
|---|
| 581 |
|
|---|
| 582 | #endif /* SHFILE_IN_USE */
|
|---|
| 583 |
|
|---|
| 584 | /**
|
|---|
| 585 | * Converts DOS slashes to UNIX slashes if necessary.
|
|---|
| 586 | *
|
|---|
| 587 | * @param pszPath The path to fix.
|
|---|
| 588 | */
|
|---|
| 589 | K_INLINE void shfile_fix_slashes(char *pszPath)
|
|---|
| 590 | {
|
|---|
| 591 | #if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
|
|---|
| 592 | while ((pszPath = strchr(pszPath, '\\')))
|
|---|
| 593 | *pszPath++ = '/';
|
|---|
| 594 | #else
|
|---|
| 595 | (void)pszPath;
|
|---|
| 596 | #endif
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | /**
|
|---|
| 600 | * Initializes the global variables in this file.
|
|---|
| 601 | */
|
|---|
| 602 | static void shfile_init_globals(void)
|
|---|
| 603 | {
|
|---|
| 604 | #if K_OS == K_OS_WINDOWS
|
|---|
| 605 | if (!g_shfile_globals_initialized)
|
|---|
| 606 | {
|
|---|
| 607 | HMODULE hNtDll = GetModuleHandle("NTDLL");
|
|---|
| 608 | g_pfnNtQueryObject = (PFN_NtQueryObject) GetProcAddress(hNtDll, "NtQueryObject");
|
|---|
| 609 | g_pfnNtQueryDirectoryFile = (PFN_NtQueryDirectoryFile)GetProcAddress(hNtDll, "NtQueryDirectoryFile");
|
|---|
| 610 | g_pfnRtlUnicodeStringToAnsiString = (PFN_RtlUnicodeStringToAnsiString)GetProcAddress(hNtDll, "RtlUnicodeStringToAnsiString");
|
|---|
| 611 | if ( !g_pfnRtlUnicodeStringToAnsiString
|
|---|
| 612 | || !g_pfnNtQueryDirectoryFile)
|
|---|
| 613 | {
|
|---|
| 614 | /* fatal error */
|
|---|
| 615 | }
|
|---|
| 616 | g_shfile_globals_initialized = 1;
|
|---|
| 617 | }
|
|---|
| 618 | #endif
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | /**
|
|---|
| 622 | * Initializes a file descriptor table.
|
|---|
| 623 | *
|
|---|
| 624 | * @returns 0 on success, -1 and errno on failure.
|
|---|
| 625 | * @param pfdtab The table to initialize.
|
|---|
| 626 | * @param inherit File descriptor table to inherit from. If not specified
|
|---|
| 627 | * we will inherit from the current process as it were.
|
|---|
| 628 | */
|
|---|
| 629 | int shfile_init(shfdtab *pfdtab, shfdtab *inherit)
|
|---|
| 630 | {
|
|---|
| 631 | int rc;
|
|---|
| 632 |
|
|---|
| 633 | shfile_init_globals();
|
|---|
| 634 |
|
|---|
| 635 | pfdtab->cwd = NULL;
|
|---|
| 636 | pfdtab->size = 0;
|
|---|
| 637 | pfdtab->tab = NULL;
|
|---|
| 638 | rc = shmtx_init(&pfdtab->mtx);
|
|---|
| 639 | if (!rc)
|
|---|
| 640 | {
|
|---|
| 641 | #ifdef SHFILE_IN_USE
|
|---|
| 642 | /* Get CWD with unix slashes. */
|
|---|
| 643 | if (!inherit)
|
|---|
| 644 | {
|
|---|
| 645 | char buf[SHFILE_MAX_PATH];
|
|---|
| 646 | if (getcwd(buf, sizeof(buf)))
|
|---|
| 647 | {
|
|---|
| 648 | shfile_fix_slashes(buf);
|
|---|
| 649 | pfdtab->cwd = sh_strdup(NULL, buf);
|
|---|
| 650 | }
|
|---|
| 651 | if (pfdtab->cwd)
|
|---|
| 652 | {
|
|---|
| 653 | # if K_OS == K_OS_WINDOWS
|
|---|
| 654 | static const struct
|
|---|
| 655 | {
|
|---|
| 656 | DWORD dwStdHandle;
|
|---|
| 657 | unsigned fFlags;
|
|---|
| 658 | } aStdHandles[3] =
|
|---|
| 659 | {
|
|---|
| 660 | { STD_INPUT_HANDLE, _O_RDONLY },
|
|---|
| 661 | { STD_OUTPUT_HANDLE, _O_WRONLY },
|
|---|
| 662 | { STD_ERROR_HANDLE, _O_WRONLY }
|
|---|
| 663 | };
|
|---|
| 664 | int i;
|
|---|
| 665 | STARTUPINFO Info;
|
|---|
| 666 | ACCESS_MASK Mask;
|
|---|
| 667 | DWORD dwErr;
|
|---|
| 668 |
|
|---|
| 669 | rc = 0;
|
|---|
| 670 |
|
|---|
| 671 | /* Try pick up the Visual C++ CRT file descriptor info. */
|
|---|
| 672 | __try {
|
|---|
| 673 | GetStartupInfo(&Info);
|
|---|
| 674 | } __except (EXCEPTION_EXECUTE_HANDLER) {
|
|---|
| 675 | memset(&Info, 0, sizeof(Info));
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | if ( Info.cbReserved2 > sizeof(int)
|
|---|
| 679 | && (uintptr_t)Info.lpReserved2 >= 0x1000
|
|---|
| 680 | && (i = *(int *)Info.lpReserved2) >= 1
|
|---|
| 681 | && i <= 2048
|
|---|
| 682 | && ( Info.cbReserved2 == i * 5 + 4
|
|---|
| 683 | //|| Info.cbReserved2 == i * 5 + 1 - check the cygwin sources.
|
|---|
| 684 | || Info.cbReserved2 == i * 9 + 4))
|
|---|
| 685 | {
|
|---|
| 686 | uint8_t *paf = (uint8_t *)Info.lpReserved2 + sizeof(int);
|
|---|
| 687 | int dwPerH = 1 + (Info.cbReserved2 == i * 9 + 4);
|
|---|
| 688 | DWORD *ph = (DWORD *)(paf + i) + dwPerH * i;
|
|---|
| 689 | HANDLE aStdHandles2[3];
|
|---|
| 690 | int j;
|
|---|
| 691 |
|
|---|
| 692 | //if (Info.cbReserved2 == i * 5 + 1) - check the cygwin sources.
|
|---|
| 693 | // i--;
|
|---|
| 694 |
|
|---|
| 695 | for (j = 0; j < 3; j++)
|
|---|
| 696 | aStdHandles2[j] = GetStdHandle(aStdHandles[j].dwStdHandle);
|
|---|
| 697 |
|
|---|
| 698 | while (i-- > 0)
|
|---|
| 699 | {
|
|---|
| 700 | ph -= dwPerH;
|
|---|
| 701 |
|
|---|
| 702 | if ( (paf[i] & (FOPEN | FNOINHERIT)) == FOPEN
|
|---|
| 703 | && *ph != (uint32_t)INVALID_HANDLE_VALUE)
|
|---|
| 704 | {
|
|---|
| 705 | HANDLE h = (HANDLE)(intptr_t)*ph;
|
|---|
| 706 | int fd2;
|
|---|
| 707 | int fFlags;
|
|---|
| 708 | int fFlags2;
|
|---|
| 709 |
|
|---|
| 710 | if ( h == aStdHandles2[j = 0]
|
|---|
| 711 | || h == aStdHandles2[j = 1]
|
|---|
| 712 | || h == aStdHandles2[j = 2])
|
|---|
| 713 | fFlags = aStdHandles[j].fFlags;
|
|---|
| 714 | else
|
|---|
| 715 | {
|
|---|
| 716 | dwErr = shfile_query_handle_access_mask(h, &Mask);
|
|---|
| 717 | if (dwErr == ERROR_INVALID_HANDLE)
|
|---|
| 718 | continue;
|
|---|
| 719 | else if (dwErr == NO_ERROR)
|
|---|
| 720 | {
|
|---|
| 721 | fFlags = 0;
|
|---|
| 722 | if ( (Mask & (GENERIC_READ | FILE_READ_DATA))
|
|---|
| 723 | && (Mask & (GENERIC_WRITE | FILE_WRITE_DATA | FILE_APPEND_DATA)))
|
|---|
| 724 | fFlags |= O_RDWR;
|
|---|
| 725 | else if (Mask & (GENERIC_READ | FILE_READ_DATA))
|
|---|
| 726 | fFlags |= O_RDONLY;
|
|---|
| 727 | else if (Mask & (GENERIC_WRITE | FILE_WRITE_DATA | FILE_APPEND_DATA))
|
|---|
| 728 | fFlags |= O_WRONLY;
|
|---|
| 729 | else
|
|---|
| 730 | fFlags |= O_RDWR;
|
|---|
| 731 | if ((Mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) == FILE_APPEND_DATA)
|
|---|
| 732 | fFlags |= O_APPEND;
|
|---|
| 733 | }
|
|---|
| 734 | else
|
|---|
| 735 | fFlags = O_RDWR;
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | if (paf[i] & FPIPE)
|
|---|
| 739 | fFlags2 = SHFILE_FLAGS_PIPE;
|
|---|
| 740 | else if (paf[i] & FDEV)
|
|---|
| 741 | fFlags2 = SHFILE_FLAGS_TTY;
|
|---|
| 742 | else
|
|---|
| 743 | fFlags2 = 0;
|
|---|
| 744 |
|
|---|
| 745 | fd2 = shfile_insert(pfdtab, (intptr_t)h, fFlags, fFlags2, i, "shtab_init");
|
|---|
| 746 | assert(fd2 == i); (void)fd2;
|
|---|
| 747 | if (fd2 != i)
|
|---|
| 748 | rc = -1;
|
|---|
| 749 | }
|
|---|
| 750 | }
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | /* Check the three standard handles. */
|
|---|
| 754 | for (i = 0; i < 3; i++)
|
|---|
| 755 | if ( (unsigned)i >= pfdtab->size
|
|---|
| 756 | || pfdtab->tab[i].fd == -1)
|
|---|
| 757 | {
|
|---|
| 758 | HANDLE hFile = GetStdHandle(aStdHandles[i].dwStdHandle);
|
|---|
| 759 | if (hFile != INVALID_HANDLE_VALUE)
|
|---|
| 760 | {
|
|---|
| 761 | DWORD dwType = GetFileType(hFile);
|
|---|
| 762 | unsigned fFlags = aStdHandles[i].fFlags;
|
|---|
| 763 | unsigned fFlags2;
|
|---|
| 764 | int fd2;
|
|---|
| 765 | if (dwType == FILE_TYPE_CHAR)
|
|---|
| 766 | fFlags2 = SHFILE_FLAGS_TTY;
|
|---|
| 767 | else if (dwType == FILE_TYPE_PIPE)
|
|---|
| 768 | fFlags2 = SHFILE_FLAGS_PIPE;
|
|---|
| 769 | else
|
|---|
| 770 | fFlags2 = SHFILE_FLAGS_FILE;
|
|---|
| 771 | fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init");
|
|---|
| 772 | assert(fd2 == i); (void)fd2;
|
|---|
| 773 | if (fd2 != i)
|
|---|
| 774 | rc = -1;
|
|---|
| 775 | }
|
|---|
| 776 | }
|
|---|
| 777 | # else
|
|---|
| 778 | /*
|
|---|
| 779 | * Annoying...
|
|---|
| 780 | */
|
|---|
| 781 | int fd;
|
|---|
| 782 |
|
|---|
| 783 | for (fd = 0; fd < 10; fd++)
|
|---|
| 784 | {
|
|---|
| 785 | int oflags = fcntl(fd, F_GETFL, 0);
|
|---|
| 786 | if (oflags != -1)
|
|---|
| 787 | {
|
|---|
| 788 | int cox = fcntl(fd, F_GETFD, 0);
|
|---|
| 789 | struct stat st;
|
|---|
| 790 | if ( cox != -1
|
|---|
| 791 | && fstat(fd, &st) != -1)
|
|---|
| 792 | {
|
|---|
| 793 | int native;
|
|---|
| 794 | int fd2;
|
|---|
| 795 | int fFlags2 = 0;
|
|---|
| 796 | if (cox & FD_CLOEXEC)
|
|---|
| 797 | fFlags2 |= SHFILE_FLAGS_CLOSE_ON_EXEC;
|
|---|
| 798 | if (S_ISREG(st.st_mode))
|
|---|
| 799 | fFlags2 |= SHFILE_FLAGS_FILE;
|
|---|
| 800 | else if (S_ISDIR(st.st_mode))
|
|---|
| 801 | fFlags2 |= SHFILE_FLAGS_DIR;
|
|---|
| 802 | else if (S_ISCHR(st.st_mode))
|
|---|
| 803 | fFlags2 |= SHFILE_FLAGS_TTY;
|
|---|
| 804 | else if (S_ISFIFO(st.st_mode))
|
|---|
| 805 | fFlags2 |= SHFILE_FLAGS_PIPE;
|
|---|
| 806 | else
|
|---|
| 807 | fFlags2 |= SHFILE_FLAGS_TTY;
|
|---|
| 808 |
|
|---|
| 809 | native = fcntl(fd, F_DUPFD, SHFILE_UNIX_MIN_FD);
|
|---|
| 810 | if (native == -1)
|
|---|
| 811 | native = fd;
|
|---|
| 812 | fd2 = shfile_insert(pfdtab, native, oflags, fFlags2, fd, "shtab_init");
|
|---|
| 813 | assert(fd2 == fd); (void)fd2;
|
|---|
| 814 | if (fd2 != fd)
|
|---|
| 815 | rc = -1;
|
|---|
| 816 | if (native != fd)
|
|---|
| 817 | close(fd);
|
|---|
| 818 | }
|
|---|
| 819 | }
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | # endif
|
|---|
| 823 | }
|
|---|
| 824 | else
|
|---|
| 825 | rc = -1;
|
|---|
| 826 | }
|
|---|
| 827 | else
|
|---|
| 828 | {
|
|---|
| 829 | /*
|
|---|
| 830 | * Inherit from parent shell's file table.
|
|---|
| 831 | */
|
|---|
| 832 | shfile const *src;
|
|---|
| 833 | shfile *dst;
|
|---|
| 834 | shmtxtmp tmp;
|
|---|
| 835 | unsigned fdcount;
|
|---|
| 836 | unsigned fd;
|
|---|
| 837 |
|
|---|
| 838 | shmtx_enter(&inherit->mtx, &tmp);
|
|---|
| 839 |
|
|---|
| 840 | /* allocate table and cwd: */
|
|---|
| 841 | fdcount = inherit->size;
|
|---|
| 842 | pfdtab->tab = dst = (shfile *)(fdcount ? sh_calloc(NULL, sizeof(pfdtab->tab[0]), fdcount) : NULL);
|
|---|
| 843 | pfdtab->cwd = sh_strdup(NULL, inherit->cwd);
|
|---|
| 844 | if ( pfdtab->cwd
|
|---|
| 845 | && (pfdtab->tab || fdcount == 0))
|
|---|
| 846 | {
|
|---|
| 847 | /* duplicate table entries: */
|
|---|
| 848 | for (fd = 0, src = inherit->tab; fd < fdcount; fd++, src++, dst++)
|
|---|
| 849 | if (src->fd == -1)
|
|---|
| 850 | dst->native = dst->fd = -1;
|
|---|
| 851 | else
|
|---|
| 852 | {
|
|---|
| 853 | # if K_OS == K_OS_WINDOWS
|
|---|
| 854 | # ifdef SH_FORKED_MODE
|
|---|
| 855 | KBOOL const cox = !!(src->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC);
|
|---|
| 856 | # else
|
|---|
| 857 | KBOOL const cox = K_TRUE;
|
|---|
| 858 | # endif
|
|---|
| 859 | # endif
|
|---|
| 860 | *dst = *src;
|
|---|
| 861 | # if K_OS == K_OS_WINDOWS
|
|---|
| 862 | if (DuplicateHandle(GetCurrentProcess(),
|
|---|
| 863 | (HANDLE)src->native,
|
|---|
| 864 | GetCurrentProcess(),
|
|---|
| 865 | (HANDLE *)&dst->native,
|
|---|
| 866 | 0,
|
|---|
| 867 | FALSE /* bInheritHandle */,
|
|---|
| 868 | DUPLICATE_SAME_ACCESS))
|
|---|
| 869 | TRACE2((NULL, "shfile_init: %d (%#x, %#x) %p (was %p)\n",
|
|---|
| 870 | dst->fd, dst->oflags, dst->shflags, dst->native, src->native));
|
|---|
| 871 | else
|
|---|
| 872 | {
|
|---|
| 873 | dst->native = (intptr_t)INVALID_HANDLE_VALUE;
|
|---|
| 874 | rc = shfile_dos2errno(GetLastError());
|
|---|
| 875 | TRACE2((NULL, "shfile_init: %d (%#x, %#x) %p - failed %d / %u!\n",
|
|---|
| 876 | dst->fd, dst->oflags, dst->shflags, src->native, rc, GetLastError()));
|
|---|
| 877 | break;
|
|---|
| 878 | }
|
|---|
| 879 |
|
|---|
| 880 | # elif K_OS == K_OS_LINUX /* 2.6.27 / glibc 2.9 */ || K_OS == K_OS_FREEBSD /* 10.0 */ || K_OS == K_OS_NETBSD /* 6.0 */
|
|---|
| 881 | dst->native = dup3(src->native, -1, cox ? O_CLOEXEC : 0);
|
|---|
| 882 | # else
|
|---|
| 883 | if (cox)
|
|---|
| 884 | {
|
|---|
| 885 | # ifndef SH_FORKED_MODE
|
|---|
| 886 | shmtxtmp tmp2;
|
|---|
| 887 | shmtx_enter(&global_exec_something, &tmp)
|
|---|
| 888 | # endif
|
|---|
| 889 | dst->native = dup2(src->native, -1);
|
|---|
| 890 | if (dst->native >= 0)
|
|---|
| 891 | rc = fcntl(dst->native, F_SETFD, FD_CLOEXEC);
|
|---|
| 892 | # ifndef SH_FORKED_MODE
|
|---|
| 893 | shmtx_leave(&global_exec_something, &tmp)
|
|---|
| 894 | # endif
|
|---|
| 895 | if (rc != 0)
|
|---|
| 896 | break;
|
|---|
| 897 | }
|
|---|
| 898 | else
|
|---|
| 899 | dst->native = dup2(src->native, -1);
|
|---|
| 900 | if (dst->native < 0)
|
|---|
| 901 | {
|
|---|
| 902 | rc = -1;
|
|---|
| 903 | break;
|
|---|
| 904 | }
|
|---|
| 905 | # endif
|
|---|
| 906 | }
|
|---|
| 907 | }
|
|---|
| 908 | else
|
|---|
| 909 | rc = -1;
|
|---|
| 910 | pfdtab->size = fd;
|
|---|
| 911 | shmtx_leave(&inherit->mtx, &tmp);
|
|---|
| 912 | } /* inherit != NULL */
|
|---|
| 913 | #endif
|
|---|
| 914 | }
|
|---|
| 915 | return rc;
|
|---|
| 916 | }
|
|---|
| 917 |
|
|---|
| 918 | /**
|
|---|
| 919 | * Deletes the file descriptor table.
|
|---|
| 920 | *
|
|---|
| 921 | * Safe to call more than once.
|
|---|
| 922 | */
|
|---|
| 923 | void shfile_uninit(shfdtab *pfdtab)
|
|---|
| 924 | {
|
|---|
| 925 | if (!pfdtab)
|
|---|
| 926 | return;
|
|---|
| 927 |
|
|---|
| 928 | if (pfdtab->tab)
|
|---|
| 929 | {
|
|---|
| 930 | unsigned left = pfdtab->size;
|
|---|
| 931 | struct shfile *pfd = pfdtab->tab;
|
|---|
| 932 | while (left-- > 0)
|
|---|
| 933 | {
|
|---|
| 934 | if (pfd->fd != -1)
|
|---|
| 935 | {
|
|---|
| 936 | #if K_OS == K_OS_WINDOWS
|
|---|
| 937 | BOOL rc = CloseHandle((HANDLE)pfd->native);
|
|---|
| 938 | assert(rc == TRUE); K_NOREF(rc);
|
|---|
| 939 | #else
|
|---|
| 940 | int rc = close((int)pfd->native);
|
|---|
| 941 | assert(rc == 0); K_NOREF(rc);
|
|---|
| 942 | #endif
|
|---|
| 943 | pfd->fd = -1;
|
|---|
| 944 | pfd->native = -1;
|
|---|
| 945 | }
|
|---|
| 946 | pfd++;
|
|---|
| 947 | }
|
|---|
| 948 |
|
|---|
| 949 | sh_free(NULL, pfdtab->tab);
|
|---|
| 950 | pfdtab->tab = NULL;
|
|---|
| 951 | }
|
|---|
| 952 |
|
|---|
| 953 | shmtx_delete(&pfdtab->mtx);
|
|---|
| 954 |
|
|---|
| 955 | sh_free(NULL, pfdtab->cwd);
|
|---|
| 956 | pfdtab->cwd = NULL;
|
|---|
| 957 | }
|
|---|
| 958 |
|
|---|
| 959 | #if K_OS == K_OS_WINDOWS && defined(SHFILE_IN_USE)
|
|---|
| 960 |
|
|---|
| 961 | /**
|
|---|
| 962 | * Changes the inheritability of a file descriptor, taking console handles into
|
|---|
| 963 | * account.
|
|---|
| 964 | *
|
|---|
| 965 | * @note This MAY change the native handle for the entry.
|
|---|
| 966 | *
|
|---|
| 967 | * @returns The native handle.
|
|---|
| 968 | * @param pfd The file descriptor to change.
|
|---|
| 969 | * @param set If set, make child processes inherit the handle, if clear
|
|---|
| 970 | * make them not inherit it.
|
|---|
| 971 | */
|
|---|
| 972 | static HANDLE shfile_set_inherit_win(shfile *pfd, int set)
|
|---|
| 973 | {
|
|---|
| 974 | HANDLE hFile = (HANDLE)pfd->native;
|
|---|
| 975 | if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, set ? HANDLE_FLAG_INHERIT : 0))
|
|---|
| 976 | {
|
|---|
| 977 | /* SetHandleInformation doesn't work for console handles,
|
|---|
| 978 | so we have to duplicate the handle to change the
|
|---|
| 979 | inheritability. */
|
|---|
| 980 | DWORD err = GetLastError();
|
|---|
| 981 | if ( err == ERROR_INVALID_PARAMETER
|
|---|
| 982 | && DuplicateHandle(GetCurrentProcess(),
|
|---|
| 983 | hFile,
|
|---|
| 984 | GetCurrentProcess(),
|
|---|
| 985 | &hFile,
|
|---|
| 986 | 0,
|
|---|
| 987 | set ? TRUE : FALSE /* bInheritHandle */,
|
|---|
| 988 | DUPLICATE_SAME_ACCESS))
|
|---|
| 989 | {
|
|---|
| 990 | TRACE2((NULL, "shfile_set_inherit_win: %p -> %p (set=%d)\n", pfd->native, hFile, set));
|
|---|
| 991 | if (!CloseHandle((HANDLE)pfd->native))
|
|---|
| 992 | assert(0);
|
|---|
| 993 | pfd->native = (intptr_t)hFile;
|
|---|
| 994 | }
|
|---|
| 995 | else
|
|---|
| 996 | {
|
|---|
| 997 | err = GetLastError();
|
|---|
| 998 | assert(0);
|
|---|
| 999 | hFile = (HANDLE)pfd->native;
|
|---|
| 1000 | }
|
|---|
| 1001 | }
|
|---|
| 1002 | return hFile;
|
|---|
| 1003 | }
|
|---|
| 1004 |
|
|---|
| 1005 | # ifdef SH_FORKED_MODE
|
|---|
| 1006 | /**
|
|---|
| 1007 | * Helper for shfork.
|
|---|
| 1008 | *
|
|---|
| 1009 | * @param pfdtab The file descriptor table.
|
|---|
| 1010 | * @param set Whether to make all handles inheritable (1) or
|
|---|
| 1011 | * to restore them to the rigth state (0).
|
|---|
| 1012 | * @param hndls Where to store the three standard handles.
|
|---|
| 1013 | */
|
|---|
| 1014 | void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls)
|
|---|
| 1015 | {
|
|---|
| 1016 | shmtxtmp tmp;
|
|---|
| 1017 | unsigned i;
|
|---|
| 1018 |
|
|---|
| 1019 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 1020 | TRACE2((NULL, "shfile_fork_win: set=%d\n", set));
|
|---|
| 1021 |
|
|---|
| 1022 | i = pfdtab->size;
|
|---|
| 1023 | while (i-- > 0)
|
|---|
| 1024 | {
|
|---|
| 1025 | if (pfdtab->tab[i].fd == i)
|
|---|
| 1026 | {
|
|---|
| 1027 | shfile_set_inherit_win(&pfdtab->tab[i], set);
|
|---|
| 1028 | if (set)
|
|---|
| 1029 | TRACE2((NULL, " #%d: native=%#x oflags=%#x shflags=%#x\n",
|
|---|
| 1030 | i, pfdtab->tab[i].native, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
|
|---|
| 1031 | }
|
|---|
| 1032 | }
|
|---|
| 1033 |
|
|---|
| 1034 | if (hndls)
|
|---|
| 1035 | {
|
|---|
| 1036 | for (i = 0; i < 3; i++)
|
|---|
| 1037 | {
|
|---|
| 1038 | if ( pfdtab->size > i
|
|---|
| 1039 | && pfdtab->tab[i].fd == i)
|
|---|
| 1040 | hndls[i] = pfdtab->tab[i].native;
|
|---|
| 1041 | else
|
|---|
| 1042 | hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
|
|---|
| 1043 | TRACE2((NULL, "shfile_fork_win: i=%d size=%d fd=%d native=%d hndls[%d]=%p\n",
|
|---|
| 1044 | i, pfdtab->size, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, hndls[i]));
|
|---|
| 1045 | }
|
|---|
| 1046 | }
|
|---|
| 1047 |
|
|---|
| 1048 | shmtx_leave(&pfdtab->mtx, &tmp);
|
|---|
| 1049 | }
|
|---|
| 1050 | # endif /* SH_FORKED_MODE */
|
|---|
| 1051 |
|
|---|
| 1052 | /** shfile_exec_win helper that make sure there are no _O_APPEND handles. */
|
|---|
| 1053 | static KBOOL shfile_exec_win_no_append(shfdtab *pfdtab, unsigned count)
|
|---|
| 1054 | {
|
|---|
| 1055 | unsigned i;
|
|---|
| 1056 | for (i = 0; i < count; i++)
|
|---|
| 1057 | if ( (pfdtab->tab[i].oflags & _O_APPEND)
|
|---|
| 1058 | && (pfdtab->tab[i].oflags & (_O_WRONLY | _O_RDWR)))
|
|---|
| 1059 | return K_FALSE;
|
|---|
| 1060 | return K_TRUE;
|
|---|
| 1061 | }
|
|---|
| 1062 |
|
|---|
| 1063 | /**
|
|---|
| 1064 | * Helper for sh_execve.
|
|---|
| 1065 | *
|
|---|
| 1066 | * This is called before and after CreateProcess. On the first call it
|
|---|
| 1067 | * will mark the non-close-on-exec handles as inheritable and produce
|
|---|
| 1068 | * the startup info for the CRT. On the second call, after CreateProcess,
|
|---|
| 1069 | * it will restore the handle inheritability properties.
|
|---|
| 1070 | *
|
|---|
| 1071 | * @returns 0 on success, non-zero on failure.
|
|---|
| 1072 | * @param pfdtab The file descriptor table.
|
|---|
| 1073 | * @param prepare Which call, 1 if before, 0 if after and success, -1 if after on failure.
|
|---|
| 1074 | * @param info The info structure.
|
|---|
| 1075 | */
|
|---|
| 1076 | int shfile_exec_win(shfdtab *pfdtab, int prepare, shfdexecwin *info)
|
|---|
| 1077 | {
|
|---|
| 1078 | STARTUPINFOA *strtinfo = (STARTUPINFOA *)info->strtinfo;
|
|---|
| 1079 | int rc = 0;
|
|---|
| 1080 | shmtxtmp tmp;
|
|---|
| 1081 | unsigned count;
|
|---|
| 1082 | unsigned i;
|
|---|
| 1083 |
|
|---|
| 1084 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 1085 | TRACE2((NULL, "shfile_exec_win: prepare=%p\n", prepare));
|
|---|
| 1086 |
|
|---|
| 1087 | count = pfdtab->size < (0x10000-4) / (1 + sizeof(HANDLE))
|
|---|
| 1088 | ? pfdtab->size
|
|---|
| 1089 | : (0x10000-4) / (1 + sizeof(HANDLE));
|
|---|
| 1090 | while ( count > 3
|
|---|
| 1091 | && ( pfdtab->tab[count - 1].fd == -1
|
|---|
| 1092 | || (pfdtab->tab[count - 1].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC)))
|
|---|
| 1093 | count--;
|
|---|
| 1094 |
|
|---|
| 1095 | if (prepare > 0)
|
|---|
| 1096 | {
|
|---|
| 1097 | if (count <= 3 && shfile_exec_win_no_append(pfdtab, count))
|
|---|
| 1098 | {
|
|---|
| 1099 | info->inherithandles = 0;
|
|---|
| 1100 | info->startsuspended = 1;
|
|---|
| 1101 | strtinfo->cbReserved2 = 0;
|
|---|
| 1102 | strtinfo->lpReserved2 = NULL;
|
|---|
| 1103 | }
|
|---|
| 1104 | else
|
|---|
| 1105 | {
|
|---|
| 1106 | size_t cbData = sizeof(int) + count * (1 + sizeof(HANDLE));
|
|---|
| 1107 | uint8_t *pbData = sh_malloc(shthread_get_shell(), cbData);
|
|---|
| 1108 | uint8_t *paf = pbData + sizeof(int);
|
|---|
| 1109 | HANDLE *pah = (HANDLE *)(paf + count);
|
|---|
| 1110 |
|
|---|
| 1111 | info->inherithandles = 1;
|
|---|
| 1112 | info->startsuspended = 0;
|
|---|
| 1113 | strtinfo->cbReserved2 = (unsigned short)cbData;
|
|---|
| 1114 | strtinfo->lpReserved2 = pbData;
|
|---|
| 1115 |
|
|---|
| 1116 | # ifndef SH_FORKED_MODE
|
|---|
| 1117 | shmtx_leave(&pfdtab->mtx, &tmp); /* should be harmless as this isn't really necessary at all. */
|
|---|
| 1118 | shmtx_enter(&g_sh_exec_inherit_mtx, &info->tmp);
|
|---|
| 1119 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 1120 | # endif
|
|---|
| 1121 |
|
|---|
| 1122 | *(int *)pbData = count;
|
|---|
| 1123 |
|
|---|
| 1124 | i = count;
|
|---|
| 1125 | while (i-- > 0)
|
|---|
| 1126 | {
|
|---|
| 1127 | if ( pfdtab->tab[i].fd == i
|
|---|
| 1128 | && !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
|
|---|
| 1129 | {
|
|---|
| 1130 | HANDLE hFile = shfile_set_inherit_win(&pfdtab->tab[i], 1);
|
|---|
| 1131 | TRACE2((NULL, " #%d: native=%#x oflags=%#x shflags=%#x\n",
|
|---|
| 1132 | i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
|
|---|
| 1133 | paf[i] = FOPEN;
|
|---|
| 1134 | if (pfdtab->tab[i].oflags & _O_APPEND)
|
|---|
| 1135 | paf[i] |= FAPPEND;
|
|---|
| 1136 | if (pfdtab->tab[i].oflags & _O_TEXT)
|
|---|
| 1137 | paf[i] |= FTEXT;
|
|---|
| 1138 | switch (pfdtab->tab[i].shflags & SHFILE_FLAGS_TYPE_MASK)
|
|---|
| 1139 | {
|
|---|
| 1140 | case SHFILE_FLAGS_TTY: paf[i] |= FDEV; break;
|
|---|
| 1141 | case SHFILE_FLAGS_PIPE: paf[i] |= FPIPE; break;
|
|---|
| 1142 | }
|
|---|
| 1143 | pah[i] = hFile;
|
|---|
| 1144 | }
|
|---|
| 1145 | else
|
|---|
| 1146 | {
|
|---|
| 1147 | paf[i] = 0;
|
|---|
| 1148 | pah[i] = INVALID_HANDLE_VALUE;
|
|---|
| 1149 | }
|
|---|
| 1150 | }
|
|---|
| 1151 | }
|
|---|
| 1152 |
|
|---|
| 1153 | for (i = 0; i < 3; i++)
|
|---|
| 1154 | {
|
|---|
| 1155 | if ( i < count
|
|---|
| 1156 | && pfdtab->tab[i].fd == i)
|
|---|
| 1157 | {
|
|---|
| 1158 | info->replacehandles[i] = 1;
|
|---|
| 1159 | info->handles[i] = pfdtab->tab[i].native;
|
|---|
| 1160 | }
|
|---|
| 1161 | else
|
|---|
| 1162 | {
|
|---|
| 1163 | info->replacehandles[i] = 0;
|
|---|
| 1164 | info->handles[i] = (intptr_t)INVALID_HANDLE_VALUE;
|
|---|
| 1165 | }
|
|---|
| 1166 | TRACE2((NULL, "shfile_exec_win: i=%d count=%d fd=%d native=%d hndls[%d]=\n",
|
|---|
| 1167 | i, count, pfdtab->tab[i].fd, pfdtab->tab[i].native, i, info->handles[i]));
|
|---|
| 1168 | }
|
|---|
| 1169 | }
|
|---|
| 1170 | else
|
|---|
| 1171 | {
|
|---|
| 1172 | shfile *file = pfdtab->tab;
|
|---|
| 1173 |
|
|---|
| 1174 | sh_free(NULL, strtinfo->lpReserved2);
|
|---|
| 1175 | strtinfo->lpReserved2 = NULL;
|
|---|
| 1176 |
|
|---|
| 1177 | i = count;
|
|---|
| 1178 | if (prepare == 0)
|
|---|
| 1179 | for (i = 0; i < count; i++, file++)
|
|---|
| 1180 | {
|
|---|
| 1181 | if ( file->fd == i
|
|---|
| 1182 | && !(file->shflags & SHFILE_FLAGS_TRACE))
|
|---|
| 1183 | {
|
|---|
| 1184 | shfile_native_close(file->native, file->oflags);
|
|---|
| 1185 |
|
|---|
| 1186 | file->fd = -1;
|
|---|
| 1187 | file->oflags = 0;
|
|---|
| 1188 | file->shflags = 0;
|
|---|
| 1189 | file->native = -1;
|
|---|
| 1190 | }
|
|---|
| 1191 | }
|
|---|
| 1192 | else if (info->inherithandles)
|
|---|
| 1193 | for (i = 0; i < count; i++, file++)
|
|---|
| 1194 | if ( file->fd == i
|
|---|
| 1195 | && !(file->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
|
|---|
| 1196 | shfile_set_inherit_win(file, 0);
|
|---|
| 1197 |
|
|---|
| 1198 | # ifndef SH_FORKED_MODE
|
|---|
| 1199 | if (info->inherithandles)
|
|---|
| 1200 | shmtx_leave(&g_sh_exec_inherit_mtx, &info->tmp);
|
|---|
| 1201 | # endif
|
|---|
| 1202 | }
|
|---|
| 1203 |
|
|---|
| 1204 | shmtx_leave(&pfdtab->mtx, &tmp);
|
|---|
| 1205 | return rc;
|
|---|
| 1206 | }
|
|---|
| 1207 |
|
|---|
| 1208 | #endif /* K_OS_WINDOWS */
|
|---|
| 1209 |
|
|---|
| 1210 | #if K_OS != K_OS_WINDOWS
|
|---|
| 1211 | /**
|
|---|
| 1212 | * Prepare file handles for inherting before a execve call.
|
|---|
| 1213 | *
|
|---|
| 1214 | * This is only used in the normal mode, so we've forked and need not worry
|
|---|
| 1215 | * about cleaning anything up after us. Nor do we need think about locking.
|
|---|
| 1216 | *
|
|---|
| 1217 | * @returns 0 on success, -1 on failure.
|
|---|
| 1218 | */
|
|---|
| 1219 | int shfile_exec_unix(shfdtab *pfdtab)
|
|---|
| 1220 | {
|
|---|
| 1221 | int rc = 0;
|
|---|
| 1222 | # ifdef SHFILE_IN_USE
|
|---|
| 1223 | unsigned fd;
|
|---|
| 1224 |
|
|---|
| 1225 | for (fd = 0; fd < pfdtab->size; fd++)
|
|---|
| 1226 | {
|
|---|
| 1227 | if ( pfdtab->tab[fd].fd != -1
|
|---|
| 1228 | && !(pfdtab->tab[fd].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC) )
|
|---|
| 1229 | {
|
|---|
| 1230 | TRACE2((NULL, "shfile_exec_unix: %d => %d\n", pfdtab->tab[fd].native, fd));
|
|---|
| 1231 | if (dup2(pfdtab->tab[fd].native, fd) < 0)
|
|---|
| 1232 | {
|
|---|
| 1233 | /* fatal_error(NULL, "shfile_exec_unix: failed to move %d to %d", pfdtab->tab[fd].fd, fd); */
|
|---|
| 1234 | rc = -1;
|
|---|
| 1235 | }
|
|---|
| 1236 | }
|
|---|
| 1237 | }
|
|---|
| 1238 | # endif
|
|---|
| 1239 | return rc;
|
|---|
| 1240 | }
|
|---|
| 1241 | #endif /* !K_OS_WINDOWS */
|
|---|
| 1242 |
|
|---|
| 1243 | /**
|
|---|
| 1244 | * open().
|
|---|
| 1245 | */
|
|---|
| 1246 | int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
|
|---|
| 1247 | {
|
|---|
| 1248 | int fd;
|
|---|
| 1249 | #ifdef SHFILE_IN_USE
|
|---|
| 1250 | char absname[SHFILE_MAX_PATH];
|
|---|
| 1251 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1252 | HANDLE hFile;
|
|---|
| 1253 | DWORD dwDesiredAccess;
|
|---|
| 1254 | DWORD dwShareMode;
|
|---|
| 1255 | DWORD dwCreationDisposition;
|
|---|
| 1256 | DWORD dwFlagsAndAttributes;
|
|---|
| 1257 | SECURITY_ATTRIBUTES SecurityAttributes;
|
|---|
| 1258 |
|
|---|
| 1259 | # ifndef _O_ACCMODE
|
|---|
| 1260 | # define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
|
|---|
| 1261 | # endif
|
|---|
| 1262 | switch (flags & (_O_ACCMODE | _O_APPEND))
|
|---|
| 1263 | {
|
|---|
| 1264 | case _O_RDONLY: dwDesiredAccess = GENERIC_READ; break;
|
|---|
| 1265 | case _O_RDONLY | _O_APPEND: dwDesiredAccess = GENERIC_READ; break;
|
|---|
| 1266 | case _O_WRONLY: dwDesiredAccess = GENERIC_WRITE; break;
|
|---|
| 1267 | case _O_WRONLY | _O_APPEND: dwDesiredAccess = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
|
|---|
| 1268 | case _O_RDWR: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; break;
|
|---|
| 1269 | case _O_RDWR | _O_APPEND: dwDesiredAccess = GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
|
|---|
| 1270 |
|
|---|
| 1271 | default:
|
|---|
| 1272 | RETURN_ERROR(-1, EINVAL, "invalid mode");
|
|---|
| 1273 | }
|
|---|
| 1274 |
|
|---|
| 1275 | dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
|
|---|
| 1276 |
|
|---|
| 1277 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
|---|
| 1278 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
|---|
| 1279 | SecurityAttributes.bInheritHandle = FALSE;
|
|---|
| 1280 |
|
|---|
| 1281 | if (flags & _O_CREAT)
|
|---|
| 1282 | {
|
|---|
| 1283 | if ((flags & (_O_EXCL | _O_TRUNC)) == (_O_EXCL | _O_TRUNC))
|
|---|
| 1284 | RETURN_ERROR(-1, EINVAL, "_O_EXCL | _O_TRUNC");
|
|---|
| 1285 |
|
|---|
| 1286 | if (flags & _O_TRUNC)
|
|---|
| 1287 | dwCreationDisposition = CREATE_ALWAYS; /* not 100%, but close enough */
|
|---|
| 1288 | else if (flags & _O_EXCL)
|
|---|
| 1289 | dwCreationDisposition = CREATE_NEW;
|
|---|
| 1290 | else
|
|---|
| 1291 | dwCreationDisposition = OPEN_ALWAYS;
|
|---|
| 1292 | }
|
|---|
| 1293 | else if (flags & _O_TRUNC)
|
|---|
| 1294 | dwCreationDisposition = TRUNCATE_EXISTING;
|
|---|
| 1295 | else
|
|---|
| 1296 | dwCreationDisposition = OPEN_EXISTING;
|
|---|
| 1297 |
|
|---|
| 1298 | if (!(flags & _O_CREAT) || (mode & 0222))
|
|---|
| 1299 | dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
|
|---|
| 1300 | else
|
|---|
| 1301 | dwFlagsAndAttributes = FILE_ATTRIBUTE_READONLY;
|
|---|
| 1302 |
|
|---|
| 1303 | fd = shfile_make_path(pfdtab, name, &absname[0]);
|
|---|
| 1304 | if (!fd)
|
|---|
| 1305 | {
|
|---|
| 1306 | SetLastError(0);
|
|---|
| 1307 | hFile = CreateFileA(absname,
|
|---|
| 1308 | dwDesiredAccess,
|
|---|
| 1309 | dwShareMode,
|
|---|
| 1310 | &SecurityAttributes,
|
|---|
| 1311 | dwCreationDisposition,
|
|---|
| 1312 | dwFlagsAndAttributes,
|
|---|
| 1313 | NULL /* hTemplateFile */);
|
|---|
| 1314 | if (hFile != INVALID_HANDLE_VALUE)
|
|---|
| 1315 | fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, 0, -1, "shfile_open");
|
|---|
| 1316 | else
|
|---|
| 1317 | fd = shfile_dos2errno(GetLastError());
|
|---|
| 1318 | }
|
|---|
| 1319 |
|
|---|
| 1320 | # else /* K_OS != K_OS_WINDOWS */
|
|---|
| 1321 | fd = shfile_make_path(pfdtab, name, &absname[0]);
|
|---|
| 1322 | if (!fd)
|
|---|
| 1323 | {
|
|---|
| 1324 | fd = open(absname, flags, mode);
|
|---|
| 1325 | if (fd != -1)
|
|---|
| 1326 | fd = shfile_copy_insert_and_close(pfdtab, &fd, flags, 0, -1, "shfile_open");
|
|---|
| 1327 | }
|
|---|
| 1328 |
|
|---|
| 1329 | # endif /* K_OS != K_OS_WINDOWS */
|
|---|
| 1330 |
|
|---|
| 1331 | #else
|
|---|
| 1332 | fd = open(name, flags, mode);
|
|---|
| 1333 | #endif
|
|---|
| 1334 |
|
|---|
| 1335 | TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
|
|---|
| 1336 | return fd;
|
|---|
| 1337 | }
|
|---|
| 1338 |
|
|---|
| 1339 | int shfile_pipe(shfdtab *pfdtab, int fds[2])
|
|---|
| 1340 | {
|
|---|
| 1341 | int rc = -1;
|
|---|
| 1342 | #ifdef SHFILE_IN_USE
|
|---|
| 1343 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1344 | HANDLE hRead = INVALID_HANDLE_VALUE;
|
|---|
| 1345 | HANDLE hWrite = INVALID_HANDLE_VALUE;
|
|---|
| 1346 | SECURITY_ATTRIBUTES SecurityAttributes;
|
|---|
| 1347 |
|
|---|
| 1348 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
|---|
| 1349 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
|---|
| 1350 | SecurityAttributes.bInheritHandle = FALSE;
|
|---|
| 1351 |
|
|---|
| 1352 | fds[1] = fds[0] = -1;
|
|---|
| 1353 | if (CreatePipe(&hRead, &hWrite, &SecurityAttributes, 4096))
|
|---|
| 1354 | {
|
|---|
| 1355 | fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
|---|
| 1356 | if (fds[0] != -1)
|
|---|
| 1357 | {
|
|---|
| 1358 | fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
|---|
| 1359 | if (fds[1] != -1)
|
|---|
| 1360 | rc = 0;
|
|---|
| 1361 | }
|
|---|
| 1362 |
|
|---|
| 1363 | # else
|
|---|
| 1364 | int native_fds[2];
|
|---|
| 1365 |
|
|---|
| 1366 | fds[1] = fds[0] = -1;
|
|---|
| 1367 | if (!pipe(native_fds))
|
|---|
| 1368 | {
|
|---|
| 1369 | fds[0] = shfile_copy_insert_and_close(pfdtab, &native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
|---|
| 1370 | if (fds[0] != -1)
|
|---|
| 1371 | {
|
|---|
| 1372 | fds[1] = shfile_copy_insert_and_close(pfdtab, &native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
|
|---|
| 1373 | if (fds[1] != -1)
|
|---|
| 1374 | rc = 0;
|
|---|
| 1375 | }
|
|---|
| 1376 | # endif
|
|---|
| 1377 | if (fds[1] == -1)
|
|---|
| 1378 | {
|
|---|
| 1379 | int s = errno;
|
|---|
| 1380 | if (fds[0] != -1)
|
|---|
| 1381 | {
|
|---|
| 1382 | shmtxtmp tmp;
|
|---|
| 1383 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 1384 | rc = fds[0];
|
|---|
| 1385 | pfdtab->tab[rc].fd = -1;
|
|---|
| 1386 | pfdtab->tab[rc].oflags = 0;
|
|---|
| 1387 | pfdtab->tab[rc].shflags = 0;
|
|---|
| 1388 | pfdtab->tab[rc].native = -1;
|
|---|
| 1389 | shmtx_leave(&pfdtab->mtx, &tmp);
|
|---|
| 1390 | }
|
|---|
| 1391 |
|
|---|
| 1392 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1393 | CloseHandle(hRead);
|
|---|
| 1394 | CloseHandle(hWrite);
|
|---|
| 1395 | # else
|
|---|
| 1396 | close(native_fds[0]);
|
|---|
| 1397 | close(native_fds[1]);
|
|---|
| 1398 | # endif
|
|---|
| 1399 | fds[0] = fds[1] = -1;
|
|---|
| 1400 | errno = s;
|
|---|
| 1401 | rc = -1;
|
|---|
| 1402 | }
|
|---|
| 1403 | }
|
|---|
| 1404 | else
|
|---|
| 1405 | {
|
|---|
| 1406 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1407 | errno = shfile_dos2errno(GetLastError());
|
|---|
| 1408 | # endif
|
|---|
| 1409 | rc = -1;
|
|---|
| 1410 | }
|
|---|
| 1411 |
|
|---|
| 1412 | #else
|
|---|
| 1413 | rc = pipe(fds);
|
|---|
| 1414 | #endif
|
|---|
| 1415 |
|
|---|
| 1416 | TRACE2((NULL, "shfile_pipe() -> %d{%d,%d} [%d]\n", rc, fds[0], fds[1], errno));
|
|---|
| 1417 | return rc;
|
|---|
| 1418 | }
|
|---|
| 1419 |
|
|---|
| 1420 | /**
|
|---|
| 1421 | * dup().
|
|---|
| 1422 | */
|
|---|
| 1423 | int shfile_dup(shfdtab *pfdtab, int fd)
|
|---|
| 1424 | {
|
|---|
| 1425 | return shfile_fcntl(pfdtab,fd, F_DUPFD, 0);
|
|---|
| 1426 | }
|
|---|
| 1427 |
|
|---|
| 1428 | /**
|
|---|
| 1429 | * Move the file descriptor, closing any existing descriptor at @a fdto.
|
|---|
| 1430 | *
|
|---|
| 1431 | * @returns fdto on success, -1 and errno on failure.
|
|---|
| 1432 | * @param pfdtab The file descriptor table.
|
|---|
| 1433 | * @param fdfrom The descriptor to move.
|
|---|
| 1434 | * @param fdto Where to move it.
|
|---|
| 1435 | */
|
|---|
| 1436 | int shfile_movefd(shfdtab *pfdtab, int fdfrom, int fdto)
|
|---|
| 1437 | {
|
|---|
| 1438 | #ifdef SHFILE_IN_USE
|
|---|
| 1439 | int rc;
|
|---|
| 1440 | shmtxtmp tmp;
|
|---|
| 1441 | shfile *file = shfile_get(pfdtab, fdfrom, &tmp);
|
|---|
| 1442 | if (file)
|
|---|
| 1443 | {
|
|---|
| 1444 | /* prepare the new entry */
|
|---|
| 1445 | if (fdto >= (int)pfdtab->size)
|
|---|
| 1446 | shfile_grow_tab_locked(pfdtab, fdto);
|
|---|
| 1447 | if (fdto < (int)pfdtab->size)
|
|---|
| 1448 | {
|
|---|
| 1449 | if (pfdtab->tab[fdto].fd != -1)
|
|---|
| 1450 | shfile_native_close(pfdtab->tab[fdto].native, pfdtab->tab[fdto].oflags);
|
|---|
| 1451 |
|
|---|
| 1452 | /* setup the target. */
|
|---|
| 1453 | pfdtab->tab[fdto].fd = fdto;
|
|---|
| 1454 | pfdtab->tab[fdto].oflags = file->oflags;
|
|---|
| 1455 | pfdtab->tab[fdto].shflags = file->shflags;
|
|---|
| 1456 | pfdtab->tab[fdto].native = file->native;
|
|---|
| 1457 |
|
|---|
| 1458 | /* close the source. */
|
|---|
| 1459 | file->fd = -1;
|
|---|
| 1460 | file->oflags = 0;
|
|---|
| 1461 | file->shflags = 0;
|
|---|
| 1462 | file->native = -1;
|
|---|
| 1463 |
|
|---|
| 1464 | rc = fdto;
|
|---|
| 1465 | }
|
|---|
| 1466 | else
|
|---|
| 1467 | {
|
|---|
| 1468 | errno = EMFILE;
|
|---|
| 1469 | rc = -1;
|
|---|
| 1470 | }
|
|---|
| 1471 |
|
|---|
| 1472 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1473 | }
|
|---|
| 1474 | else
|
|---|
| 1475 | rc = -1;
|
|---|
| 1476 | return rc;
|
|---|
| 1477 |
|
|---|
| 1478 | #else
|
|---|
| 1479 | int fdnew = dup2(fdfrom, fdto);
|
|---|
| 1480 | if (fdnew >= 0)
|
|---|
| 1481 | close(fdfrom);
|
|---|
| 1482 | return fdnew;
|
|---|
| 1483 | #endif
|
|---|
| 1484 | }
|
|---|
| 1485 |
|
|---|
| 1486 | /**
|
|---|
| 1487 | * Move the file descriptor to somewhere at @a fdMin or above.
|
|---|
| 1488 | *
|
|---|
| 1489 | * @returns the new file descriptor success, -1 and errno on failure.
|
|---|
| 1490 | * @param pfdtab The file descriptor table.
|
|---|
| 1491 | * @param fdfrom The descriptor to move.
|
|---|
| 1492 | * @param fdMin The minimum descriptor.
|
|---|
| 1493 | */
|
|---|
| 1494 | int shfile_movefd_above(shfdtab *pfdtab, int fdfrom, int fdMin)
|
|---|
| 1495 | {
|
|---|
| 1496 | #ifdef SHFILE_IN_USE
|
|---|
| 1497 | int fdto;
|
|---|
| 1498 | shmtxtmp tmp;
|
|---|
| 1499 | shfile *file = shfile_get(pfdtab, fdfrom, &tmp);
|
|---|
| 1500 | if (file)
|
|---|
| 1501 | {
|
|---|
| 1502 | /* find a new place */
|
|---|
| 1503 | int i;
|
|---|
| 1504 | fdto = -1;
|
|---|
| 1505 | for (i = fdMin; (unsigned)i < pfdtab->size; i++)
|
|---|
| 1506 | if (pfdtab->tab[i].fd == -1)
|
|---|
| 1507 | {
|
|---|
| 1508 | fdto = i;
|
|---|
| 1509 | break;
|
|---|
| 1510 | }
|
|---|
| 1511 | if (fdto == -1)
|
|---|
| 1512 | fdto = shfile_grow_tab_locked(pfdtab, fdMin);
|
|---|
| 1513 | if (fdto != -1)
|
|---|
| 1514 | {
|
|---|
| 1515 | /* setup the target. */
|
|---|
| 1516 | pfdtab->tab[fdto].fd = fdto;
|
|---|
| 1517 | pfdtab->tab[fdto].oflags = file->oflags;
|
|---|
| 1518 | pfdtab->tab[fdto].shflags = file->shflags;
|
|---|
| 1519 | pfdtab->tab[fdto].native = file->native;
|
|---|
| 1520 |
|
|---|
| 1521 | /* close the source. */
|
|---|
| 1522 | file->fd = -1;
|
|---|
| 1523 | file->oflags = 0;
|
|---|
| 1524 | file->shflags = 0;
|
|---|
| 1525 | file->native = -1;
|
|---|
| 1526 | }
|
|---|
| 1527 | else
|
|---|
| 1528 | {
|
|---|
| 1529 | errno = EMFILE;
|
|---|
| 1530 | fdto = -1;
|
|---|
| 1531 | }
|
|---|
| 1532 |
|
|---|
| 1533 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1534 | }
|
|---|
| 1535 | else
|
|---|
| 1536 | fdto = -1;
|
|---|
| 1537 | return fdto;
|
|---|
| 1538 |
|
|---|
| 1539 | #else
|
|---|
| 1540 | int fdnew = fcntl(fdfrom, F_DUPFD, fdMin);
|
|---|
| 1541 | if (fdnew >= 0)
|
|---|
| 1542 | close(fdfrom);
|
|---|
| 1543 | return fdnew;
|
|---|
| 1544 | #endif
|
|---|
| 1545 | }
|
|---|
| 1546 |
|
|---|
| 1547 | /**
|
|---|
| 1548 | * close().
|
|---|
| 1549 | */
|
|---|
| 1550 | int shfile_close(shfdtab *pfdtab, unsigned fd)
|
|---|
| 1551 | {
|
|---|
| 1552 | int rc;
|
|---|
| 1553 | #ifdef SHFILE_IN_USE
|
|---|
| 1554 | shmtxtmp tmp;
|
|---|
| 1555 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 1556 | if (file)
|
|---|
| 1557 | {
|
|---|
| 1558 | shfile_native_close(file->native, file->oflags);
|
|---|
| 1559 |
|
|---|
| 1560 | file->fd = -1;
|
|---|
| 1561 | file->oflags = 0;
|
|---|
| 1562 | file->shflags = 0;
|
|---|
| 1563 | file->native = -1;
|
|---|
| 1564 |
|
|---|
| 1565 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1566 | rc = 0;
|
|---|
| 1567 | }
|
|---|
| 1568 | else
|
|---|
| 1569 | rc = -1;
|
|---|
| 1570 |
|
|---|
| 1571 | #else
|
|---|
| 1572 | rc = close(fd);
|
|---|
| 1573 | #endif
|
|---|
| 1574 |
|
|---|
| 1575 | TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
|
|---|
| 1576 | return rc;
|
|---|
| 1577 | }
|
|---|
| 1578 |
|
|---|
| 1579 | /**
|
|---|
| 1580 | * read().
|
|---|
| 1581 | */
|
|---|
| 1582 | long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
|
|---|
| 1583 | {
|
|---|
| 1584 | long rc;
|
|---|
| 1585 | #ifdef SHFILE_IN_USE
|
|---|
| 1586 | shmtxtmp tmp;
|
|---|
| 1587 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 1588 | if (file)
|
|---|
| 1589 | {
|
|---|
| 1590 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1591 | DWORD dwRead = 0;
|
|---|
| 1592 | if (ReadFile((HANDLE)file->native, buf, (DWORD)len, &dwRead, NULL))
|
|---|
| 1593 | rc = dwRead;
|
|---|
| 1594 | else
|
|---|
| 1595 | rc = shfile_dos2errno(GetLastError());
|
|---|
| 1596 | # else
|
|---|
| 1597 | rc = read(file->native, buf, len);
|
|---|
| 1598 | # endif
|
|---|
| 1599 |
|
|---|
| 1600 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1601 | }
|
|---|
| 1602 | else
|
|---|
| 1603 | rc = -1;
|
|---|
| 1604 |
|
|---|
| 1605 | #else
|
|---|
| 1606 | rc = read(fd, buf, len);
|
|---|
| 1607 | #endif
|
|---|
| 1608 | return rc;
|
|---|
| 1609 | }
|
|---|
| 1610 |
|
|---|
| 1611 | /**
|
|---|
| 1612 | * write().
|
|---|
| 1613 | */
|
|---|
| 1614 | long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
|
|---|
| 1615 | {
|
|---|
| 1616 | long rc;
|
|---|
| 1617 | #ifdef SHFILE_IN_USE
|
|---|
| 1618 | shmtxtmp tmp;
|
|---|
| 1619 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 1620 | if (file)
|
|---|
| 1621 | {
|
|---|
| 1622 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1623 | DWORD dwWritten = 0;
|
|---|
| 1624 | if (WriteFile((HANDLE)file->native, buf, (DWORD)len, &dwWritten, NULL))
|
|---|
| 1625 | rc = dwWritten;
|
|---|
| 1626 | else
|
|---|
| 1627 | rc = shfile_dos2errno(GetLastError());
|
|---|
| 1628 | # else
|
|---|
| 1629 | rc = write(file->native, buf, len);
|
|---|
| 1630 | # endif
|
|---|
| 1631 |
|
|---|
| 1632 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1633 | }
|
|---|
| 1634 | else
|
|---|
| 1635 | rc = -1;
|
|---|
| 1636 |
|
|---|
| 1637 | # ifdef DEBUG
|
|---|
| 1638 | if (fd != shthread_get_shell()->tracefd)
|
|---|
| 1639 | TRACE2((NULL, "shfile_write(%d,,%d) -> %d [%d]\n", fd, len, rc, errno));
|
|---|
| 1640 | # endif
|
|---|
| 1641 |
|
|---|
| 1642 | #else
|
|---|
| 1643 | if (fd != shthread_get_shell()->tracefd)
|
|---|
| 1644 | {
|
|---|
| 1645 | int iSavedErrno = errno;
|
|---|
| 1646 | struct stat s;
|
|---|
| 1647 | int x;
|
|---|
| 1648 | x = fstat(fd, &s);
|
|---|
| 1649 | TRACE2((NULL, "shfile_write(%d) - %lu bytes (%d) - pos %lu - before; %o\n",
|
|---|
| 1650 | fd, (long)s.st_size, x, (long)lseek(fd, 0, SEEK_CUR), s.st_mode ));
|
|---|
| 1651 | K_NOREF(x);
|
|---|
| 1652 | errno = iSavedErrno;
|
|---|
| 1653 | }
|
|---|
| 1654 |
|
|---|
| 1655 | rc = write(fd, buf, len);
|
|---|
| 1656 | #endif
|
|---|
| 1657 | return rc;
|
|---|
| 1658 | }
|
|---|
| 1659 |
|
|---|
| 1660 | /**
|
|---|
| 1661 | * lseek().
|
|---|
| 1662 | */
|
|---|
| 1663 | long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
|
|---|
| 1664 | {
|
|---|
| 1665 | long rc;
|
|---|
| 1666 | #ifdef SHFILE_IN_USE
|
|---|
| 1667 | shmtxtmp tmp;
|
|---|
| 1668 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 1669 | if (file)
|
|---|
| 1670 | {
|
|---|
| 1671 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1672 | assert(SEEK_SET == FILE_BEGIN);
|
|---|
| 1673 | assert(SEEK_CUR == FILE_CURRENT);
|
|---|
| 1674 | assert(SEEK_END == FILE_END);
|
|---|
| 1675 | rc = SetFilePointer((HANDLE)file->native, off, NULL, whench);
|
|---|
| 1676 | if (rc == INVALID_SET_FILE_POINTER)
|
|---|
| 1677 | rc = shfile_dos2errno(GetLastError());
|
|---|
| 1678 | # else
|
|---|
| 1679 | rc = lseek(file->native, off, whench);
|
|---|
| 1680 | # endif
|
|---|
| 1681 |
|
|---|
| 1682 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1683 | }
|
|---|
| 1684 | else
|
|---|
| 1685 | rc = -1;
|
|---|
| 1686 |
|
|---|
| 1687 | #else
|
|---|
| 1688 | rc = lseek(fd, off, whench);
|
|---|
| 1689 | #endif
|
|---|
| 1690 |
|
|---|
| 1691 | return rc;
|
|---|
| 1692 | }
|
|---|
| 1693 |
|
|---|
| 1694 | int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
|
|---|
| 1695 | {
|
|---|
| 1696 | int rc;
|
|---|
| 1697 | #ifdef SHFILE_IN_USE
|
|---|
| 1698 | shmtxtmp tmp;
|
|---|
| 1699 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 1700 | if (file)
|
|---|
| 1701 | {
|
|---|
| 1702 | switch (cmd)
|
|---|
| 1703 | {
|
|---|
| 1704 | case F_GETFL:
|
|---|
| 1705 | rc = file->oflags;
|
|---|
| 1706 | break;
|
|---|
| 1707 |
|
|---|
| 1708 | case F_SETFL:
|
|---|
| 1709 | {
|
|---|
| 1710 | unsigned mask = O_NONBLOCK | O_APPEND | O_BINARY | O_TEXT;
|
|---|
| 1711 | # ifdef O_DIRECT
|
|---|
| 1712 | mask |= O_DIRECT;
|
|---|
| 1713 | # endif
|
|---|
| 1714 | # ifdef O_ASYNC
|
|---|
| 1715 | mask |= O_ASYNC;
|
|---|
| 1716 | # endif
|
|---|
| 1717 | # ifdef O_SYNC
|
|---|
| 1718 | mask |= O_SYNC;
|
|---|
| 1719 | # endif
|
|---|
| 1720 | if ((file->oflags & mask) == (arg & mask))
|
|---|
| 1721 | rc = 0;
|
|---|
| 1722 | else
|
|---|
| 1723 | {
|
|---|
| 1724 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1725 | assert(0);
|
|---|
| 1726 | errno = EINVAL;
|
|---|
| 1727 | rc = -1;
|
|---|
| 1728 | # else
|
|---|
| 1729 | rc = fcntl(file->native, F_SETFL, arg);
|
|---|
| 1730 | if (rc != -1)
|
|---|
| 1731 | file->oflags = (file->oflags & ~mask) | (arg & mask);
|
|---|
| 1732 | # endif
|
|---|
| 1733 | }
|
|---|
| 1734 | break;
|
|---|
| 1735 | }
|
|---|
| 1736 |
|
|---|
| 1737 | case F_DUPFD:
|
|---|
| 1738 | {
|
|---|
| 1739 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1740 | HANDLE hNew = INVALID_HANDLE_VALUE;
|
|---|
| 1741 | if (DuplicateHandle(GetCurrentProcess(),
|
|---|
| 1742 | (HANDLE)file->native,
|
|---|
| 1743 | GetCurrentProcess(),
|
|---|
| 1744 | &hNew,
|
|---|
| 1745 | 0,
|
|---|
| 1746 | FALSE /* bInheritHandle */,
|
|---|
| 1747 | DUPLICATE_SAME_ACCESS))
|
|---|
| 1748 | rc = shfile_insert(pfdtab, (intptr_t)hNew, file->oflags, file->shflags, arg, "shfile_fcntl");
|
|---|
| 1749 | else
|
|---|
| 1750 | rc = shfile_dos2errno(GetLastError());
|
|---|
| 1751 | # else
|
|---|
| 1752 | int nativeNew = fcntl(file->native, F_DUPFD, SHFILE_UNIX_MIN_FD);
|
|---|
| 1753 | if (nativeNew != -1)
|
|---|
| 1754 | rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg, "shfile_fcntl");
|
|---|
| 1755 | else
|
|---|
| 1756 | rc = -1;
|
|---|
| 1757 | # endif
|
|---|
| 1758 | break;
|
|---|
| 1759 | }
|
|---|
| 1760 |
|
|---|
| 1761 | default:
|
|---|
| 1762 | errno = -EINVAL;
|
|---|
| 1763 | rc = -1;
|
|---|
| 1764 | break;
|
|---|
| 1765 | }
|
|---|
| 1766 |
|
|---|
| 1767 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1768 | }
|
|---|
| 1769 | else
|
|---|
| 1770 | rc = -1;
|
|---|
| 1771 |
|
|---|
| 1772 | #else
|
|---|
| 1773 | rc = fcntl(fd, cmd, arg);
|
|---|
| 1774 | #endif
|
|---|
| 1775 |
|
|---|
| 1776 | switch (cmd)
|
|---|
| 1777 | {
|
|---|
| 1778 | case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
|
|---|
| 1779 | case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break;
|
|---|
| 1780 | case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFD,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
|
|---|
| 1781 | default: TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break;
|
|---|
| 1782 | }
|
|---|
| 1783 | return rc;
|
|---|
| 1784 | }
|
|---|
| 1785 |
|
|---|
| 1786 | int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
|---|
| 1787 | {
|
|---|
| 1788 | #ifdef SHFILE_IN_USE
|
|---|
| 1789 | char abspath[SHFILE_MAX_PATH];
|
|---|
| 1790 | int rc;
|
|---|
| 1791 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
|---|
| 1792 | if (!rc)
|
|---|
| 1793 | {
|
|---|
| 1794 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1795 | int dir_slash = shfile_trailing_slash_hack(abspath);
|
|---|
| 1796 | rc = stat(abspath, pst); /** @todo re-implement stat. */
|
|---|
| 1797 | if (!rc && dir_slash && !S_ISDIR(pst->st_mode))
|
|---|
| 1798 | {
|
|---|
| 1799 | rc = -1;
|
|---|
| 1800 | errno = ENOTDIR;
|
|---|
| 1801 | }
|
|---|
| 1802 | # else
|
|---|
| 1803 | rc = stat(abspath, pst);
|
|---|
| 1804 | # endif
|
|---|
| 1805 | }
|
|---|
| 1806 | TRACE2((NULL, "shfile_stat(,%s,) -> %d [%d]\n", path, rc, errno));
|
|---|
| 1807 | return rc;
|
|---|
| 1808 | #else
|
|---|
| 1809 | return stat(path, pst);
|
|---|
| 1810 | #endif
|
|---|
| 1811 | }
|
|---|
| 1812 |
|
|---|
| 1813 | int shfile_lstat(shfdtab *pfdtab, const char *path, struct stat *pst)
|
|---|
| 1814 | {
|
|---|
| 1815 | int rc;
|
|---|
| 1816 | #ifdef SHFILE_IN_USE
|
|---|
| 1817 | char abspath[SHFILE_MAX_PATH];
|
|---|
| 1818 |
|
|---|
| 1819 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
|---|
| 1820 | if (!rc)
|
|---|
| 1821 | {
|
|---|
| 1822 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1823 | int dir_slash = shfile_trailing_slash_hack(abspath);
|
|---|
| 1824 | rc = stat(abspath, pst); /** @todo re-implement stat. */
|
|---|
| 1825 | if (!rc && dir_slash && !S_ISDIR(pst->st_mode))
|
|---|
| 1826 | {
|
|---|
| 1827 | rc = -1;
|
|---|
| 1828 | errno = ENOTDIR;
|
|---|
| 1829 | }
|
|---|
| 1830 | # else
|
|---|
| 1831 | rc = lstat(abspath, pst);
|
|---|
| 1832 | # endif
|
|---|
| 1833 | }
|
|---|
| 1834 | #else
|
|---|
| 1835 | rc = stat(path, pst);
|
|---|
| 1836 | #endif
|
|---|
| 1837 | TRACE2((NULL, "shfile_stat(,%s,) -> %d [%d]\n", path, rc, errno));
|
|---|
| 1838 | return rc;
|
|---|
| 1839 | }
|
|---|
| 1840 |
|
|---|
| 1841 | /**
|
|---|
| 1842 | * chdir().
|
|---|
| 1843 | */
|
|---|
| 1844 | int shfile_chdir(shfdtab *pfdtab, const char *path)
|
|---|
| 1845 | {
|
|---|
| 1846 | int rc;
|
|---|
| 1847 | #ifdef SHFILE_IN_USE
|
|---|
| 1848 | shinstance *psh = shthread_get_shell();
|
|---|
| 1849 | char abspath[SHFILE_MAX_PATH];
|
|---|
| 1850 |
|
|---|
| 1851 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
|---|
| 1852 | if (!rc)
|
|---|
| 1853 | {
|
|---|
| 1854 | char *abspath_copy = sh_strdup(psh, abspath);
|
|---|
| 1855 | char *free_me = abspath_copy;
|
|---|
| 1856 | rc = chdir(abspath);
|
|---|
| 1857 | if (!rc)
|
|---|
| 1858 | {
|
|---|
| 1859 | shmtxtmp tmp;
|
|---|
| 1860 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 1861 |
|
|---|
| 1862 | shfile_fix_slashes(abspath_copy);
|
|---|
| 1863 | free_me = pfdtab->cwd;
|
|---|
| 1864 | pfdtab->cwd = abspath_copy;
|
|---|
| 1865 |
|
|---|
| 1866 | shmtx_leave(&pfdtab->mtx, &tmp);
|
|---|
| 1867 | }
|
|---|
| 1868 | sh_free(psh, free_me);
|
|---|
| 1869 | }
|
|---|
| 1870 | else
|
|---|
| 1871 | rc = -1;
|
|---|
| 1872 | #else
|
|---|
| 1873 | rc = chdir(path);
|
|---|
| 1874 | #endif
|
|---|
| 1875 |
|
|---|
| 1876 | TRACE2((NULL, "shfile_chdir(,%s) -> %d [%d]\n", path, rc, errno));
|
|---|
| 1877 | return rc;
|
|---|
| 1878 | }
|
|---|
| 1879 |
|
|---|
| 1880 | /**
|
|---|
| 1881 | * getcwd().
|
|---|
| 1882 | */
|
|---|
| 1883 | char *shfile_getcwd(shfdtab *pfdtab, char *buf, int size)
|
|---|
| 1884 | {
|
|---|
| 1885 | char *ret;
|
|---|
| 1886 | #ifdef SHFILE_IN_USE
|
|---|
| 1887 |
|
|---|
| 1888 | ret = NULL;
|
|---|
| 1889 | if (buf && !size)
|
|---|
| 1890 | errno = -EINVAL;
|
|---|
| 1891 | else
|
|---|
| 1892 | {
|
|---|
| 1893 | size_t cwd_size;
|
|---|
| 1894 | shmtxtmp tmp;
|
|---|
| 1895 | shmtx_enter(&pfdtab->mtx, &tmp);
|
|---|
| 1896 |
|
|---|
| 1897 | cwd_size = strlen(pfdtab->cwd) + 1;
|
|---|
| 1898 | if (buf)
|
|---|
| 1899 | {
|
|---|
| 1900 | if (cwd_size <= (size_t)size)
|
|---|
| 1901 | ret = memcpy(buf, pfdtab->cwd, cwd_size);
|
|---|
| 1902 | else
|
|---|
| 1903 | errno = ERANGE;
|
|---|
| 1904 | }
|
|---|
| 1905 | else
|
|---|
| 1906 | {
|
|---|
| 1907 | if ((size_t)size < cwd_size)
|
|---|
| 1908 | size = (int)cwd_size;
|
|---|
| 1909 | ret = sh_malloc(shthread_get_shell(), size);
|
|---|
| 1910 | if (ret)
|
|---|
| 1911 | ret = memcpy(ret, pfdtab->cwd, cwd_size);
|
|---|
| 1912 | else
|
|---|
| 1913 | errno = ENOMEM;
|
|---|
| 1914 | }
|
|---|
| 1915 |
|
|---|
| 1916 | shmtx_leave(&pfdtab->mtx, &tmp);
|
|---|
| 1917 | }
|
|---|
| 1918 | #else
|
|---|
| 1919 | ret = getcwd(buf, size);
|
|---|
| 1920 | #endif
|
|---|
| 1921 |
|
|---|
| 1922 | TRACE2((NULL, "shfile_getcwd(,%p,%d) -> %s [%d]\n", buf, size, ret, errno));
|
|---|
| 1923 | return ret;
|
|---|
| 1924 | }
|
|---|
| 1925 |
|
|---|
| 1926 | /**
|
|---|
| 1927 | * access().
|
|---|
| 1928 | */
|
|---|
| 1929 | int shfile_access(shfdtab *pfdtab, const char *path, int type)
|
|---|
| 1930 | {
|
|---|
| 1931 | int rc;
|
|---|
| 1932 | #ifdef SHFILE_IN_USE
|
|---|
| 1933 | char abspath[SHFILE_MAX_PATH];
|
|---|
| 1934 |
|
|---|
| 1935 | rc = shfile_make_path(pfdtab, path, &abspath[0]);
|
|---|
| 1936 | if (!rc)
|
|---|
| 1937 | {
|
|---|
| 1938 | # ifdef _MSC_VER
|
|---|
| 1939 | if (type & X_OK)
|
|---|
| 1940 | type = (type & ~X_OK) | R_OK;
|
|---|
| 1941 | # endif
|
|---|
| 1942 | rc = access(abspath, type);
|
|---|
| 1943 | }
|
|---|
| 1944 | #else
|
|---|
| 1945 | # ifdef _MSC_VER
|
|---|
| 1946 | if (type & X_OK)
|
|---|
| 1947 | type = (type & ~X_OK) | R_OK;
|
|---|
| 1948 | # endif
|
|---|
| 1949 | rc = access(path, type);
|
|---|
| 1950 | #endif
|
|---|
| 1951 |
|
|---|
| 1952 | TRACE2((NULL, "shfile_access(,%s,%#x) -> %d [%d]\n", path, type, rc, errno));
|
|---|
| 1953 | return rc;
|
|---|
| 1954 | }
|
|---|
| 1955 |
|
|---|
| 1956 | /**
|
|---|
| 1957 | * isatty()
|
|---|
| 1958 | */
|
|---|
| 1959 | int shfile_isatty(shfdtab *pfdtab, int fd)
|
|---|
| 1960 | {
|
|---|
| 1961 | int rc;
|
|---|
| 1962 | #ifdef SHFILE_IN_USE
|
|---|
| 1963 | shmtxtmp tmp;
|
|---|
| 1964 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 1965 | if (file)
|
|---|
| 1966 | {
|
|---|
| 1967 | # if K_OS == K_OS_WINDOWS
|
|---|
| 1968 | rc = (file->shflags & SHFILE_FLAGS_TYPE_MASK) == SHFILE_FLAGS_TTY;
|
|---|
| 1969 | # else
|
|---|
| 1970 | rc = isatty(file->native);
|
|---|
| 1971 | # endif
|
|---|
| 1972 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 1973 | }
|
|---|
| 1974 | else
|
|---|
| 1975 | rc = 0;
|
|---|
| 1976 | #else
|
|---|
| 1977 | rc = isatty(fd);
|
|---|
| 1978 | #endif
|
|---|
| 1979 |
|
|---|
| 1980 | TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
|
|---|
| 1981 | return rc;
|
|---|
| 1982 | }
|
|---|
| 1983 |
|
|---|
| 1984 | /**
|
|---|
| 1985 | * fcntl F_SETFD / FD_CLOEXEC.
|
|---|
| 1986 | */
|
|---|
| 1987 | int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
|
|---|
| 1988 | {
|
|---|
| 1989 | int rc;
|
|---|
| 1990 | #ifdef SHFILE_IN_USE
|
|---|
| 1991 | shmtxtmp tmp;
|
|---|
| 1992 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 1993 | if (file)
|
|---|
| 1994 | {
|
|---|
| 1995 | if (closeit)
|
|---|
| 1996 | file->shflags |= SHFILE_FLAGS_CLOSE_ON_EXEC;
|
|---|
| 1997 | else
|
|---|
| 1998 | file->shflags &= ~SHFILE_FLAGS_CLOSE_ON_EXEC;
|
|---|
| 1999 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 2000 | rc = 0;
|
|---|
| 2001 | }
|
|---|
| 2002 | else
|
|---|
| 2003 | rc = -1;
|
|---|
| 2004 | #else
|
|---|
| 2005 | rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
|
|---|
| 2006 | | (closeit ? FD_CLOEXEC : 0));
|
|---|
| 2007 | #endif
|
|---|
| 2008 |
|
|---|
| 2009 | TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
|
|---|
| 2010 | return rc;
|
|---|
| 2011 | }
|
|---|
| 2012 |
|
|---|
| 2013 | /**
|
|---|
| 2014 | * Sets the SHFILE_FLAGS_TRACE flag.
|
|---|
| 2015 | */
|
|---|
| 2016 | int shfile_set_trace(shfdtab *pfdtab, int fd)
|
|---|
| 2017 | {
|
|---|
| 2018 | int rc;
|
|---|
| 2019 | #ifdef SHFILE_IN_USE
|
|---|
| 2020 | shmtxtmp tmp;
|
|---|
| 2021 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 2022 | if (file)
|
|---|
| 2023 | {
|
|---|
| 2024 | file->shflags |= SHFILE_FLAGS_TRACE;
|
|---|
| 2025 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 2026 | rc = 0;
|
|---|
| 2027 | }
|
|---|
| 2028 | else
|
|---|
| 2029 | rc = -1;
|
|---|
| 2030 | #else
|
|---|
| 2031 | rc = 0;
|
|---|
| 2032 | #endif
|
|---|
| 2033 |
|
|---|
| 2034 | TRACE2((NULL, "shfile_set_trace(%d) -> %d\n", fd, rc));
|
|---|
| 2035 | return rc;
|
|---|
| 2036 | }
|
|---|
| 2037 |
|
|---|
| 2038 |
|
|---|
| 2039 | int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
|
|---|
| 2040 | {
|
|---|
| 2041 | int rc;
|
|---|
| 2042 | #ifdef SHFILE_IN_USE
|
|---|
| 2043 | shmtxtmp tmp;
|
|---|
| 2044 | shfile *file = shfile_get(pfdtab, fd, &tmp);
|
|---|
| 2045 | if (file)
|
|---|
| 2046 | {
|
|---|
| 2047 | # if K_OS == K_OS_WINDOWS
|
|---|
| 2048 | rc = -1;
|
|---|
| 2049 | errno = ENOSYS;
|
|---|
| 2050 | # else
|
|---|
| 2051 | rc = ioctl(file->native, request, buf);
|
|---|
| 2052 | # endif
|
|---|
| 2053 | shfile_put(pfdtab, file, &tmp);
|
|---|
| 2054 | }
|
|---|
| 2055 | else
|
|---|
| 2056 | rc = -1;
|
|---|
| 2057 | #else
|
|---|
| 2058 | rc = ioctl(fd, request, buf);
|
|---|
| 2059 | #endif
|
|---|
| 2060 |
|
|---|
| 2061 | TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
|
|---|
| 2062 | return rc;
|
|---|
| 2063 | }
|
|---|
| 2064 |
|
|---|
| 2065 |
|
|---|
| 2066 | mode_t shfile_get_umask(shfdtab *pfdtab)
|
|---|
| 2067 | {
|
|---|
| 2068 | /** @todo */
|
|---|
| 2069 | return 022;
|
|---|
| 2070 | }
|
|---|
| 2071 |
|
|---|
| 2072 | void shfile_set_umask(shfdtab *pfdtab, mode_t mask)
|
|---|
| 2073 | {
|
|---|
| 2074 | /** @todo */
|
|---|
| 2075 | (void)mask;
|
|---|
| 2076 | }
|
|---|
| 2077 |
|
|---|
| 2078 |
|
|---|
| 2079 |
|
|---|
| 2080 | shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
|
|---|
| 2081 | {
|
|---|
| 2082 | #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS
|
|---|
| 2083 | shdir *pdir = NULL;
|
|---|
| 2084 |
|
|---|
| 2085 | TRACE2((NULL, "shfile_opendir: dir='%s'\n", dir));
|
|---|
| 2086 | shfile_init_globals();
|
|---|
| 2087 | if (g_pfnNtQueryDirectoryFile)
|
|---|
| 2088 | {
|
|---|
| 2089 | char abspath[SHFILE_MAX_PATH];
|
|---|
| 2090 | if (shfile_make_path(pfdtab, dir, &abspath[0]) == 0)
|
|---|
| 2091 | {
|
|---|
| 2092 | HANDLE hFile;
|
|---|
| 2093 | SECURITY_ATTRIBUTES SecurityAttributes;
|
|---|
| 2094 |
|
|---|
| 2095 | SecurityAttributes.nLength = sizeof(SecurityAttributes);
|
|---|
| 2096 | SecurityAttributes.lpSecurityDescriptor = NULL;
|
|---|
| 2097 | SecurityAttributes.bInheritHandle = FALSE;
|
|---|
| 2098 |
|
|---|
| 2099 | hFile = CreateFileA(abspath,
|
|---|
| 2100 | GENERIC_READ,
|
|---|
| 2101 | FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|---|
| 2102 | &SecurityAttributes,
|
|---|
| 2103 | OPEN_EXISTING,
|
|---|
| 2104 | FILE_ATTRIBUTE_DIRECTORY | FILE_FLAG_BACKUP_SEMANTICS,
|
|---|
| 2105 | NULL /* hTemplateFile */);
|
|---|
| 2106 | if (hFile != INVALID_HANDLE_VALUE)
|
|---|
| 2107 | {
|
|---|
| 2108 | pdir = (shdir *)sh_malloc(shthread_get_shell(), sizeof(*pdir));
|
|---|
| 2109 | if (pdir)
|
|---|
| 2110 | {
|
|---|
| 2111 | pdir->pfdtab = pfdtab;
|
|---|
| 2112 | pdir->native = hFile;
|
|---|
| 2113 | pdir->off = ~(size_t)0;
|
|---|
| 2114 | }
|
|---|
| 2115 | else
|
|---|
| 2116 | CloseHandle(hFile);
|
|---|
| 2117 | }
|
|---|
| 2118 | else
|
|---|
| 2119 | {
|
|---|
| 2120 | errno = shfile_dos2errno(GetLastError());
|
|---|
| 2121 | TRACE2((NULL, "shfile_opendir: CreateFileA(%s) -> %d/%d\n", abspath, GetLastError(), errno));
|
|---|
| 2122 | }
|
|---|
| 2123 | }
|
|---|
| 2124 | }
|
|---|
| 2125 | else
|
|---|
| 2126 | errno = ENOSYS;
|
|---|
| 2127 | return pdir;
|
|---|
| 2128 | #else
|
|---|
| 2129 | TRACE2((NULL, "shfile_opendir: dir='%s'\n", dir));
|
|---|
| 2130 | return (shdir *)opendir(dir);
|
|---|
| 2131 | #endif
|
|---|
| 2132 | }
|
|---|
| 2133 |
|
|---|
| 2134 | shdirent *shfile_readdir(struct shdir *pdir)
|
|---|
| 2135 | {
|
|---|
| 2136 | #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS
|
|---|
| 2137 | if (pdir)
|
|---|
| 2138 | {
|
|---|
| 2139 | NTSTATUS rcNt;
|
|---|
| 2140 |
|
|---|
| 2141 | if ( pdir->off == ~(size_t)0
|
|---|
| 2142 | || pdir->off + sizeof(MY_FILE_NAMES_INFORMATION) >= pdir->cb)
|
|---|
| 2143 | {
|
|---|
| 2144 | MY_IO_STATUS_BLOCK Ios;
|
|---|
| 2145 |
|
|---|
| 2146 | memset(&Ios, 0, sizeof(Ios));
|
|---|
| 2147 | rcNt = g_pfnNtQueryDirectoryFile(pdir->native,
|
|---|
| 2148 | NULL /*Event*/,
|
|---|
| 2149 | NULL /*ApcRoutine*/,
|
|---|
| 2150 | NULL /*ApcContext*/,
|
|---|
| 2151 | &Ios,
|
|---|
| 2152 | &pdir->buf[0],
|
|---|
| 2153 | sizeof(pdir->buf),
|
|---|
| 2154 | MY_FileNamesInformation,
|
|---|
| 2155 | FALSE /*ReturnSingleEntry*/,
|
|---|
| 2156 | NULL /*FileName*/,
|
|---|
| 2157 | pdir->off == ~(size_t)0 /*RestartScan*/);
|
|---|
| 2158 | if (rcNt >= 0 && rcNt != STATUS_PENDING)
|
|---|
| 2159 | {
|
|---|
| 2160 | pdir->cb = Ios.Information;
|
|---|
| 2161 | pdir->off = 0;
|
|---|
| 2162 | }
|
|---|
| 2163 | else if (rcNt == STATUS_NO_MORE_FILES)
|
|---|
| 2164 | errno = 0; /* wrong? */
|
|---|
| 2165 | else
|
|---|
| 2166 | shfile_nt2errno(rcNt);
|
|---|
| 2167 | }
|
|---|
| 2168 |
|
|---|
| 2169 | if ( pdir->off != ~(size_t)0
|
|---|
| 2170 | && pdir->off + sizeof(MY_FILE_NAMES_INFORMATION) <= pdir->cb)
|
|---|
| 2171 | {
|
|---|
| 2172 | PMY_FILE_NAMES_INFORMATION pcur = (PMY_FILE_NAMES_INFORMATION)&pdir->buf[pdir->off];
|
|---|
| 2173 | ANSI_STRING astr;
|
|---|
| 2174 | UNICODE_STRING ustr;
|
|---|
| 2175 |
|
|---|
| 2176 | astr.Length = astr.MaximumLength = sizeof(pdir->ent.name);
|
|---|
| 2177 | astr.Buffer = &pdir->ent.name[0];
|
|---|
| 2178 |
|
|---|
| 2179 | ustr.Length = ustr.MaximumLength = pcur->FileNameLength < ~(USHORT)0 ? (USHORT)pcur->FileNameLength : ~(USHORT)0;
|
|---|
| 2180 | ustr.Buffer = &pcur->FileName[0];
|
|---|
| 2181 |
|
|---|
| 2182 | rcNt = g_pfnRtlUnicodeStringToAnsiString(&astr, &ustr, 0/*AllocateDestinationString*/);
|
|---|
| 2183 | if (rcNt < 0)
|
|---|
| 2184 | sprintf(pdir->ent.name, "conversion-failed-%08x-rcNt=%08x-len=%u",
|
|---|
| 2185 | pcur->FileIndex, rcNt, pcur->FileNameLength);
|
|---|
| 2186 | if (pcur->NextEntryOffset)
|
|---|
| 2187 | pdir->off += pcur->NextEntryOffset;
|
|---|
| 2188 | else
|
|---|
| 2189 | pdir->off = pdir->cb;
|
|---|
| 2190 | return &pdir->ent;
|
|---|
| 2191 | }
|
|---|
| 2192 | }
|
|---|
| 2193 | else
|
|---|
| 2194 | errno = EINVAL;
|
|---|
| 2195 | return NULL;
|
|---|
| 2196 | #else
|
|---|
| 2197 | struct dirent *pde = readdir((DIR *)pdir);
|
|---|
| 2198 | return pde ? (shdirent *)&pde->d_name[0] : NULL;
|
|---|
| 2199 | #endif
|
|---|
| 2200 | }
|
|---|
| 2201 |
|
|---|
| 2202 | void shfile_closedir(struct shdir *pdir)
|
|---|
| 2203 | {
|
|---|
| 2204 | #if defined(SHFILE_IN_USE) && K_OS == K_OS_WINDOWS
|
|---|
| 2205 | if (pdir)
|
|---|
| 2206 | {
|
|---|
| 2207 | CloseHandle(pdir->native);
|
|---|
| 2208 | pdir->pfdtab = NULL;
|
|---|
| 2209 | pdir->native = INVALID_HANDLE_VALUE;
|
|---|
| 2210 | sh_free(shthread_get_shell(), pdir);
|
|---|
| 2211 | }
|
|---|
| 2212 | else
|
|---|
| 2213 | errno = EINVAL;
|
|---|
| 2214 | #else
|
|---|
| 2215 | closedir((DIR *)pdir);
|
|---|
| 2216 | #endif
|
|---|
| 2217 | }
|
|---|
| 2218 |
|
|---|