fs.c 16.6 KB
Newer Older
1 2
/* -----------------------------------------------------------------------------
 *
3
 * (c) Tamar Christina 2018-2019
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Windows I/O routines for file opening.
 *
 * NOTE: Only modify this file in utils/fs/ and rerun configure. Do not edit
 *       this file in any other directory as it will be overwritten.
 *
 * ---------------------------------------------------------------------------*/
#include "fs.h"
#include <stdio.h>

#if defined(_WIN32)

#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>

#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <wchar.h>
24 25
#include <sys/stat.h>
#include <sys/types.h>
26
#include <share.h>
27
#include <errno.h>
28

29 30 31 32 33 34 35 36 37 38
/* Duplicate a string, but in wide form. The caller is responsible for freeing
   the result. */
static wchar_t* FS(to_wide) (const char *path) {
  size_t len = mbstowcs (NULL, path, 0);
  wchar_t *w_path = malloc (sizeof (wchar_t) * (len + 1));
  mbstowcs (w_path, path, len);
  w_path[len] = L'\0';
  return w_path;
}

39 40
/* This function converts Windows paths between namespaces. More specifically
   It converts an explorer style path into a NT or Win32 namespace.
41
   This has several caveats but they are caveats that are native to Windows and
42 43 44 45
   not POSIX. See
   https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx.
   Anything else such as raw device paths we leave untouched.  The main benefit
   of doing any of this is that we can break the MAX_PATH restriction and also
46 47 48 49 50
   access raw handles that we couldn't before.

   The resulting string is dynamically allocated and so callers are expected to
   free this string.  */
wchar_t* FS(create_device_name) (const wchar_t* filename) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  const wchar_t* win32_dev_namespace  = L"\\\\.\\";
  const wchar_t* win32_file_namespace = L"\\\\?\\";
  const wchar_t* nt_device_namespace  = L"\\Device\\";
  const wchar_t* unc_prefix           = L"UNC\\";
  const wchar_t* network_share        = L"\\\\";

  wchar_t* result = _wcsdup (filename);
  wchar_t ns[10] = {0};

  /* If the file is already in a native namespace don't change it.  */
  if (   wcsncmp (win32_dev_namespace , filename, 4) == 0
      || wcsncmp (win32_file_namespace, filename, 4) == 0
      || wcsncmp (nt_device_namespace , filename, 8) == 0)
    return result;

  /* Since we're using the lower level APIs we must normalize slashes now.  The
     Win32 API layer will no longer convert '/' into '\\' for us.  */
  for (size_t i = 0; i < wcslen (result); i++)
    {
      if (result[i] == L'/')
        result[i] = L'\\';
    }

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  /* We need to expand dos short paths as well.  */
  DWORD nResult = GetLongPathNameW (result, NULL, 0) + 1;
  wchar_t* temp = NULL;
  if (nResult > 1)
    {
      temp = _wcsdup (result);
      free (result);
      result = malloc (nResult * sizeof (wchar_t));
      if (GetLongPathNameW (temp, result, nResult) == 0)
        {
          result = memcpy (result, temp, wcslen (temp));
          goto cleanup;
        }
      free (temp);
    }

90 91
  /* Now resolve any . and .. in the path or subsequent API calls may fail since
     Win32 will no longer resolve them.  */
92 93 94
  nResult = GetFullPathNameW (result, 0, NULL, NULL) + 1;
  temp = _wcsdup (result);
  free (result);
95 96 97
  result = malloc (nResult * sizeof (wchar_t));
  if (GetFullPathNameW (temp, nResult, result, NULL) == 0)
    {
98
      result = memcpy (result, temp, wcslen (temp));
99 100 101 102 103
      goto cleanup;
    }

  free (temp);

Tamar Christina's avatar
Tamar Christina committed
104 105 106
  int startOffset = 0;
  /* When remapping a network share, \\foo needs to become
     \\?\UNC\foo and not \\?\\UNC\\foo which is an invalid path.  */
107 108 109 110 111 112
  if (wcsncmp (network_share, result, 2) == 0)
    {
      if (swprintf (ns, 10, L"%ls%ls", win32_file_namespace, unc_prefix) <= 0)
        {
          goto cleanup;
        }
Tamar Christina's avatar
Tamar Christina committed
113
      startOffset = 2;
114 115 116 117 118 119 120
    }
  else if (swprintf (ns, 10, L"%ls", win32_file_namespace) <= 0)
    {
      goto cleanup;
    }

  /* Create new string.  */
Tamar Christina's avatar
Tamar Christina committed
121 122
  int bLen = wcslen (result) + wcslen (ns) + 1 - startOffset;
  temp = _wcsdup (result + startOffset);
123
  free (result);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  result = malloc (bLen * sizeof (wchar_t));
  if (swprintf (result, bLen, L"%ls%ls", ns, temp) <= 0)
    {
      goto cleanup;
    }

  free (temp);

  return result;

cleanup:
  free (temp);
  free (result);
  return NULL;
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static int setErrNoFromWin32Error (void);
/* Sets errno to the right error value and returns -1 to indicate the failure.
   This function should only be called when the creation of the fd actually
   failed and you want to return -1 for the fd.  */
static
int setErrNoFromWin32Error () {
  switch (GetLastError()) {
    case ERROR_SUCCESS:
      errno = 0;
      break;
    case ERROR_ACCESS_DENIED:
    case ERROR_FILE_READ_ONLY:
      errno = EACCES;
      break;
    case ERROR_FILE_NOT_FOUND:
    case ERROR_PATH_NOT_FOUND:
      errno = ENOENT;
      break;
    case ERROR_FILE_EXISTS:
      errno = EEXIST;
      break;
    case ERROR_NOT_ENOUGH_MEMORY:
    case ERROR_OUTOFMEMORY:
      errno = ENOMEM;
      break;
    case ERROR_INVALID_HANDLE:
      errno = EBADF;
      break;
    case ERROR_INVALID_FUNCTION:
      errno = EFAULT;
      break;
    default:
      errno = EINVAL;
      break;
  }
  return -1;
}


#define HAS_FLAG(a,b) (((a) & (b)) == (b))
180 181 182 183

int FS(swopen) (const wchar_t* filename, int oflag, int shflag, int pmode)
{
  /* Construct access mode.  */
184
  /* https://docs.microsoft.com/en-us/windows/win32/fileio/file-access-rights-constants  */
185 186
  DWORD dwDesiredAccess = 0;
  if (HAS_FLAG (oflag, _O_RDONLY))
187
    dwDesiredAccess |= GENERIC_READ;
188
  if (HAS_FLAG (oflag, _O_RDWR))
189 190 191 192 193
    dwDesiredAccess |= GENERIC_WRITE | GENERIC_READ;
  if (HAS_FLAG (oflag, _O_WRONLY))
    dwDesiredAccess |= GENERIC_WRITE;
  if (HAS_FLAG (oflag, _O_APPEND))
    dwDesiredAccess |= FILE_APPEND_DATA;
194 195

  /* Construct shared mode.  */
196 197
  /* https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants */
  DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  if (HAS_FLAG (shflag, _SH_DENYRW))
    dwShareMode &= ~(FILE_SHARE_READ | FILE_SHARE_WRITE);
  if (HAS_FLAG (shflag, _SH_DENYWR))
    dwShareMode &= ~FILE_SHARE_WRITE;
  if (HAS_FLAG (shflag, _SH_DENYRD))
    dwShareMode &= ~FILE_SHARE_READ;
  if (HAS_FLAG (pmode, _S_IWRITE))
    dwShareMode |= FILE_SHARE_READ | FILE_SHARE_WRITE;
  if (HAS_FLAG (pmode, _S_IREAD))
    dwShareMode |= FILE_SHARE_READ;

  /* Override access mode with pmode if creating file.  */
  if (HAS_FLAG (oflag, _O_CREAT))
    {
      if (HAS_FLAG (pmode, _S_IWRITE))
        dwDesiredAccess |= FILE_GENERIC_WRITE;
      if (HAS_FLAG (pmode, _S_IREAD))
        dwDesiredAccess |= FILE_GENERIC_READ;
    }

  /* Create file disposition.  */
219 220
  /* https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea */
  DWORD dwCreationDisposition = 0;
221
  if (HAS_FLAG (oflag, (_O_CREAT | _O_EXCL)))
222 223 224 225 226 227 228 229 230 231 232 233 234
    dwCreationDisposition |= CREATE_NEW;
  else if (HAS_FLAG (oflag, _O_TRUNC | _O_CREAT))
    dwCreationDisposition |= CREATE_ALWAYS;
  else if (HAS_FLAG (oflag, _O_TRUNC) && !HAS_FLAG (oflag, O_RDONLY))
    dwCreationDisposition |= TRUNCATE_EXISTING;
  else if (HAS_FLAG (oflag, _O_APPEND | _O_CREAT))
    dwCreationDisposition |= OPEN_ALWAYS;
  else if (HAS_FLAG (oflag, _O_APPEND))
    dwCreationDisposition |= OPEN_EXISTING;
  else if (HAS_FLAG (oflag, _O_CREAT))
    dwCreationDisposition |= OPEN_ALWAYS;
  else
    dwCreationDisposition |= OPEN_EXISTING;
235 236 237 238 239

  /* Set file access attributes.  */
  DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
  if (HAS_FLAG (oflag, _O_RDONLY))
    dwFlagsAndAttributes |= 0; /* No special attribute.  */
240 241 242 243 244 245
  if (HAS_FLAG (oflag, _O_TEMPORARY))
    {
      dwFlagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
      dwShareMode |= FILE_SHARE_DELETE;
    }
  if (HAS_FLAG (oflag, _O_SHORT_LIVED))
246 247 248 249 250 251 252 253 254
    dwFlagsAndAttributes |= FILE_ATTRIBUTE_TEMPORARY;
  if (HAS_FLAG (oflag, _O_RANDOM))
    dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
  if (HAS_FLAG (oflag, _O_SEQUENTIAL))
    dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
  /* Flag is only valid on it's own.  */
  if (dwFlagsAndAttributes != FILE_ATTRIBUTE_NORMAL)
    dwFlagsAndAttributes &= ~FILE_ATTRIBUTE_NORMAL;

255 256 257 258 259
  /* Ensure we have shared read for files which are opened read-only. */
  if (HAS_FLAG (dwCreationDisposition, OPEN_EXISTING)
      && ((dwDesiredAccess & (GENERIC_WRITE|GENERIC_READ)) == GENERIC_READ))
    dwShareMode |= FILE_SHARE_READ;

260 261 262 263 264 265 266
  /* Set security attributes.  */
  SECURITY_ATTRIBUTES securityAttributes;
  ZeroMemory (&securityAttributes, sizeof(SECURITY_ATTRIBUTES));
  securityAttributes.bInheritHandle       = !(oflag & _O_NOINHERIT);
  securityAttributes.lpSecurityDescriptor = NULL;
  securityAttributes.nLength              = sizeof(SECURITY_ATTRIBUTES);

267
  wchar_t* _filename = FS(create_device_name) (filename);
268 269 270 271 272 273
  if (!_filename)
    return -1;

  HANDLE hResult
    = CreateFileW (_filename, dwDesiredAccess, dwShareMode, &securityAttributes,
                   dwCreationDisposition, dwFlagsAndAttributes, NULL);
274

275 276
  free (_filename);
  if (INVALID_HANDLE_VALUE == hResult)
277
    return setErrNoFromWin32Error ();
278 279 280 281 282 283

  /* Now we have a Windows handle, we have to convert it to an FD and apply
     the remaining flags.  */
  const int flag_mask = _O_APPEND | _O_RDONLY | _O_TEXT | _O_WTEXT;
  int fd = _open_osfhandle ((intptr_t)hResult, oflag & flag_mask);
  if (-1 == fd)
284
    return setErrNoFromWin32Error ();
285 286 287 288

  /* Finally we can change the mode to the requested one.  */
  const int mode_mask = _O_TEXT | _O_BINARY | _O_U16TEXT | _O_U8TEXT | _O_WTEXT;
  if ((oflag & mode_mask) && (-1 == _setmode (fd, oflag & mode_mask)))
289
    return setErrNoFromWin32Error ();
290 291 292 293

  return fd;
}

294
int FS(translate_mode) (const wchar_t* mode)
295
{
296
  int oflag = 0;
297 298
  int len = wcslen (mode);
  int i;
299
  #define IS_EXT(X) ((i < (len - 1)) && mode[i+1] == X)
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

  for (i = 0; i < len; i++)
    {
      switch (mode[i])
        {
          case L'a':
            if (IS_EXT (L'+'))
              oflag |= _O_RDWR | _O_CREAT | _O_APPEND;
            else
              oflag |= _O_WRONLY | _O_CREAT | _O_APPEND;
            break;
          case L'r':
            if (IS_EXT (L'+'))
              oflag |= _O_RDWR;
            else
              oflag |= _O_RDONLY;
            break;
          case L'w':
            if (IS_EXT (L'+'))
              oflag |= _O_RDWR | _O_CREAT | _O_TRUNC;
            else
              oflag |= _O_WRONLY | _O_CREAT | _O_TRUNC;
            break;
          case L'b':
            oflag |= _O_BINARY;
            break;
          case L't':
            oflag |= _O_TEXT;
            break;
          case L'c':
          case L'n':
            oflag |= 0;
            break;
          case L'S':
            oflag |= _O_SEQUENTIAL;
            break;
          case L'R':
            oflag |= _O_RANDOM;
            break;
          case L'T':
            oflag |= _O_SHORT_LIVED;
            break;
          case L'D':
            oflag |= _O_TEMPORARY;
            break;
          default:
            if (wcsncmp (mode, L"ccs=UNICODE", 11) == 0)
              oflag |= _O_WTEXT;
            else if (wcsncmp (mode, L"ccs=UTF-8", 9) == 0)
              oflag |= _O_U8TEXT;
            else if (wcsncmp (mode, L"ccs=UTF-16LE", 12) == 0)
              oflag |= _O_U16TEXT;
            else continue;
        }
    }
  #undef IS_EXT

357 358 359 360 361 362 363 364 365
  return oflag;
}

FILE *FS(fwopen) (const wchar_t* filename, const wchar_t* mode)
{
  int shflag = 0;
  int pmode  = 0;
  int oflag  = FS(translate_mode) (mode);

366
  int fd = FS(swopen) (filename, oflag, shflag, pmode);
367 368 369
  if (fd < 0)
    return NULL;

370 371 372 373 374 375
  FILE* file = _wfdopen (fd, mode);
  return file;
}

FILE *FS(fopen) (const char* filename, const char* mode)
{
376 377
  wchar_t * const w_filename = FS(to_wide) (filename);
  wchar_t * const w_mode = FS(to_wide) (mode);
378 379 380 381

  FILE *result = FS(fwopen) (w_filename, w_mode);
  free (w_filename);
  free (w_mode);
382 383 384 385 386 387

  return result;
}

int FS(sopen) (const char* filename, int oflag, int shflag, int pmode)
{
388
  wchar_t * const w_filename = FS(to_wide) (filename);
389 390 391 392 393 394 395 396
  int result = FS(swopen) (w_filename, oflag, shflag, pmode);
  free (w_filename);

  return result;
}

int FS(_stat) (const char *path, struct _stat *buffer)
{
397
  wchar_t * const w_path = FS(to_wide) (path);
398 399 400
  int result = FS(_wstat) (w_path, buffer);
  free (w_path);

401 402
  return result;
}
403 404 405

int FS(_stat64) (const char *path, struct __stat64 *buffer)
{
406
  wchar_t * const w_path = FS(to_wide) (path);
407 408 409 410 411 412 413 414
  int result = FS(_wstat64) (w_path, buffer);
  free (w_path);

  return result;
}

static __time64_t ftToPosix(FILETIME ft)
{
415
  // takes the last modified date
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  LARGE_INTEGER date, adjust;
  date.HighPart = ft.dwHighDateTime;
  date.LowPart = ft.dwLowDateTime;

  /* 100-nanoseconds = milliseconds * 10000.  */
  /* A UNIX timestamp contains the number of seconds from Jan 1, 1970, while the
     FILETIME documentation says: Contains a 64-bit value representing the
     number of 100-nanosecond intervals since January 1, 1601 (UTC).

     Between Jan 1, 1601 and Jan 1, 1970 there are 11644473600 seconds */
  adjust.QuadPart = 11644473600000 * 10000;

  /* removes the diff between 1970 and 1601.  */
  date.QuadPart -= adjust.QuadPart;

  /* converts back from 100-nanoseconds to seconds.  */
  return (__time64_t)date.QuadPart / 10000000;
}

int FS(_wstat) (const wchar_t *path, struct _stat *buffer)
{
  ZeroMemory (buffer, sizeof (struct _stat));
  wchar_t* _path = FS(create_device_name) (path);
  if (!_path)
    return -1;

    /* Construct shared mode.  */
  DWORD dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
  DWORD dwDesiredAccess = FILE_READ_ATTRIBUTES;
  DWORD dwFlagsAndAttributes = FILE_FLAG_BACKUP_SEMANTICS;
  DWORD dwCreationDisposition = OPEN_EXISTING;

  SECURITY_ATTRIBUTES securityAttributes;
  ZeroMemory (&securityAttributes, sizeof(SECURITY_ATTRIBUTES));
  securityAttributes.bInheritHandle       = false;
  securityAttributes.lpSecurityDescriptor = NULL;
  securityAttributes.nLength              = sizeof(SECURITY_ATTRIBUTES);

  HANDLE hResult
    = CreateFileW (_path, dwDesiredAccess, dwShareMode, &securityAttributes,
                   dwCreationDisposition, dwFlagsAndAttributes, NULL);

  if (INVALID_HANDLE_VALUE == hResult)
    {
      free (_path);
      return setErrNoFromWin32Error ();
    }

  WIN32_FILE_ATTRIBUTE_DATA finfo;
  ZeroMemory (&finfo, sizeof (WIN32_FILE_ATTRIBUTE_DATA));
  if(!GetFileAttributesExW (_path, GetFileExInfoStandard, &finfo))
    {
      free (_path);
      CloseHandle (hResult);
      return setErrNoFromWin32Error ();
    }

  unsigned short mode = _S_IREAD;

  if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    mode |= (_S_IFDIR | _S_IEXEC);
  else
  {
    mode |= _S_IFREG;
    DWORD type;
    if (GetBinaryTypeW (_path, &type))
      mode |= _S_IEXEC;
  }

  if (!(finfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
    mode |= _S_IWRITE;

  buffer->st_mode  = mode;
  buffer->st_nlink = 1;
  buffer->st_size  = ((uint64_t)finfo.nFileSizeHigh << 32) + finfo.nFileSizeLow;
  buffer->st_atime = ftToPosix (finfo.ftLastAccessTime);
  buffer->st_mtime = buffer->st_ctime = ftToPosix (finfo.ftLastWriteTime);
  free (_path);
  CloseHandle (hResult);
  return 0;
}

int FS(_wstat64) (const wchar_t *path, struct __stat64 *buffer)
{
  struct _stat buf;
  ZeroMemory (buffer, sizeof (struct __stat64));

  int result = FS(_wstat) (path, &buf);

  buffer->st_mode = buf.st_mode;
  buffer->st_nlink = 1;
  buffer->st_size = buf.st_size;
  buffer->st_atime = buf.st_atime;
  buffer->st_mtime = buf.st_mtime;

  return result;
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
int FS(_wrename) (const wchar_t *from, const wchar_t *to)
{
  wchar_t* const _from = FS(create_device_name) (from);
  if (!_from)
    return -1;

  wchar_t* const _to = FS(create_device_name) (to);
  if (!_to)
    {
      free (_from);
      return -1;
    }

  if (MoveFileW(_from, _to) == 0) {
    free (_from);
    free (_to);
    return setErrNoFromWin32Error ();
  }


  free (_from);
  free (_to);
  return 0;
}

int FS(rename) (const char *from, const char *to)
{
  wchar_t * const w_from = FS(to_wide) (from);
  wchar_t * const w_to = FS(to_wide) (to);
  int result = FS(_wrename) (w_from, w_to);
  free(w_from);
  free(w_to);

  return result;
}

int FS(unlink) (const char *filename)
{
  return FS(_unlink) (filename);
}

int FS(_unlink) (const char *filename)
{
  wchar_t * const w_filename = FS(to_wide) (filename);
  int result = FS(_wunlink) (w_filename);
  free(w_filename);

  return result;
}

int FS(_wunlink) (const wchar_t *filename)
{
  wchar_t* const _filename = FS(create_device_name) (filename);
  if (!_filename)
    return -1;

  if (DeleteFileW(_filename) == 0) {
    free (_filename);
    return setErrNoFromWin32Error ();
  }


  free (_filename);
  return 0;
}
int FS(remove) (const char *path)
{
  return FS(_unlink) (path);
}
int FS(_wremove) (const wchar_t *path)
{
  return FS(_wunlink) (path);
}
587 588 589 590 591 592
#else
FILE *FS(fopen) (const char* filename, const char* mode)
{
  return fopen (filename, mode);
}
#endif