Blame glib/gstdio.c

Packit ae235b
/* gstdio.c - wrappers for C library functions
Packit ae235b
 *
Packit ae235b
 * Copyright 2004 Tor Lillqvist
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public License
Packit ae235b
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "glibconfig.h"
Packit ae235b
Packit ae235b
#define G_STDIO_NO_WRAP_ON_UNIX
Packit ae235b
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <wchar.h>
Packit ae235b
#include <direct.h>
Packit ae235b
#include <io.h>
Packit ae235b
#include <sys/utime.h>
Packit ae235b
#else
Packit ae235b
#include <utime.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gstdio.h"
Packit ae235b
#include "gstdioprivate.h"
Packit ae235b
Packit ae235b
#if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
Packit ae235b
#error Please port this to your operating system
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if defined (_MSC_VER) && !defined(_WIN64)
Packit ae235b
#undef _wstat
Packit ae235b
#define _wstat _wstat32
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if defined (G_OS_WIN32)
Packit ae235b
Packit ae235b
/* We can't include Windows DDK and Windows SDK simultaneously,
Packit ae235b
 * so let's copy this here from MinGW-w64 DDK.
Packit ae235b
 * The structure is ultimately documented here:
Packit ae235b
 * https://msdn.microsoft.com/en-us/library/ff552012(v=vs.85).aspx
Packit ae235b
 */
Packit ae235b
typedef struct _REPARSE_DATA_BUFFER
Packit ae235b
{
Packit ae235b
  ULONG  ReparseTag;
Packit ae235b
  USHORT ReparseDataLength;
Packit ae235b
  USHORT Reserved;
Packit ae235b
  union
Packit ae235b
  {
Packit ae235b
    struct
Packit ae235b
    {
Packit ae235b
      USHORT SubstituteNameOffset;
Packit ae235b
      USHORT SubstituteNameLength;
Packit ae235b
      USHORT PrintNameOffset;
Packit ae235b
      USHORT PrintNameLength;
Packit ae235b
      ULONG  Flags;
Packit ae235b
      WCHAR  PathBuffer[1];
Packit ae235b
    } SymbolicLinkReparseBuffer;
Packit ae235b
    struct
Packit ae235b
    {
Packit ae235b
      USHORT SubstituteNameOffset;
Packit ae235b
      USHORT SubstituteNameLength;
Packit ae235b
      USHORT PrintNameOffset;
Packit ae235b
      USHORT PrintNameLength;
Packit ae235b
      WCHAR  PathBuffer[1];
Packit ae235b
    } MountPointReparseBuffer;
Packit ae235b
    struct
Packit ae235b
    {
Packit ae235b
      UCHAR  DataBuffer[1];
Packit ae235b
    } GenericReparseBuffer;
Packit ae235b
  };
Packit ae235b
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
Packit ae235b
Packit ae235b
static int
Packit ae235b
w32_error_to_errno (DWORD error_code)
Packit ae235b
{
Packit ae235b
  switch (error_code)
Packit ae235b
    {
Packit ae235b
    case ERROR_ACCESS_DENIED:
Packit ae235b
      return EACCES;
Packit ae235b
      break;
Packit ae235b
    case ERROR_INVALID_HANDLE:
Packit ae235b
      return EBADF;
Packit ae235b
      break;
Packit ae235b
    case ERROR_INVALID_FUNCTION:
Packit ae235b
      return EFAULT;
Packit ae235b
      break;
Packit ae235b
    case ERROR_FILE_NOT_FOUND:
Packit ae235b
      return ENOENT;
Packit ae235b
      break;
Packit ae235b
    case ERROR_PATH_NOT_FOUND:
Packit ae235b
      return ENOENT; /* or ELOOP, or ENAMETOOLONG */
Packit ae235b
      break;
Packit ae235b
    case ERROR_NOT_ENOUGH_MEMORY:
Packit ae235b
    case ERROR_OUTOFMEMORY:
Packit ae235b
      return ENOMEM;
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      return EIO;
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
_g_win32_stat_utf16_no_trailing_slashes (const gunichar2    *filename,
Packit ae235b
                                         int                 fd,
Packit ae235b
                                         GWin32PrivateStat  *buf,
Packit ae235b
                                         gboolean            for_symlink)
Packit ae235b
{
Packit ae235b
  HANDLE file_handle;
Packit ae235b
  gboolean succeeded_so_far;
Packit ae235b
  DWORD error_code;
Packit ae235b
  struct __stat64 statbuf;
Packit ae235b
  BY_HANDLE_FILE_INFORMATION handle_info;
Packit ae235b
  FILE_STANDARD_INFO std_info;
Packit ae235b
  WIN32_FIND_DATAW finddata;
Packit ae235b
  DWORD immediate_attributes;
Packit ae235b
  gboolean is_symlink = FALSE;
Packit ae235b
  gboolean is_directory;
Packit ae235b
  DWORD open_flags;
Packit ae235b
  wchar_t *filename_target = NULL;
Packit ae235b
  int result;
Packit ae235b
Packit ae235b
  if (fd < 0)
Packit ae235b
    {
Packit ae235b
      immediate_attributes = GetFileAttributesW (filename);
Packit ae235b
Packit ae235b
      if (immediate_attributes == INVALID_FILE_ATTRIBUTES)
Packit ae235b
        {
Packit ae235b
          error_code = GetLastError ();
Packit ae235b
          errno = w32_error_to_errno (error_code);
Packit ae235b
Packit ae235b
          return -1;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      is_symlink = (immediate_attributes & FILE_ATTRIBUTE_REPARSE_POINT) == FILE_ATTRIBUTE_REPARSE_POINT;
Packit ae235b
      is_directory = (immediate_attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
Packit ae235b
Packit ae235b
      open_flags = FILE_ATTRIBUTE_NORMAL;
Packit ae235b
Packit ae235b
      if (for_symlink && is_symlink)
Packit ae235b
        open_flags |= FILE_FLAG_OPEN_REPARSE_POINT;
Packit ae235b
Packit ae235b
      if (is_directory)
Packit ae235b
        open_flags |= FILE_FLAG_BACKUP_SEMANTICS;
Packit ae235b
Packit ae235b
      file_handle = CreateFileW (filename, FILE_READ_ATTRIBUTES,
Packit ae235b
                                 FILE_SHARE_READ, NULL, OPEN_EXISTING,
Packit ae235b
                                 open_flags,
Packit ae235b
                                 NULL);
Packit ae235b
Packit ae235b
      if (file_handle == INVALID_HANDLE_VALUE)
Packit ae235b
        {
Packit ae235b
          error_code = GetLastError ();
Packit ae235b
          errno = w32_error_to_errno (error_code);
Packit ae235b
          return -1;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      file_handle = (HANDLE) _get_osfhandle (fd);
Packit ae235b
Packit ae235b
      if (file_handle == INVALID_HANDLE_VALUE)
Packit ae235b
        return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  succeeded_so_far = GetFileInformationByHandle (file_handle,
Packit ae235b
                                                 &handle_info);
Packit ae235b
  error_code = GetLastError ();
Packit ae235b
Packit ae235b
  if (succeeded_so_far)
Packit ae235b
    {
Packit ae235b
      succeeded_so_far = GetFileInformationByHandleEx (file_handle,
Packit ae235b
                                                       FileStandardInfo,
Packit ae235b
                                                       &std_info,
Packit ae235b
                                                       sizeof (std_info));
Packit ae235b
      error_code = GetLastError ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!succeeded_so_far)
Packit ae235b
    {
Packit ae235b
      CloseHandle (file_handle);
Packit ae235b
      errno = w32_error_to_errno (error_code);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* It's tempting to use GetFileInformationByHandleEx(FileAttributeTagInfo),
Packit ae235b
   * but it always reports that the ReparseTag is 0.
Packit ae235b
   */
Packit ae235b
  if (fd < 0)
Packit ae235b
    {
Packit ae235b
      HANDLE tmp = FindFirstFileW (filename,
Packit ae235b
                                   &finddata);
Packit ae235b
Packit ae235b
      if (tmp == INVALID_HANDLE_VALUE)
Packit ae235b
        {
Packit ae235b
          error_code = GetLastError ();
Packit ae235b
          errno = w32_error_to_errno (error_code);
Packit ae235b
          CloseHandle (file_handle);
Packit ae235b
          return -1;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      FindClose (tmp);
Packit ae235b
Packit ae235b
      if (is_symlink && !for_symlink)
Packit ae235b
        {
Packit ae235b
          /* If filename is a symlink, _wstat64 obtains information about
Packit ae235b
           * the symlink (except that st_size will be 0).
Packit ae235b
           * To get information about the target we need to resolve
Packit ae235b
           * the symlink first. And we need _wstat64() to get st_dev,
Packit ae235b
           * it's a bother to try finding it ourselves.
Packit ae235b
           */
Packit ae235b
          DWORD filename_target_len;
Packit ae235b
          DWORD new_len;
Packit ae235b
Packit ae235b
          /* Just in case, give it a real memory location instead of NULL */
Packit ae235b
          new_len = GetFinalPathNameByHandleW (file_handle,
Packit ae235b
                                               (wchar_t *) &filename_target_len,
Packit ae235b
                                               0,
Packit ae235b
                                               FILE_NAME_NORMALIZED);
Packit ae235b
Packit ae235b
#define SANE_LIMIT 1024 * 10
Packit ae235b
          if (new_len >= SANE_LIMIT)
Packit ae235b
#undef SANE_LIMIT
Packit ae235b
            {
Packit ae235b
              new_len = 0;
Packit ae235b
              error_code = ERROR_BUFFER_OVERFLOW;
Packit ae235b
            }
Packit ae235b
          else if (new_len == 0)
Packit ae235b
            {
Packit ae235b
              error_code = GetLastError ();
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if (new_len > 0)
Packit ae235b
            {
Packit ae235b
              const wchar_t *extended_prefix = L"\\\\?\\";
Packit ae235b
              const gsize    extended_prefix_len = wcslen (extended_prefix);
Packit ae235b
              const gsize    extended_prefix_len_bytes = sizeof (wchar_t) * extended_prefix_len;
Packit ae235b
Packit ae235b
              /* Pretend that new_len doesn't count the terminating NUL char,
Packit ae235b
               * and ask for a bit more space than is needed.
Packit ae235b
               */
Packit ae235b
              filename_target_len = new_len + 5;
Packit ae235b
              filename_target = g_malloc (filename_target_len * sizeof (wchar_t));
Packit ae235b
Packit ae235b
              new_len = GetFinalPathNameByHandleW (file_handle,
Packit ae235b
                                                   filename_target,
Packit ae235b
                                                   filename_target_len,
Packit ae235b
                                                   FILE_NAME_NORMALIZED);
Packit ae235b
Packit ae235b
              /* filename_target_len is already larger than needed,
Packit ae235b
               * new_len should be smaller than that, even if the size
Packit ae235b
               * is off by 1 for some reason.
Packit ae235b
               */
Packit ae235b
              if (new_len >= filename_target_len - 1)
Packit ae235b
                {
Packit ae235b
                  new_len = 0;
Packit ae235b
                  error_code = ERROR_BUFFER_OVERFLOW;
Packit ae235b
                  g_clear_pointer (&filename_target, g_free);
Packit ae235b
                }
Packit ae235b
              /* GetFinalPathNameByHandle() is documented to return extended paths,
Packit ae235b
               * strip the extended prefix.
Packit ae235b
               */
Packit ae235b
              else if (new_len > extended_prefix_len &&
Packit ae235b
                       memcmp (filename_target, extended_prefix, extended_prefix_len_bytes) == 0)
Packit ae235b
                {
Packit ae235b
                  new_len -= extended_prefix_len;
Packit ae235b
                  memmove (filename_target,
Packit ae235b
                           filename_target + extended_prefix_len,
Packit ae235b
                           (new_len + 1) * sizeof (wchar_t));
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if (new_len == 0)
Packit ae235b
            succeeded_so_far = FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      CloseHandle (file_handle);
Packit ae235b
    }
Packit ae235b
  /* else if fd >= 0 the file_handle was obtained via _get_osfhandle()
Packit ae235b
   * and must not be closed, it is owned by fd.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  if (!succeeded_so_far)
Packit ae235b
    {
Packit ae235b
      errno = w32_error_to_errno (error_code);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (fd < 0)
Packit ae235b
    result = _wstat64 (filename_target != NULL ? filename_target : filename, &statbuf);
Packit ae235b
  else
Packit ae235b
    result = _fstat64 (fd, &statbuf);
Packit ae235b
Packit ae235b
  if (result != 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_free (filename_target);
Packit ae235b
      errno = errsv;
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (filename_target);
Packit ae235b
Packit ae235b
  buf->st_dev = statbuf.st_dev;
Packit ae235b
  buf->st_mode = statbuf.st_mode;
Packit ae235b
  buf->volume_serial = handle_info.dwVolumeSerialNumber;
Packit ae235b
  buf->file_index = (((guint64) handle_info.nFileIndexHigh) << 32) | handle_info.nFileIndexLow;
Packit ae235b
  /* Note that immediate_attributes is for the symlink
Packit ae235b
   * (if it's a symlink), while handle_info contains info
Packit ae235b
   * about the symlink or the target, depending on the flags
Packit ae235b
   * we used earlier.
Packit ae235b
   */
Packit ae235b
  buf->attributes = handle_info.dwFileAttributes;
Packit ae235b
  buf->st_nlink = handle_info.nNumberOfLinks;
Packit ae235b
  buf->st_size = (((guint64) handle_info.nFileSizeHigh) << 32) | handle_info.nFileSizeLow;
Packit ae235b
  buf->allocated_size = std_info.AllocationSize.QuadPart;
Packit ae235b
Packit ae235b
  if (fd < 0 && buf->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
Packit ae235b
    buf->reparse_tag = finddata.dwReserved0;
Packit ae235b
  else
Packit ae235b
    buf->reparse_tag = 0;
Packit ae235b
Packit ae235b
  buf->st_ctime = statbuf.st_ctime;
Packit ae235b
  buf->st_atime = statbuf.st_atime;
Packit ae235b
  buf->st_mtime = statbuf.st_mtime;
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
_g_win32_stat_utf8 (const gchar       *filename,
Packit ae235b
                    GWin32PrivateStat *buf,
Packit ae235b
                    gboolean           for_symlink)
Packit ae235b
{
Packit ae235b
  wchar_t *wfilename;
Packit ae235b
  int result;
Packit ae235b
  gsize len;
Packit ae235b
Packit ae235b
  len = strlen (filename);
Packit ae235b
Packit ae235b
  while (len > 0 && G_IS_DIR_SEPARATOR (filename[len - 1]))
Packit ae235b
    len--;
Packit ae235b
Packit ae235b
  if (len <= 0 ||
Packit ae235b
      (g_path_is_absolute (filename) && len <= g_path_skip_root (filename) - filename))
Packit ae235b
    len = strlen (filename);
Packit ae235b
Packit ae235b
  wfilename = g_utf8_to_utf16 (filename, len, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = _g_win32_stat_utf16_no_trailing_slashes (wfilename, -1, buf, for_symlink);
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
g_win32_stat_utf8 (const gchar       *filename,
Packit ae235b
                   GWin32PrivateStat *buf)
Packit ae235b
{
Packit ae235b
  return _g_win32_stat_utf8 (filename, buf, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
g_win32_lstat_utf8 (const gchar       *filename,
Packit ae235b
                    GWin32PrivateStat *buf)
Packit ae235b
{
Packit ae235b
  return _g_win32_stat_utf8 (filename, buf, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
g_win32_fstat (int                fd,
Packit ae235b
               GWin32PrivateStat *buf)
Packit ae235b
{
Packit ae235b
  return _g_win32_stat_utf16_no_trailing_slashes (NULL, fd, buf, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
_g_win32_readlink_utf16_raw (const gunichar2 *filename,
Packit ae235b
                             gunichar2       *buf,
Packit ae235b
                             gsize            buf_size)
Packit ae235b
{
Packit ae235b
  DWORD returned_bytes;
Packit ae235b
  BYTE returned_data[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; /* This is 16k, by the way */
Packit ae235b
  HANDLE h;
Packit ae235b
  DWORD attributes;
Packit ae235b
  REPARSE_DATA_BUFFER *rep_buf;
Packit ae235b
  DWORD to_copy;
Packit ae235b
  DWORD error_code;
Packit ae235b
Packit ae235b
  if (buf_size > G_MAXSIZE / sizeof (wchar_t))
Packit ae235b
    {
Packit ae235b
      /* "buf_size * sizeof (wchar_t)" overflows */
Packit ae235b
      errno = EFAULT;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((attributes = GetFileAttributesW (filename)) == 0)
Packit ae235b
    {
Packit ae235b
      error_code = GetLastError ();
Packit ae235b
      errno = w32_error_to_errno (error_code);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((attributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* To read symlink target we need to open the file as a reparse
Packit ae235b
   * point and use DeviceIoControl() on it.
Packit ae235b
   */
Packit ae235b
  h = CreateFileW (filename,
Packit ae235b
                   FILE_READ_ATTRIBUTES | SYNCHRONIZE | GENERIC_READ,
Packit ae235b
                   FILE_SHARE_READ, NULL, OPEN_EXISTING,
Packit ae235b
                   FILE_ATTRIBUTE_NORMAL
Packit ae235b
                   | FILE_FLAG_OPEN_REPARSE_POINT
Packit ae235b
                   | (attributes & FILE_ATTRIBUTE_DIRECTORY ? FILE_FLAG_BACKUP_SEMANTICS : 0),
Packit ae235b
                   NULL);
Packit ae235b
Packit ae235b
  if (h == INVALID_HANDLE_VALUE)
Packit ae235b
    {
Packit ae235b
      error_code = GetLastError ();
Packit ae235b
      errno = w32_error_to_errno (error_code);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!DeviceIoControl (h, FSCTL_GET_REPARSE_POINT, NULL, 0,
Packit ae235b
                        returned_data, MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
Packit ae235b
                        &returned_bytes, NULL))
Packit ae235b
    {
Packit ae235b
      error_code = GetLastError ();
Packit ae235b
      errno = w32_error_to_errno (error_code);
Packit ae235b
      CloseHandle (h);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  rep_buf = (REPARSE_DATA_BUFFER *) returned_data;
Packit ae235b
  to_copy = 0;
Packit ae235b
Packit ae235b
  if (rep_buf->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Packit ae235b
    {
Packit ae235b
      to_copy = rep_buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
Packit ae235b
Packit ae235b
      if (to_copy > buf_size * sizeof (wchar_t))
Packit ae235b
        to_copy = buf_size * sizeof (wchar_t);
Packit ae235b
Packit ae235b
      memcpy (buf,
Packit ae235b
              &((BYTE *) rep_buf->SymbolicLinkReparseBuffer.PathBuffer)[rep_buf->SymbolicLinkReparseBuffer.SubstituteNameOffset],
Packit ae235b
              to_copy);
Packit ae235b
    }
Packit ae235b
  else if (rep_buf->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
Packit ae235b
    {
Packit ae235b
      to_copy = rep_buf->MountPointReparseBuffer.SubstituteNameLength;
Packit ae235b
Packit ae235b
      if (to_copy > buf_size * sizeof (wchar_t))
Packit ae235b
        to_copy = buf_size * sizeof (wchar_t);
Packit ae235b
Packit ae235b
      memcpy (buf,
Packit ae235b
              &((BYTE *) rep_buf->MountPointReparseBuffer.PathBuffer)[rep_buf->MountPointReparseBuffer.SubstituteNameOffset],
Packit ae235b
              to_copy);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  CloseHandle (h);
Packit ae235b
Packit ae235b
  return to_copy;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
_g_win32_readlink_utf16 (const gunichar2 *filename,
Packit ae235b
                         gunichar2       *buf,
Packit ae235b
                         gsize            buf_size)
Packit ae235b
{
Packit ae235b
  const wchar_t *ntobjm_prefix = L"\\??\\";
Packit ae235b
  const gsize    ntobjm_prefix_len_unichar2 = wcslen (ntobjm_prefix);
Packit ae235b
  const gsize    ntobjm_prefix_len_bytes = sizeof (gunichar2) * ntobjm_prefix_len_unichar2;
Packit ae235b
  int            result = _g_win32_readlink_utf16_raw (filename, buf, buf_size);
Packit ae235b
Packit ae235b
  if (result <= 0)
Packit ae235b
    return result;
Packit ae235b
Packit ae235b
  /* Ensure that output is a multiple of sizeof (gunichar2),
Packit ae235b
   * cutting any trailing partial gunichar2, if present.
Packit ae235b
   */
Packit ae235b
  result -= result % sizeof (gunichar2);
Packit ae235b
Packit ae235b
  if (result <= 0)
Packit ae235b
    return result;
Packit ae235b
Packit ae235b
  /* DeviceIoControl () tends to return filenames as NT Object Manager
Packit ae235b
   * names , i.e. "\\??\\C:\\foo\\bar".
Packit ae235b
   * Remove the leading 4-byte \??\ prefix, as glib (as well as many W32 API
Packit ae235b
   * functions) is unprepared to deal with it.
Packit ae235b
   */
Packit ae235b
  if (result > ntobjm_prefix_len_bytes &&
Packit ae235b
      memcmp (buf, ntobjm_prefix, ntobjm_prefix_len_bytes) == 0)
Packit ae235b
    {
Packit ae235b
      result -= ntobjm_prefix_len_bytes;
Packit ae235b
      memmove (buf, buf + ntobjm_prefix_len_unichar2, result);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
g_win32_readlink_utf8 (const gchar *filename,
Packit ae235b
                       gchar       *buf,
Packit ae235b
                       gsize        buf_size)
Packit ae235b
{
Packit ae235b
  wchar_t *wfilename;
Packit ae235b
  int result;
Packit ae235b
Packit ae235b
  wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = _g_win32_readlink_utf16 (wfilename, (gunichar2 *) buf, buf_size);
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  if (result > 0)
Packit ae235b
    {
Packit ae235b
      glong tmp_len;
Packit ae235b
      gchar *tmp = g_utf16_to_utf8 ((const gunichar2 *) buf,
Packit ae235b
                                    result / sizeof (gunichar2),
Packit ae235b
                                    NULL,
Packit ae235b
                                    &tmp_len,
Packit ae235b
                                    NULL);
Packit ae235b
Packit ae235b
      if (tmp == NULL)
Packit ae235b
        {
Packit ae235b
          errno = EINVAL;
Packit ae235b
          return -1;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (tmp_len > buf_size - 1)
Packit ae235b
        tmp_len = buf_size - 1;
Packit ae235b
Packit ae235b
      memcpy (buf, tmp, tmp_len);
Packit ae235b
      /* readlink() doesn't NUL-terminate, but we do.
Packit ae235b
       * To be compliant, however, we return the
Packit ae235b
       * number of bytes without the NUL-terminator.
Packit ae235b
       */
Packit ae235b
      buf[tmp_len] = '\0';
Packit ae235b
      result = tmp_len;
Packit ae235b
      g_free (tmp);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_access:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @mode: as in access()
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX access() function. This function is used to
Packit ae235b
 * test a pathname for one or several of read, write or execute
Packit ae235b
 * permissions, or just existence.
Packit ae235b
 *
Packit ae235b
 * On Windows, the file protection mechanism is not at all POSIX-like,
Packit ae235b
 * and the underlying function in the C library only checks the
Packit ae235b
 * FAT-style READONLY attribute, and does not look at the ACL of a
Packit ae235b
 * file at all. This function is this in practise almost useless on
Packit ae235b
 * Windows. Software that needs to handle file permissions on Windows
Packit ae235b
 * more exactly should use the Win32 API.
Packit ae235b
 *
Packit ae235b
 * See your C library manual for more details about access().
Packit ae235b
 *
Packit ae235b
 * Returns: zero if the pathname refers to an existing file system
Packit ae235b
 *     object that has all the tested permissions, or -1 otherwise
Packit ae235b
 *     or on error.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_access (const gchar *filename,
Packit ae235b
	  int          mode)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
    
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifndef X_OK
Packit ae235b
#define X_OK 1
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  retval = _waccess (wfilename, mode & ~X_OK);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return access (filename, mode);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_chmod:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @mode: as in chmod()
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX chmod() function. The chmod() function is
Packit ae235b
 * used to set the permissions of a file system object.
Packit ae235b
 * 
Packit ae235b
 * On Windows the file protection mechanism is not at all POSIX-like,
Packit ae235b
 * and the underlying chmod() function in the C library just sets or
Packit ae235b
 * clears the FAT-style READONLY attribute. It does not touch any
Packit ae235b
 * ACL. Software that needs to manage file permissions on Windows
Packit ae235b
 * exactly should use the Win32 API.
Packit ae235b
 *
Packit ae235b
 * See your C library manual for more details about chmod().
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the operation succeeded, -1 on error
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_chmod (const gchar *filename,
Packit ae235b
	 int          mode)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
    
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wchmod (wfilename, mode);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return chmod (filename, mode);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
/**
Packit ae235b
 * g_open:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @flags: as in open()
Packit ae235b
 * @mode: as in open()
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX open() function. The open() function is
Packit ae235b
 * used to convert a pathname into a file descriptor.
Packit ae235b
 *
Packit ae235b
 * On POSIX systems file descriptors are implemented by the operating
Packit ae235b
 * system. On Windows, it's the C library that implements open() and
Packit ae235b
 * file descriptors. The actual Win32 API for opening files is quite
Packit ae235b
 * different, see MSDN documentation for CreateFile(). The Win32 API
Packit ae235b
 * uses file handles, which are more randomish integers, not small
Packit ae235b
 * integers like file descriptors.
Packit ae235b
 *
Packit ae235b
 * Because file descriptors are specific to the C library on Windows,
Packit ae235b
 * the file descriptor returned by this function makes sense only to
Packit ae235b
 * functions in the same C library. Thus if the GLib-using code uses a
Packit ae235b
 * different C library than GLib does, the file descriptor returned by
Packit ae235b
 * this function cannot be passed to C library functions like write()
Packit ae235b
 * or read().
Packit ae235b
 *
Packit ae235b
 * See your C library manual for more details about open().
Packit ae235b
 *
Packit ae235b
 * Returns: a new file descriptor, or -1 if an error occurred.
Packit ae235b
 *     The return value can be used exactly like the return value
Packit ae235b
 *     from open().
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_open (const gchar *filename,
Packit ae235b
	int          flags,
Packit ae235b
	int          mode)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
    
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wopen (wfilename, flags, mode);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  int fd;
Packit ae235b
  do
Packit ae235b
    fd = open (filename, flags, mode);
Packit ae235b
  while (G_UNLIKELY (fd == -1 && errno == EINTR));
Packit ae235b
  return fd;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_creat:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @mode: as in creat()
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX creat() function. The creat() function is
Packit ae235b
 * used to convert a pathname into a file descriptor, creating a file
Packit ae235b
 * if necessary.
Packit ae235b
 *
Packit ae235b
 * On POSIX systems file descriptors are implemented by the operating
Packit ae235b
 * system. On Windows, it's the C library that implements creat() and
Packit ae235b
 * file descriptors. The actual Windows API for opening files is
Packit ae235b
 * different, see MSDN documentation for CreateFile(). The Win32 API
Packit ae235b
 * uses file handles, which are more randomish integers, not small
Packit ae235b
 * integers like file descriptors.
Packit ae235b
 *
Packit ae235b
 * Because file descriptors are specific to the C library on Windows,
Packit ae235b
 * the file descriptor returned by this function makes sense only to
Packit ae235b
 * functions in the same C library. Thus if the GLib-using code uses a
Packit ae235b
 * different C library than GLib does, the file descriptor returned by
Packit ae235b
 * this function cannot be passed to C library functions like write()
Packit ae235b
 * or read().
Packit ae235b
 *
Packit ae235b
 * See your C library manual for more details about creat().
Packit ae235b
 *
Packit ae235b
 * Returns: a new file descriptor, or -1 if an error occurred.
Packit ae235b
 *     The return value can be used exactly like the return value
Packit ae235b
 *     from creat().
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_creat (const gchar *filename,
Packit ae235b
	 int          mode)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
    
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wcreat (wfilename, mode);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return creat (filename, mode);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rename:
Packit ae235b
 * @oldfilename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @newfilename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX rename() function. The rename() function 
Packit ae235b
 * renames a file, moving it between directories if required.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about how rename() works
Packit ae235b
 * on your system. It is not possible in general on Windows to rename
Packit ae235b
 * a file that is open to some process.
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the renaming succeeded, -1 if an error occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_rename (const gchar *oldfilename,
Packit ae235b
	  const gchar *newfilename)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
Packit ae235b
  wchar_t *wnewfilename;
Packit ae235b
  int retval;
Packit ae235b
  int save_errno = 0;
Packit ae235b
Packit ae235b
  if (woldfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if (wnewfilename == NULL)
Packit ae235b
    {
Packit ae235b
      g_free (woldfilename);
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
Packit ae235b
    retval = 0;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      retval = -1;
Packit ae235b
      switch (GetLastError ())
Packit ae235b
	{
Packit ae235b
#define CASE(a,b) case ERROR_##a: save_errno = b; break
Packit ae235b
	  CASE (FILE_NOT_FOUND, ENOENT);
Packit ae235b
	  CASE (PATH_NOT_FOUND, ENOENT);
Packit ae235b
	  CASE (ACCESS_DENIED, EACCES);
Packit ae235b
	  CASE (NOT_SAME_DEVICE, EXDEV);
Packit ae235b
	  CASE (LOCK_VIOLATION, EACCES);
Packit ae235b
	  CASE (SHARING_VIOLATION, EACCES);
Packit ae235b
	  CASE (FILE_EXISTS, EEXIST);
Packit ae235b
	  CASE (ALREADY_EXISTS, EEXIST);
Packit ae235b
#undef CASE
Packit ae235b
	default: save_errno = EIO;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (woldfilename);
Packit ae235b
  g_free (wnewfilename);
Packit ae235b
    
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return rename (oldfilename, newfilename);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mkdir: 
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @mode: permissions to use for the newly created directory
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX mkdir() function. The mkdir() function 
Packit ae235b
 * attempts to create a directory with the given name and permissions.
Packit ae235b
 * The mode argument is ignored on Windows.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about mkdir().
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the directory was successfully created, -1 if an error 
Packit ae235b
 *    occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_mkdir (const gchar *filename,
Packit ae235b
	 int          mode)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wmkdir (wfilename);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
    
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return mkdir (filename, mode);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_chdir: 
Packit ae235b
 * @path: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX chdir() function. The function changes the
Packit ae235b
 * current directory of the process to @path.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about chdir().
Packit ae235b
 *
Packit ae235b
 * Returns: 0 on success, -1 if an error occurred.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_chdir (const gchar *path)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wpath == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wchdir (wpath);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wpath);
Packit ae235b
    
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return chdir (path);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GStatBuf:
Packit ae235b
 *
Packit ae235b
 * A type corresponding to the appropriate struct type for the stat()
Packit ae235b
 * system call, depending on the platform and/or compiler being used.
Packit ae235b
 *
Packit ae235b
 * See g_stat() for more information.
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * g_stat: 
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @buf: a pointer to a stat struct, which will be filled with the file
Packit ae235b
 *     information
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX stat() function. The stat() function
Packit ae235b
 * returns information about a file. On Windows the stat() function in
Packit ae235b
 * the C library checks only the FAT-style READONLY attribute and does
Packit ae235b
 * not look at the ACL at all. Thus on Windows the protection bits in
Packit ae235b
 * the @st_mode field are a fabrication of little use.
Packit ae235b
 * 
Packit ae235b
 * On Windows the Microsoft C libraries have several variants of the
Packit ae235b
 * stat struct and stat() function with names like _stat(), _stat32(),
Packit ae235b
 * _stat32i64() and _stat64i32(). The one used here is for 32-bit code
Packit ae235b
 * the one with 32-bit size and time fields, specifically called _stat32().
Packit ae235b
 *
Packit ae235b
 * In Microsoft's compiler, by default struct stat means one with
Packit ae235b
 * 64-bit time fields while in MinGW struct stat is the legacy one
Packit ae235b
 * with 32-bit fields. To hopefully clear up this messs, the gstdio.h
Packit ae235b
 * header defines a type #GStatBuf which is the appropriate struct type
Packit ae235b
 * depending on the platform and/or compiler being used. On POSIX it
Packit ae235b
 * is just struct stat, but note that even on POSIX platforms, stat()
Packit ae235b
 * might be a macro.
Packit ae235b
 *
Packit ae235b
 * See your C library manual for more details about stat().
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the information was successfully retrieved,
Packit ae235b
 *     -1 if an error occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_stat (const gchar *filename,
Packit ae235b
	GStatBuf    *buf)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  GWin32PrivateStat w32_buf;
Packit ae235b
  int retval = g_win32_stat_utf8 (filename, &w32_buf);
Packit ae235b
Packit ae235b
  buf->st_dev = w32_buf.st_dev;
Packit ae235b
  buf->st_ino = w32_buf.st_ino;
Packit ae235b
  buf->st_mode = w32_buf.st_mode;
Packit ae235b
  buf->st_nlink = w32_buf.st_nlink;
Packit ae235b
  buf->st_uid = w32_buf.st_uid;
Packit ae235b
  buf->st_gid = w32_buf.st_gid;
Packit ae235b
  buf->st_rdev = w32_buf.st_dev;
Packit ae235b
  buf->st_size = w32_buf.st_size;
Packit ae235b
  buf->st_atime = w32_buf.st_atime;
Packit ae235b
  buf->st_mtime = w32_buf.st_mtime;
Packit ae235b
  buf->st_ctime = w32_buf.st_ctime;
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return stat (filename, buf);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_lstat: 
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @buf: a pointer to a stat struct, which will be filled with the file
Packit ae235b
 *     information
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX lstat() function. The lstat() function is
Packit ae235b
 * like stat() except that in the case of symbolic links, it returns
Packit ae235b
 * information about the symbolic link itself and not the file that it
Packit ae235b
 * refers to. If the system does not support symbolic links g_lstat()
Packit ae235b
 * is identical to g_stat().
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about lstat().
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the information was successfully retrieved,
Packit ae235b
 *     -1 if an error occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_lstat (const gchar *filename,
Packit ae235b
	 GStatBuf    *buf)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_LSTAT
Packit ae235b
  /* This can't be Win32, so don't do the widechar dance. */
Packit ae235b
  return lstat (filename, buf);
Packit ae235b
#elif defined (G_OS_WIN32)
Packit ae235b
  GWin32PrivateStat w32_buf;
Packit ae235b
  int retval = g_win32_lstat_utf8 (filename, &w32_buf);
Packit ae235b
Packit ae235b
  buf->st_dev = w32_buf.st_dev;
Packit ae235b
  buf->st_ino = w32_buf.st_ino;
Packit ae235b
  buf->st_mode = w32_buf.st_mode;
Packit ae235b
  buf->st_nlink = w32_buf.st_nlink;
Packit ae235b
  buf->st_uid = w32_buf.st_uid;
Packit ae235b
  buf->st_gid = w32_buf.st_gid;
Packit ae235b
  buf->st_rdev = w32_buf.st_dev;
Packit ae235b
  buf->st_size = w32_buf.st_size;
Packit ae235b
  buf->st_atime = w32_buf.st_atime;
Packit ae235b
  buf->st_mtime = w32_buf.st_mtime;
Packit ae235b
  buf->st_ctime = w32_buf.st_ctime;
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return g_stat (filename, buf);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unlink:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX unlink() function. The unlink() function 
Packit ae235b
 * deletes a name from the filesystem. If this was the last link to the 
Packit ae235b
 * file and no processes have it opened, the diskspace occupied by the
Packit ae235b
 * file is freed.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about unlink(). Note
Packit ae235b
 * that on Windows, it is in general not possible to delete files that
Packit ae235b
 * are open to some process, or mapped into memory.
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the name was successfully deleted, -1 if an error 
Packit ae235b
 *    occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_unlink (const gchar *filename)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wunlink (wfilename);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return unlink (filename);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_remove:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX remove() function. The remove() function
Packit ae235b
 * deletes a name from the filesystem.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about how remove() works
Packit ae235b
 * on your system. On Unix, remove() removes also directories, as it
Packit ae235b
 * calls unlink() for files and rmdir() for directories. On Windows,
Packit ae235b
 * although remove() in the C library only works for files, this
Packit ae235b
 * function tries first remove() and then if that fails rmdir(), and
Packit ae235b
 * thus works for both files and directories. Note however, that on
Packit ae235b
 * Windows, it is in general not possible to remove a file that is
Packit ae235b
 * open to some process, or mapped into memory.
Packit ae235b
 *
Packit ae235b
 * If this function fails on Windows you can't infer too much from the
Packit ae235b
 * errno value. rmdir() is tried regardless of what caused remove() to
Packit ae235b
 * fail. Any errno value set by remove() will be overwritten by that
Packit ae235b
 * set by rmdir().
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the file was successfully removed, -1 if an error 
Packit ae235b
 *    occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_remove (const gchar *filename)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wremove (wfilename);
Packit ae235b
  if (retval == -1)
Packit ae235b
    retval = _wrmdir (wfilename);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return remove (filename);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_rmdir:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX rmdir() function. The rmdir() function
Packit ae235b
 * deletes a directory from the filesystem.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about how rmdir() works
Packit ae235b
 * on your system.
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the directory was successfully removed, -1 if an error 
Packit ae235b
 *    occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_rmdir (const gchar *filename)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  retval = _wrmdir (wfilename);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return rmdir (filename);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_fopen:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @mode: a string describing the mode in which the file should be opened
Packit ae235b
 *
Packit ae235b
 * A wrapper for the stdio fopen() function. The fopen() function
Packit ae235b
 * opens a file and associates a new stream with it.
Packit ae235b
 * 
Packit ae235b
 * Because file descriptors are specific to the C library on Windows,
Packit ae235b
 * and a file descriptor is part of the FILE struct, the FILE* returned
Packit ae235b
 * by this function makes sense only to functions in the same C library.
Packit ae235b
 * Thus if the GLib-using code uses a different C library than GLib does,
Packit ae235b
 * the FILE* returned by this function cannot be passed to C library
Packit ae235b
 * functions like fprintf() or fread().
Packit ae235b
 *
Packit ae235b
 * See your C library manual for more details about fopen().
Packit ae235b
 *
Packit ae235b
 * Returns: A FILE* if the file was successfully opened, or %NULL if
Packit ae235b
 *     an error occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
FILE *
Packit ae235b
g_fopen (const gchar *filename,
Packit ae235b
	 const gchar *mode)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  wchar_t *wmode;
Packit ae235b
  FILE *retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if (wmode == NULL)
Packit ae235b
    {
Packit ae235b
      g_free (wfilename);
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = _wfopen (wfilename, wmode);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
  g_free (wmode);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return fopen (filename, mode);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_freopen:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @mode: a string describing the mode in which the file should be  opened
Packit ae235b
 * @stream: (nullable): an existing stream which will be reused, or %NULL
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX freopen() function. The freopen() function
Packit ae235b
 * opens a file and associates it with an existing stream.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about freopen().
Packit ae235b
 *
Packit ae235b
 * Returns: A FILE* if the file was successfully opened, or %NULL if
Packit ae235b
 *     an error occurred.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
FILE *
Packit ae235b
g_freopen (const gchar *filename,
Packit ae235b
	   const gchar *mode,
Packit ae235b
	   FILE        *stream)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  wchar_t *wmode;
Packit ae235b
  FILE *retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if (wmode == NULL)
Packit ae235b
    {
Packit ae235b
      g_free (wfilename);
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  retval = _wfreopen (wfilename, wmode, stream);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
  g_free (wmode);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return freopen (filename, mode, stream);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_utime:
Packit ae235b
 * @filename: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 *     (UTF-8 on Windows)
Packit ae235b
 * @utb: a pointer to a struct utimbuf.
Packit ae235b
 *
Packit ae235b
 * A wrapper for the POSIX utime() function. The utime() function
Packit ae235b
 * sets the access and modification timestamps of a file.
Packit ae235b
 * 
Packit ae235b
 * See your C library manual for more details about how utime() works
Packit ae235b
 * on your system.
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the operation was successful, -1 if an error occurred
Packit ae235b
 * 
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_utime (const gchar    *filename,
Packit ae235b
	 struct utimbuf *utb)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  int retval;
Packit ae235b
  int save_errno;
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  retval = _wutime (wfilename, (struct _utimbuf*) utb);
Packit ae235b
  save_errno = errno;
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  errno = save_errno;
Packit ae235b
  return retval;
Packit ae235b
#else
Packit ae235b
  return utime (filename, utb);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_close:
Packit ae235b
 * @fd: A file descriptor
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * This wraps the close() call; in case of error, %errno will be
Packit ae235b
 * preserved, but the error will also be stored as a #GError in @error.
Packit ae235b
 *
Packit ae235b
 * Besides using #GError, there is another major reason to prefer this
Packit ae235b
 * function over the call provided by the system; on Unix, it will
Packit ae235b
 * attempt to correctly handle %EINTR, which has platform-specific
Packit ae235b
 * semantics.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if there was an error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_close (gint       fd,
Packit ae235b
         GError   **error)
Packit ae235b
{
Packit ae235b
  int res;
Packit ae235b
  res = close (fd);
Packit ae235b
  /* Just ignore EINTR for now; a retry loop is the wrong thing to do
Packit ae235b
   * on Linux at least.  Anyone who wants to add a conditional check
Packit ae235b
   * for e.g. HP-UX is welcome to do so later...
Packit ae235b
   *
Packit ae235b
   * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
Packit ae235b
   * https://bugzilla.gnome.org/show_bug.cgi?id=682819
Packit ae235b
   * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
Packit ae235b
   * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
Packit ae235b
   */
Packit ae235b
  if (G_UNLIKELY (res == -1 && errno == EINTR))
Packit ae235b
    return TRUE;
Packit ae235b
  else if (res == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      g_set_error_literal (error, G_FILE_ERROR,
Packit ae235b
                           g_file_error_from_errno (errsv),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
      errno = errsv;
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b