Blame glib/gfileutils.c

Packit ae235b
/* gfileutils.c - File utility functions
Packit ae235b
 *
Packit ae235b
 *  Copyright 2000 Red Hat, Inc.
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
#include <sys/stat.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdarg.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#include <io.h>
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#ifndef S_ISLNK
Packit ae235b
#define S_ISLNK(x) 0
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef O_BINARY
Packit ae235b
#define O_BINARY 0
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gfileutils.h"
Packit ae235b
Packit ae235b
#include "gstdio.h"
Packit ae235b
#include "gstdioprivate.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#ifdef HAVE_LINUX_MAGIC_H /* for btrfs check */
Packit ae235b
#include <linux/magic.h>
Packit ae235b
#include <sys/vfs.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:fileutils
Packit ae235b
 * @title: File Utilities
Packit ae235b
 * @short_description: various file-related functions
Packit ae235b
 *
Packit ae235b
 * Do not use these APIs unless you are porting a POSIX application to Windows.
Packit ae235b
 * A more high-level file access API is provided as GIO — see the documentation
Packit ae235b
 * for #GFile.
Packit ae235b
 *
Packit ae235b
 * There is a group of functions which wrap the common POSIX functions
Packit ae235b
 * dealing with filenames (g_open(), g_rename(), g_mkdir(), g_stat(),
Packit ae235b
 * g_unlink(), g_remove(), g_fopen(), g_freopen()). The point of these
Packit ae235b
 * wrappers is to make it possible to handle file names with any Unicode
Packit ae235b
 * characters in them on Windows without having to use ifdefs and the
Packit ae235b
 * wide character API in the application code.
Packit ae235b
 *
Packit ae235b
 * On some Unix systems, these APIs may be defined as identical to their POSIX
Packit ae235b
 * counterparts. For this reason, you must check for and include the necessary
Packit ae235b
 * header files (such as `fcntl.h`) before using functions like g_creat(). You
Packit ae235b
 * must also define the relevant feature test macros.
Packit ae235b
 *
Packit ae235b
 * The pathname argument should be in the GLib file name encoding.
Packit ae235b
 * On POSIX this is the actual on-disk encoding which might correspond
Packit ae235b
 * to the locale settings of the process (or the `G_FILENAME_ENCODING`
Packit ae235b
 * environment variable), or not.
Packit ae235b
 *
Packit ae235b
 * On Windows the GLib file name encoding is UTF-8. Note that the
Packit ae235b
 * Microsoft C library does not use UTF-8, but has separate APIs for
Packit ae235b
 * current system code page and wide characters (UTF-16). The GLib
Packit ae235b
 * wrappers call the wide character API if present (on modern Windows
Packit ae235b
 * systems), otherwise convert to/from the system code page.
Packit ae235b
 *
Packit ae235b
 * Another group of functions allows to open and read directories
Packit ae235b
 * in the GLib file name encoding. These are g_dir_open(),
Packit ae235b
 * g_dir_read_name(), g_dir_rewind(), g_dir_close().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GFileError:
Packit ae235b
 * @G_FILE_ERROR_EXIST: Operation not permitted; only the owner of
Packit ae235b
 *     the file (or other resource) or processes with special privileges
Packit ae235b
 *     can perform the operation.
Packit ae235b
 * @G_FILE_ERROR_ISDIR: File is a directory; you cannot open a directory
Packit ae235b
 *     for writing, or create or remove hard links to it.
Packit ae235b
 * @G_FILE_ERROR_ACCES: Permission denied; the file permissions do not
Packit ae235b
 *     allow the attempted operation.
Packit ae235b
 * @G_FILE_ERROR_NAMETOOLONG: Filename too long.
Packit ae235b
 * @G_FILE_ERROR_NOENT: No such file or directory. This is a "file
Packit ae235b
 *     doesn't exist" error for ordinary files that are referenced in
Packit ae235b
 *     contexts where they are expected to already exist.
Packit ae235b
 * @G_FILE_ERROR_NOTDIR: A file that isn't a directory was specified when
Packit ae235b
 *     a directory is required.
Packit ae235b
 * @G_FILE_ERROR_NXIO: No such device or address. The system tried to
Packit ae235b
 *     use the device represented by a file you specified, and it
Packit ae235b
 *     couldn't find the device. This can mean that the device file was
Packit ae235b
 *     installed incorrectly, or that the physical device is missing or
Packit ae235b
 *     not correctly attached to the computer.
Packit ae235b
 * @G_FILE_ERROR_NODEV: The underlying file system of the specified file
Packit ae235b
 *     does not support memory mapping.
Packit ae235b
 * @G_FILE_ERROR_ROFS: The directory containing the new link can't be
Packit ae235b
 *     modified because it's on a read-only file system.
Packit ae235b
 * @G_FILE_ERROR_TXTBSY: Text file busy.
Packit ae235b
 * @G_FILE_ERROR_FAULT: You passed in a pointer to bad memory.
Packit ae235b
 *     (GLib won't reliably return this, don't pass in pointers to bad
Packit ae235b
 *     memory.)
Packit ae235b
 * @G_FILE_ERROR_LOOP: Too many levels of symbolic links were encountered
Packit ae235b
 *     in looking up a file name. This often indicates a cycle of symbolic
Packit ae235b
 *     links.
Packit ae235b
 * @G_FILE_ERROR_NOSPC: No space left on device; write operation on a
Packit ae235b
 *     file failed because the disk is full.
Packit ae235b
 * @G_FILE_ERROR_NOMEM: No memory available. The system cannot allocate
Packit ae235b
 *     more virtual memory because its capacity is full.
Packit ae235b
 * @G_FILE_ERROR_MFILE: The current process has too many files open and
Packit ae235b
 *     can't open any more. Duplicate descriptors do count toward this
Packit ae235b
 *     limit.
Packit ae235b
 * @G_FILE_ERROR_NFILE: There are too many distinct file openings in the
Packit ae235b
 *     entire system.
Packit ae235b
 * @G_FILE_ERROR_BADF: Bad file descriptor; for example, I/O on a
Packit ae235b
 *     descriptor that has been closed or reading from a descriptor open
Packit ae235b
 *     only for writing (or vice versa).
Packit ae235b
 * @G_FILE_ERROR_INVAL: Invalid argument. This is used to indicate
Packit ae235b
 *     various kinds of problems with passing the wrong argument to a
Packit ae235b
 *     library function.
Packit ae235b
 * @G_FILE_ERROR_PIPE: Broken pipe; there is no process reading from the
Packit ae235b
 *     other end of a pipe. Every library function that returns this
Packit ae235b
 *     error code also generates a 'SIGPIPE' signal; this signal
Packit ae235b
 *     terminates the program if not handled or blocked. Thus, your
Packit ae235b
 *     program will never actually see this code unless it has handled
Packit ae235b
 *     or blocked 'SIGPIPE'.
Packit ae235b
 * @G_FILE_ERROR_AGAIN: Resource temporarily unavailable; the call might
Packit ae235b
 *     work if you try again later.
Packit ae235b
 * @G_FILE_ERROR_INTR: Interrupted function call; an asynchronous signal
Packit ae235b
 *     occurred and prevented completion of the call. When this
Packit ae235b
 *     happens, you should try the call again.
Packit ae235b
 * @G_FILE_ERROR_IO: Input/output error; usually used for physical read
Packit ae235b
 *    or write errors. i.e. the disk or other physical device hardware
Packit ae235b
 *    is returning errors.
Packit ae235b
 * @G_FILE_ERROR_PERM: Operation not permitted; only the owner of the
Packit ae235b
 *    file (or other resource) or processes with special privileges can
Packit ae235b
 *    perform the operation.
Packit ae235b
 * @G_FILE_ERROR_NOSYS: Function not implemented; this indicates that
Packit ae235b
 *    the system is missing some functionality.
Packit ae235b
 * @G_FILE_ERROR_FAILED: Does not correspond to a UNIX error code; this
Packit ae235b
 *    is the standard "failed for unspecified reason" error code present
Packit ae235b
 *    in all #GError error code enumerations. Returned if no specific
Packit ae235b
 *    code applies.
Packit ae235b
 *
Packit ae235b
 * Values corresponding to @errno codes returned from file operations
Packit ae235b
 * on UNIX. Unlike @errno codes, GFileError values are available on
Packit ae235b
 * all systems, even Windows. The exact meaning of each code depends
Packit ae235b
 * on what sort of file operation you were performing; the UNIX
Packit ae235b
 * documentation gives more details. The following error code descriptions
Packit ae235b
 * come from the GNU C Library manual, and are under the copyright
Packit ae235b
 * of that manual.
Packit ae235b
 *
Packit ae235b
 * It's not very portable to make detailed assumptions about exactly
Packit ae235b
 * which errors will be returned from a given operation. Some errors
Packit ae235b
 * don't occur on some systems, etc., sometimes there are subtle
Packit ae235b
 * differences in when a system will report a given error, etc.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_FILE_ERROR:
Packit ae235b
 *
Packit ae235b
 * Error domain for file operations. Errors in this domain will
Packit ae235b
 * be from the #GFileError enumeration. See #GError for information
Packit ae235b
 * on error domains.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GFileTest:
Packit ae235b
 * @G_FILE_TEST_IS_REGULAR: %TRUE if the file is a regular file
Packit ae235b
 *     (not a directory). Note that this test will also return %TRUE
Packit ae235b
 *     if the tested file is a symlink to a regular file.
Packit ae235b
 * @G_FILE_TEST_IS_SYMLINK: %TRUE if the file is a symlink.
Packit ae235b
 * @G_FILE_TEST_IS_DIR: %TRUE if the file is a directory.
Packit ae235b
 * @G_FILE_TEST_IS_EXECUTABLE: %TRUE if the file is executable.
Packit ae235b
 * @G_FILE_TEST_EXISTS: %TRUE if the file exists. It may or may not
Packit ae235b
 *     be a regular file.
Packit ae235b
 *
Packit ae235b
 * A test to perform on a file using g_file_test().
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mkdir_with_parents:
Packit ae235b
 * @pathname: (type filename): a pathname in the GLib file name encoding
Packit ae235b
 * @mode: permissions to use for newly created directories
Packit ae235b
 *
Packit ae235b
 * Create a directory if it doesn't already exist. Create intermediate
Packit ae235b
 * parent directories as needed, too.
Packit ae235b
 *
Packit ae235b
 * Returns: 0 if the directory already exists, or was successfully
Packit ae235b
 * created. Returns -1 if an error occurred, with errno set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_mkdir_with_parents (const gchar *pathname,
Packit ae235b
		      int          mode)
Packit ae235b
{
Packit ae235b
  gchar *fn, *p;
Packit ae235b
Packit ae235b
  if (pathname == NULL || *pathname == '\0')
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  fn = g_strdup (pathname);
Packit ae235b
Packit ae235b
  if (g_path_is_absolute (fn))
Packit ae235b
    p = (gchar *) g_path_skip_root (fn);
Packit ae235b
  else
Packit ae235b
    p = fn;
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      while (*p && !G_IS_DIR_SEPARATOR (*p))
Packit ae235b
	p++;
Packit ae235b
      
Packit ae235b
      if (!*p)
Packit ae235b
	p = NULL;
Packit ae235b
      else
Packit ae235b
	*p = '\0';
Packit ae235b
      
Packit ae235b
      if (!g_file_test (fn, G_FILE_TEST_EXISTS))
Packit ae235b
	{
Packit ae235b
	  if (g_mkdir (fn, mode) == -1 && errno != EEXIST)
Packit ae235b
	    {
Packit ae235b
	      int errno_save = errno;
Packit ae235b
	      g_free (fn);
Packit ae235b
	      errno = errno_save;
Packit ae235b
	      return -1;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else if (!g_file_test (fn, G_FILE_TEST_IS_DIR))
Packit ae235b
	{
Packit ae235b
	  g_free (fn);
Packit ae235b
	  errno = ENOTDIR;
Packit ae235b
	  return -1;
Packit ae235b
	}
Packit ae235b
      if (p)
Packit ae235b
	{
Packit ae235b
	  *p++ = G_DIR_SEPARATOR;
Packit ae235b
	  while (*p && G_IS_DIR_SEPARATOR (*p))
Packit ae235b
	    p++;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  while (p);
Packit ae235b
Packit ae235b
  g_free (fn);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_test:
Packit ae235b
 * @filename: (type filename): a filename to test in the
Packit ae235b
 *     GLib file name encoding
Packit ae235b
 * @test: bitfield of #GFileTest flags
Packit ae235b
 * 
Packit ae235b
 * Returns %TRUE if any of the tests in the bitfield @test are
Packit ae235b
 * %TRUE. For example, `(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)`
Packit ae235b
 * will return %TRUE if the file exists; the check whether it's a
Packit ae235b
 * directory doesn't matter since the existence test is %TRUE. With
Packit ae235b
 * the current set of available tests, there's no point passing in
Packit ae235b
 * more than one test at a time.
Packit ae235b
 * 
Packit ae235b
 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
Packit ae235b
 * so for a symbolic link to a regular file g_file_test() will return
Packit ae235b
 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
Packit ae235b
 *
Packit ae235b
 * Note, that for a dangling symbolic link g_file_test() will return
Packit ae235b
 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
Packit ae235b
 *
Packit ae235b
 * You should never use g_file_test() to test whether it is safe
Packit ae235b
 * to perform an operation, because there is always the possibility
Packit ae235b
 * of the condition changing before you actually perform the operation.
Packit ae235b
 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
Packit ae235b
 * to know whether it is safe to write to a file without being
Packit ae235b
 * tricked into writing into a different location. It doesn't work!
Packit ae235b
 * |[
Packit ae235b
 *  // DON'T DO THIS
Packit ae235b
 *  if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) 
Packit ae235b
 *    {
Packit ae235b
 *      fd = g_open (filename, O_WRONLY);
Packit ae235b
 *      // write to fd
Packit ae235b
 *    }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Another thing to note is that %G_FILE_TEST_EXISTS and
Packit ae235b
 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
Packit ae235b
 * system call. This usually doesn't matter, but if your program
Packit ae235b
 * is setuid or setgid it means that these tests will give you
Packit ae235b
 * the answer for the real user ID and group ID, rather than the
Packit ae235b
 * effective user ID and group ID.
Packit ae235b
 *
Packit ae235b
 * On Windows, there are no symlinks, so testing for
Packit ae235b
 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
Packit ae235b
 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
Packit ae235b
 * its name indicates that it is executable, checking for well-known
Packit ae235b
 * extensions and those listed in the `PATHEXT` environment variable.
Packit ae235b
 *
Packit ae235b
 * Returns: whether a test was %TRUE
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_test (const gchar *filename,
Packit ae235b
             GFileTest    test)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  int attributes;
Packit ae235b
  wchar_t *wfilename;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_val_if_fail (filename != NULL, FALSE);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
/* stuff missing in std vc6 api */
Packit ae235b
#  ifndef INVALID_FILE_ATTRIBUTES
Packit ae235b
#    define INVALID_FILE_ATTRIBUTES -1
Packit ae235b
#  endif
Packit ae235b
#  ifndef FILE_ATTRIBUTE_DEVICE
Packit ae235b
#    define FILE_ATTRIBUTE_DEVICE 64
Packit ae235b
#  endif
Packit ae235b
  wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  if (wfilename == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  attributes = GetFileAttributesW (wfilename);
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
Packit ae235b
  if (attributes == INVALID_FILE_ATTRIBUTES)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (test & G_FILE_TEST_EXISTS)
Packit ae235b
    return TRUE;
Packit ae235b
      
Packit ae235b
  if (test & G_FILE_TEST_IS_REGULAR)
Packit ae235b
    {
Packit ae235b
      if ((attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0)
Packit ae235b
	return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (test & G_FILE_TEST_IS_DIR)
Packit ae235b
    {
Packit ae235b
      if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
Packit ae235b
	return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* "while" so that we can exit this "loop" with a simple "break" */
Packit ae235b
  while (test & G_FILE_TEST_IS_EXECUTABLE)
Packit ae235b
    {
Packit ae235b
      const gchar *lastdot = strrchr (filename, '.');
Packit ae235b
      const gchar *pathext = NULL, *p;
Packit ae235b
      int extlen;
Packit ae235b
Packit ae235b
      if (lastdot == NULL)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      if (_stricmp (lastdot, ".exe") == 0 ||
Packit ae235b
	  _stricmp (lastdot, ".cmd") == 0 ||
Packit ae235b
	  _stricmp (lastdot, ".bat") == 0 ||
Packit ae235b
	  _stricmp (lastdot, ".com") == 0)
Packit ae235b
	return TRUE;
Packit ae235b
Packit ae235b
      /* Check if it is one of the types listed in %PATHEXT% */
Packit ae235b
Packit ae235b
      pathext = g_getenv ("PATHEXT");
Packit ae235b
      if (pathext == NULL)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      pathext = g_utf8_casefold (pathext, -1);
Packit ae235b
Packit ae235b
      lastdot = g_utf8_casefold (lastdot, -1);
Packit ae235b
      extlen = strlen (lastdot);
Packit ae235b
Packit ae235b
      p = pathext;
Packit ae235b
      while (TRUE)
Packit ae235b
	{
Packit ae235b
	  const gchar *q = strchr (p, ';');
Packit ae235b
	  if (q == NULL)
Packit ae235b
	    q = p + strlen (p);
Packit ae235b
	  if (extlen == q - p &&
Packit ae235b
	      memcmp (lastdot, p, extlen) == 0)
Packit ae235b
	    {
Packit ae235b
	      g_free ((gchar *) pathext);
Packit ae235b
	      g_free ((gchar *) lastdot);
Packit ae235b
	      return TRUE;
Packit ae235b
	    }
Packit ae235b
	  if (*q)
Packit ae235b
	    p = q + 1;
Packit ae235b
	  else
Packit ae235b
	    break;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_free ((gchar *) pathext);
Packit ae235b
      g_free ((gchar *) lastdot);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
#else
Packit ae235b
  if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
Packit ae235b
    return TRUE;
Packit ae235b
  
Packit ae235b
  if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
Packit ae235b
    {
Packit ae235b
      if (getuid () != 0)
Packit ae235b
	return TRUE;
Packit ae235b
Packit ae235b
      /* For root, on some POSIX systems, access (filename, X_OK)
Packit ae235b
       * will succeed even if no executable bits are set on the
Packit ae235b
       * file. We fall through to a stat test to avoid that.
Packit ae235b
       */
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    test &= ~G_FILE_TEST_IS_EXECUTABLE;
Packit ae235b
Packit ae235b
  if (test & G_FILE_TEST_IS_SYMLINK)
Packit ae235b
    {
Packit ae235b
      struct stat s;
Packit ae235b
Packit ae235b
      if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
Packit ae235b
        return TRUE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (test & (G_FILE_TEST_IS_REGULAR |
Packit ae235b
	      G_FILE_TEST_IS_DIR |
Packit ae235b
	      G_FILE_TEST_IS_EXECUTABLE))
Packit ae235b
    {
Packit ae235b
      struct stat s;
Packit ae235b
      
Packit ae235b
      if (stat (filename, &s) == 0)
Packit ae235b
	{
Packit ae235b
	  if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
Packit ae235b
	    return TRUE;
Packit ae235b
	  
Packit ae235b
	  if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
Packit ae235b
	    return TRUE;
Packit ae235b
Packit ae235b
	  /* The extra test for root when access (file, X_OK) succeeds.
Packit ae235b
	   */
Packit ae235b
	  if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
Packit ae235b
	      ((s.st_mode & S_IXOTH) ||
Packit ae235b
	       (s.st_mode & S_IXUSR) ||
Packit ae235b
	       (s.st_mode & S_IXGRP)))
Packit ae235b
	    return TRUE;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
G_DEFINE_QUARK (g-file-error-quark, g_file_error)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_error_from_errno:
Packit ae235b
 * @err_no: an "errno" value
Packit ae235b
 * 
Packit ae235b
 * Gets a #GFileError constant based on the passed-in @err_no.
Packit ae235b
 * For example, if you pass in `EEXIST` this function returns
Packit ae235b
 * #G_FILE_ERROR_EXIST. Unlike `errno` values, you can portably
Packit ae235b
 * assume that all #GFileError values will exist.
Packit ae235b
 *
Packit ae235b
 * Normally a #GFileError value goes into a #GError returned
Packit ae235b
 * from a function that manipulates files. So you would use
Packit ae235b
 * g_file_error_from_errno() when constructing a #GError.
Packit ae235b
 * 
Packit ae235b
 * Returns: #GFileError corresponding to the given @errno
Packit ae235b
 **/
Packit ae235b
GFileError
Packit ae235b
g_file_error_from_errno (gint err_no)
Packit ae235b
{
Packit ae235b
  switch (err_no)
Packit ae235b
    {
Packit ae235b
#ifdef EEXIST
Packit ae235b
    case EEXIST:
Packit ae235b
      return G_FILE_ERROR_EXIST;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EISDIR
Packit ae235b
    case EISDIR:
Packit ae235b
      return G_FILE_ERROR_ISDIR;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EACCES
Packit ae235b
    case EACCES:
Packit ae235b
      return G_FILE_ERROR_ACCES;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENAMETOOLONG
Packit ae235b
    case ENAMETOOLONG:
Packit ae235b
      return G_FILE_ERROR_NAMETOOLONG;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOENT
Packit ae235b
    case ENOENT:
Packit ae235b
      return G_FILE_ERROR_NOENT;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOTDIR
Packit ae235b
    case ENOTDIR:
Packit ae235b
      return G_FILE_ERROR_NOTDIR;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENXIO
Packit ae235b
    case ENXIO:
Packit ae235b
      return G_FILE_ERROR_NXIO;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENODEV
Packit ae235b
    case ENODEV:
Packit ae235b
      return G_FILE_ERROR_NODEV;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EROFS
Packit ae235b
    case EROFS:
Packit ae235b
      return G_FILE_ERROR_ROFS;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ETXTBSY
Packit ae235b
    case ETXTBSY:
Packit ae235b
      return G_FILE_ERROR_TXTBSY;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EFAULT
Packit ae235b
    case EFAULT:
Packit ae235b
      return G_FILE_ERROR_FAULT;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ELOOP
Packit ae235b
    case ELOOP:
Packit ae235b
      return G_FILE_ERROR_LOOP;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOSPC
Packit ae235b
    case ENOSPC:
Packit ae235b
      return G_FILE_ERROR_NOSPC;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOMEM
Packit ae235b
    case ENOMEM:
Packit ae235b
      return G_FILE_ERROR_NOMEM;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EMFILE
Packit ae235b
    case EMFILE:
Packit ae235b
      return G_FILE_ERROR_MFILE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENFILE
Packit ae235b
    case ENFILE:
Packit ae235b
      return G_FILE_ERROR_NFILE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EBADF
Packit ae235b
    case EBADF:
Packit ae235b
      return G_FILE_ERROR_BADF;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EINVAL
Packit ae235b
    case EINVAL:
Packit ae235b
      return G_FILE_ERROR_INVAL;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPIPE
Packit ae235b
    case EPIPE:
Packit ae235b
      return G_FILE_ERROR_PIPE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EAGAIN
Packit ae235b
    case EAGAIN:
Packit ae235b
      return G_FILE_ERROR_AGAIN;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EINTR
Packit ae235b
    case EINTR:
Packit ae235b
      return G_FILE_ERROR_INTR;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EIO
Packit ae235b
    case EIO:
Packit ae235b
      return G_FILE_ERROR_IO;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPERM
Packit ae235b
    case EPERM:
Packit ae235b
      return G_FILE_ERROR_PERM;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOSYS
Packit ae235b
    case ENOSYS:
Packit ae235b
      return G_FILE_ERROR_NOSYS;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      return G_FILE_ERROR_FAILED;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
format_error_message (const gchar  *filename,
Packit ae235b
                      const gchar  *format_string,
Packit ae235b
                      int           saved_errno) G_GNUC_FORMAT(2);
Packit ae235b
Packit ae235b
#pragma GCC diagnostic push
Packit ae235b
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
Packit ae235b
Packit ae235b
static char *
Packit ae235b
format_error_message (const gchar  *filename,
Packit ae235b
                      const gchar  *format_string,
Packit ae235b
                      int           saved_errno)
Packit ae235b
{
Packit ae235b
  gchar *display_name;
Packit ae235b
  gchar *msg;
Packit ae235b
Packit ae235b
  display_name = g_filename_display_name (filename);
Packit ae235b
  msg = g_strdup_printf (format_string, display_name, g_strerror (saved_errno));
Packit ae235b
  g_free (display_name);
Packit ae235b
Packit ae235b
  return msg;
Packit ae235b
}
Packit ae235b
Packit ae235b
#pragma GCC diagnostic pop
Packit ae235b
Packit ae235b
/* format string must have two '%s':
Packit ae235b
 *
Packit ae235b
 *   - the place for the filename
Packit ae235b
 *   - the place for the strerror
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
set_file_error (GError      **error,
Packit ae235b
                const gchar  *filename,
Packit ae235b
                const gchar  *format_string,
Packit ae235b
                int           saved_errno)
Packit ae235b
{
Packit ae235b
  char *msg = format_error_message (filename, format_string, saved_errno);
Packit ae235b
Packit ae235b
  g_set_error_literal (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
Packit ae235b
                       msg);
Packit ae235b
  g_free (msg);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_contents_stdio (const gchar  *filename,
Packit ae235b
                    FILE         *f,
Packit ae235b
                    gchar       **contents,
Packit ae235b
                    gsize        *length,
Packit ae235b
                    GError      **error)
Packit ae235b
{
Packit ae235b
  gchar buf[4096];
Packit ae235b
  gsize bytes;  /* always <= sizeof(buf) */
Packit ae235b
  gchar *str = NULL;
Packit ae235b
  gsize total_bytes = 0;
Packit ae235b
  gsize total_allocated = 0;
Packit ae235b
  gchar *tmp;
Packit ae235b
  gchar *display_filename;
Packit ae235b
Packit ae235b
  g_assert (f != NULL);
Packit ae235b
Packit ae235b
  while (!feof (f))
Packit ae235b
    {
Packit ae235b
      gint save_errno;
Packit ae235b
Packit ae235b
      bytes = fread (buf, 1, sizeof (buf), f);
Packit ae235b
      save_errno = errno;
Packit ae235b
Packit ae235b
      if (total_bytes > G_MAXSIZE - bytes)
Packit ae235b
          goto file_too_large;
Packit ae235b
Packit ae235b
      /* Possibility of overflow eliminated above. */
Packit ae235b
      while (total_bytes + bytes >= total_allocated)
Packit ae235b
        {
Packit ae235b
          if (str)
Packit ae235b
            {
Packit ae235b
              if (total_allocated > G_MAXSIZE / 2)
Packit ae235b
                  goto file_too_large;
Packit ae235b
              total_allocated *= 2;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              total_allocated = MIN (bytes + 1, sizeof (buf));
Packit ae235b
            }
Packit ae235b
Packit ae235b
          tmp = g_try_realloc (str, total_allocated);
Packit ae235b
Packit ae235b
          if (tmp == NULL)
Packit ae235b
            {
Packit ae235b
              display_filename = g_filename_display_name (filename);
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_FILE_ERROR,
Packit ae235b
                           G_FILE_ERROR_NOMEM,
Packit ae235b
                           g_dngettext (GETTEXT_PACKAGE, "Could not allocate %lu byte to read file “%s”", "Could not allocate %lu bytes to read file “%s”", (gulong)total_allocated),
Packit ae235b
                           (gulong) total_allocated,
Packit ae235b
			   display_filename);
Packit ae235b
              g_free (display_filename);
Packit ae235b
Packit ae235b
              goto error;
Packit ae235b
            }
Packit ae235b
Packit ae235b
	  str = tmp;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (ferror (f))
Packit ae235b
        {
Packit ae235b
          display_filename = g_filename_display_name (filename);
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_FILE_ERROR,
Packit ae235b
                       g_file_error_from_errno (save_errno),
Packit ae235b
                       _("Error reading file “%s”: %s"),
Packit ae235b
                       display_filename,
Packit ae235b
		       g_strerror (save_errno));
Packit ae235b
          g_free (display_filename);
Packit ae235b
Packit ae235b
          goto error;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_assert (str != NULL);
Packit ae235b
      memcpy (str + total_bytes, buf, bytes);
Packit ae235b
Packit ae235b
      total_bytes += bytes;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  fclose (f);
Packit ae235b
Packit ae235b
  if (total_allocated == 0)
Packit ae235b
    {
Packit ae235b
      str = g_new (gchar, 1);
Packit ae235b
      total_bytes = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  str[total_bytes] = '\0';
Packit ae235b
Packit ae235b
  if (length)
Packit ae235b
    *length = total_bytes;
Packit ae235b
Packit ae235b
  *contents = str;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
Packit ae235b
 file_too_large:
Packit ae235b
  display_filename = g_filename_display_name (filename);
Packit ae235b
  g_set_error (error,
Packit ae235b
               G_FILE_ERROR,
Packit ae235b
               G_FILE_ERROR_FAILED,
Packit ae235b
               _("File “%s” is too large"),
Packit ae235b
               display_filename);
Packit ae235b
  g_free (display_filename);
Packit ae235b
Packit ae235b
 error:
Packit ae235b
Packit ae235b
  g_free (str);
Packit ae235b
  fclose (f);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_contents_regfile (const gchar  *filename,
Packit ae235b
                      struct stat  *stat_buf,
Packit ae235b
                      gint          fd,
Packit ae235b
                      gchar       **contents,
Packit ae235b
                      gsize        *length,
Packit ae235b
                      GError      **error)
Packit ae235b
{
Packit ae235b
  gchar *buf;
Packit ae235b
  gsize bytes_read;
Packit ae235b
  gsize size;
Packit ae235b
  gsize alloc_size;
Packit ae235b
  gchar *display_filename;
Packit ae235b
  
Packit ae235b
  size = stat_buf->st_size;
Packit ae235b
Packit ae235b
  alloc_size = size + 1;
Packit ae235b
  buf = g_try_malloc (alloc_size);
Packit ae235b
Packit ae235b
  if (buf == NULL)
Packit ae235b
    {
Packit ae235b
      display_filename = g_filename_display_name (filename);
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_FILE_ERROR,
Packit ae235b
                   G_FILE_ERROR_NOMEM,
Packit ae235b
                           g_dngettext (GETTEXT_PACKAGE, "Could not allocate %lu byte to read file “%s”", "Could not allocate %lu bytes to read file “%s”", (gulong)alloc_size),
Packit ae235b
                   (gulong) alloc_size, 
Packit ae235b
		   display_filename);
Packit ae235b
      g_free (display_filename);
Packit ae235b
      goto error;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  bytes_read = 0;
Packit ae235b
  while (bytes_read < size)
Packit ae235b
    {
Packit ae235b
      gssize rc;
Packit ae235b
          
Packit ae235b
      rc = read (fd, buf + bytes_read, size - bytes_read);
Packit ae235b
Packit ae235b
      if (rc < 0)
Packit ae235b
        {
Packit ae235b
          if (errno != EINTR) 
Packit ae235b
            {
Packit ae235b
	      int save_errno = errno;
Packit ae235b
Packit ae235b
              g_free (buf);
Packit ae235b
              display_filename = g_filename_display_name (filename);
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_FILE_ERROR,
Packit ae235b
                           g_file_error_from_errno (save_errno),
Packit ae235b
                           _("Failed to read from file “%s”: %s"),
Packit ae235b
                           display_filename, 
Packit ae235b
			   g_strerror (save_errno));
Packit ae235b
              g_free (display_filename);
Packit ae235b
	      goto error;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else if (rc == 0)
Packit ae235b
        break;
Packit ae235b
      else
Packit ae235b
        bytes_read += rc;
Packit ae235b
    }
Packit ae235b
      
Packit ae235b
  buf[bytes_read] = '\0';
Packit ae235b
Packit ae235b
  if (length)
Packit ae235b
    *length = bytes_read;
Packit ae235b
  
Packit ae235b
  *contents = buf;
Packit ae235b
Packit ae235b
  close (fd);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
Packit ae235b
 error:
Packit ae235b
Packit ae235b
  close (fd);
Packit ae235b
  
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_contents_posix (const gchar  *filename,
Packit ae235b
                    gchar       **contents,
Packit ae235b
                    gsize        *length,
Packit ae235b
                    GError      **error)
Packit ae235b
{
Packit ae235b
  struct stat stat_buf;
Packit ae235b
  gint fd;
Packit ae235b
Packit ae235b
  /* O_BINARY useful on Cygwin */
Packit ae235b
  fd = open (filename, O_RDONLY|O_BINARY);
Packit ae235b
Packit ae235b
  if (fd < 0)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
      set_file_error (error,
Packit ae235b
                      filename,
Packit ae235b
                      _("Failed to open file “%s”: %s"),
Packit ae235b
                      saved_errno);
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* I don't think this will ever fail, aside from ENOMEM, but. */
Packit ae235b
  if (fstat (fd, &stat_buf) < 0)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
      set_file_error (error,
Packit ae235b
                      filename,
Packit ae235b
                      _("Failed to get attributes of file “%s”: fstat() failed: %s"),
Packit ae235b
                      saved_errno);
Packit ae235b
      close (fd);
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
Packit ae235b
    {
Packit ae235b
      gboolean retval = get_contents_regfile (filename,
Packit ae235b
					      &stat_buf,
Packit ae235b
					      fd,
Packit ae235b
					      contents,
Packit ae235b
					      length,
Packit ae235b
					      error);
Packit ae235b
Packit ae235b
      return retval;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      FILE *f;
Packit ae235b
      gboolean retval;
Packit ae235b
Packit ae235b
      f = fdopen (fd, "r");
Packit ae235b
      
Packit ae235b
      if (f == NULL)
Packit ae235b
        {
Packit ae235b
          int saved_errno = errno;
Packit ae235b
          set_file_error (error,
Packit ae235b
                          filename,
Packit ae235b
                          _("Failed to open file “%s”: fdopen() failed: %s"),
Packit ae235b
                          saved_errno);
Packit ae235b
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
  
Packit ae235b
      retval = get_contents_stdio (filename, f, contents, length, error);
Packit ae235b
Packit ae235b
      return retval;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
#else  /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_contents_win32 (const gchar  *filename,
Packit ae235b
		    gchar       **contents,
Packit ae235b
		    gsize        *length,
Packit ae235b
		    GError      **error)
Packit ae235b
{
Packit ae235b
  FILE *f;
Packit ae235b
  gboolean retval;
Packit ae235b
  
Packit ae235b
  f = g_fopen (filename, "rb");
Packit ae235b
Packit ae235b
  if (f == NULL)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
      set_file_error (error,
Packit ae235b
                      filename,
Packit ae235b
                      _("Failed to open file “%s”: %s"),
Packit ae235b
                      saved_errno);
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  retval = get_contents_stdio (filename, f, contents, length, error);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_get_contents:
Packit ae235b
 * @filename: (type filename): name of a file to read contents from, in the GLib file name encoding
Packit ae235b
 * @contents: (out) (array length=length) (element-type guint8): location to store an allocated string, use g_free() to free
Packit ae235b
 *     the returned string
Packit ae235b
 * @length: (nullable): location to store length in bytes of the contents, or %NULL
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Reads an entire file into allocated memory, with good error
Packit ae235b
 * checking.
Packit ae235b
 *
Packit ae235b
 * If the call was successful, it returns %TRUE and sets @contents to the file
Packit ae235b
 * contents and @length to the length of the file contents in bytes. The string
Packit ae235b
 * stored in @contents will be nul-terminated, so for text files you can pass
Packit ae235b
 * %NULL for the @length argument. If the call was not successful, it returns
Packit ae235b
 * %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
Packit ae235b
 * codes are those in the #GFileError enumeration. In the error case,
Packit ae235b
 * @contents is set to %NULL and @length is set to zero.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error occurred
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_get_contents (const gchar  *filename,
Packit ae235b
                     gchar       **contents,
Packit ae235b
                     gsize        *length,
Packit ae235b
                     GError      **error)
Packit ae235b
{  
Packit ae235b
  g_return_val_if_fail (filename != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (contents != NULL, FALSE);
Packit ae235b
Packit ae235b
  *contents = NULL;
Packit ae235b
  if (length)
Packit ae235b
    *length = 0;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  return get_contents_win32 (filename, contents, length, error);
Packit ae235b
#else
Packit ae235b
  return get_contents_posix (filename, contents, length, error);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
rename_file (const char  *old_name,
Packit ae235b
	     const char  *new_name,
Packit ae235b
	     GError     **err)
Packit ae235b
{
Packit ae235b
  errno = 0;
Packit ae235b
  if (g_rename (old_name, new_name) == -1)
Packit ae235b
    {
Packit ae235b
      int save_errno = errno;
Packit ae235b
      gchar *display_old_name = g_filename_display_name (old_name);
Packit ae235b
      gchar *display_new_name = g_filename_display_name (new_name);
Packit ae235b
Packit ae235b
      g_set_error (err,
Packit ae235b
		   G_FILE_ERROR,
Packit ae235b
		   g_file_error_from_errno (save_errno),
Packit ae235b
		   _("Failed to rename file “%s” to “%s”: g_rename() failed: %s"),
Packit ae235b
		   display_old_name,
Packit ae235b
		   display_new_name,
Packit ae235b
		   g_strerror (save_errno));
Packit ae235b
Packit ae235b
      g_free (display_old_name);
Packit ae235b
      g_free (display_new_name);
Packit ae235b
      
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
write_to_temp_file (const gchar  *contents,
Packit ae235b
		    gssize        length,
Packit ae235b
		    const gchar  *dest_file,
Packit ae235b
		    GError      **err)
Packit ae235b
{
Packit ae235b
  gchar *tmp_name;
Packit ae235b
  gchar *retval;
Packit ae235b
  gint fd;
Packit ae235b
Packit ae235b
  retval = NULL;
Packit ae235b
Packit ae235b
  tmp_name = g_strdup_printf ("%s.XXXXXX", dest_file);
Packit ae235b
Packit ae235b
  errno = 0;
Packit ae235b
  fd = g_mkstemp_full (tmp_name, O_RDWR | O_BINARY, 0666);
Packit ae235b
Packit ae235b
  if (fd == -1)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
      set_file_error (err,
Packit ae235b
                      tmp_name, _("Failed to create file “%s”: %s"),
Packit ae235b
                      saved_errno);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef HAVE_FALLOCATE
Packit ae235b
  if (length > 0)
Packit ae235b
    {
Packit ae235b
      /* We do this on a 'best effort' basis... It may not be supported
Packit ae235b
       * on the underlying filesystem.
Packit ae235b
       */
Packit ae235b
      (void) fallocate (fd, 0, 0, length);
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
  while (length > 0)
Packit ae235b
    {
Packit ae235b
      gssize s;
Packit ae235b
Packit ae235b
      s = write (fd, contents, length);
Packit ae235b
Packit ae235b
      if (s < 0)
Packit ae235b
        {
Packit ae235b
          int saved_errno = errno;
Packit ae235b
          if (saved_errno == EINTR)
Packit ae235b
            continue;
Packit ae235b
Packit ae235b
          set_file_error (err,
Packit ae235b
                          tmp_name, _("Failed to write file “%s”: write() failed: %s"),
Packit ae235b
                          saved_errno);
Packit ae235b
          close (fd);
Packit ae235b
          g_unlink (tmp_name);
Packit ae235b
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_assert (s <= length);
Packit ae235b
Packit ae235b
      contents += s;
Packit ae235b
      length -= s;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef BTRFS_SUPER_MAGIC
Packit ae235b
  {
Packit ae235b
    struct statfs buf;
Packit ae235b
Packit ae235b
    /* On Linux, on btrfs, skip the fsync since rename-over-existing is
Packit ae235b
     * guaranteed to be atomic and this is the only case in which we
Packit ae235b
     * would fsync() anyway.
Packit ae235b
     */
Packit ae235b
Packit ae235b
    if (fstatfs (fd, &buf) == 0 && buf.f_type == BTRFS_SUPER_MAGIC)
Packit ae235b
      goto no_fsync;
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_FSYNC
Packit ae235b
  {
Packit ae235b
    struct stat statbuf;
Packit ae235b
Packit ae235b
    errno = 0;
Packit ae235b
    /* If the final destination exists and is > 0 bytes, we want to sync the
Packit ae235b
     * newly written file to ensure the data is on disk when we rename over
Packit ae235b
     * the destination. Otherwise if we get a system crash we can lose both
Packit ae235b
     * the new and the old file on some filesystems. (I.E. those that don't
Packit ae235b
     * guarantee the data is written to the disk before the metadata.)
Packit ae235b
     */
Packit ae235b
    if (g_lstat (dest_file, &statbuf) == 0 && statbuf.st_size > 0 && fsync (fd) != 0)
Packit ae235b
      {
Packit ae235b
        int saved_errno = errno;
Packit ae235b
        set_file_error (err,
Packit ae235b
                        tmp_name, _("Failed to write file “%s”: fsync() failed: %s"),
Packit ae235b
                        saved_errno);
Packit ae235b
        close (fd);
Packit ae235b
        g_unlink (tmp_name);
Packit ae235b
Packit ae235b
        goto out;
Packit ae235b
      }
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef BTRFS_SUPER_MAGIC
Packit ae235b
 no_fsync:
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  errno = 0;
Packit ae235b
  if (!g_close (fd, err))
Packit ae235b
    {
Packit ae235b
      g_unlink (tmp_name);
Packit ae235b
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = g_strdup (tmp_name);
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_free (tmp_name);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_set_contents:
Packit ae235b
 * @filename: (type filename): name of a file to write @contents to, in the GLib file name
Packit ae235b
 *   encoding
Packit ae235b
 * @contents: (array length=length) (element-type guint8): string to write to the file
Packit ae235b
 * @length: length of @contents, or -1 if @contents is a nul-terminated string
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Writes all of @contents to a file named @filename, with good error checking.
Packit ae235b
 * If a file called @filename already exists it will be overwritten.
Packit ae235b
 *
Packit ae235b
 * This write is atomic in the sense that it is first written to a temporary
Packit ae235b
 * file which is then renamed to the final name. Notes:
Packit ae235b
 *
Packit ae235b
 * - On UNIX, if @filename already exists hard links to @filename will break.
Packit ae235b
 *   Also since the file is recreated, existing permissions, access control
Packit ae235b
 *   lists, metadata etc. may be lost. If @filename is a symbolic link,
Packit ae235b
 *   the link itself will be replaced, not the linked file.
Packit ae235b
 *
Packit ae235b
 * - On Windows renaming a file will not remove an existing file with the
Packit ae235b
 *   new name, so on Windows there is a race condition between the existing
Packit ae235b
 *   file being removed and the temporary file being renamed.
Packit ae235b
 *
Packit ae235b
 * - On Windows there is no way to remove a file that is open to some
Packit ae235b
 *   process, or mapped into memory. Thus, this function will fail if
Packit ae235b
 *   @filename already exists and is open.
Packit ae235b
 *
Packit ae235b
 * If the call was successful, it returns %TRUE. If the call was not successful,
Packit ae235b
 * it returns %FALSE and sets @error. The error domain is #G_FILE_ERROR.
Packit ae235b
 * Possible error codes are those in the #GFileError enumeration.
Packit ae235b
 *
Packit ae235b
 * Note that the name for the temporary file is constructed by appending up
Packit ae235b
 * to 7 characters to @filename.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error occurred
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_file_set_contents (const gchar  *filename,
Packit ae235b
		     const gchar  *contents,
Packit ae235b
		     gssize	   length,
Packit ae235b
		     GError	 **error)
Packit ae235b
{
Packit ae235b
  gchar *tmp_filename;
Packit ae235b
  gboolean retval;
Packit ae235b
  GError *rename_error = NULL;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (filename != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (contents != NULL || length == 0, FALSE);
Packit ae235b
  g_return_val_if_fail (length >= -1, FALSE);
Packit ae235b
  
Packit ae235b
  if (length == -1)
Packit ae235b
    length = strlen (contents);
Packit ae235b
Packit ae235b
  tmp_filename = write_to_temp_file (contents, length, filename, error);
Packit ae235b
  
Packit ae235b
  if (!tmp_filename)
Packit ae235b
    {
Packit ae235b
      retval = FALSE;
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!rename_file (tmp_filename, filename, &rename_error))
Packit ae235b
    {
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
      g_unlink (tmp_filename);
Packit ae235b
      g_propagate_error (error, rename_error);
Packit ae235b
      retval = FALSE;
Packit ae235b
      goto out;
Packit ae235b
Packit ae235b
#else /* G_OS_WIN32 */
Packit ae235b
      
Packit ae235b
      /* Renaming failed, but on Windows this may just mean
Packit ae235b
       * the file already exists. So if the target file
Packit ae235b
       * exists, try deleting it and do the rename again.
Packit ae235b
       */
Packit ae235b
      if (!g_file_test (filename, G_FILE_TEST_EXISTS))
Packit ae235b
	{
Packit ae235b
	  g_unlink (tmp_filename);
Packit ae235b
	  g_propagate_error (error, rename_error);
Packit ae235b
	  retval = FALSE;
Packit ae235b
	  goto out;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_error_free (rename_error);
Packit ae235b
      
Packit ae235b
      if (g_unlink (filename) == -1)
Packit ae235b
	{
Packit ae235b
          int saved_errno = errno;
Packit ae235b
          set_file_error (error,
Packit ae235b
                          filename,
Packit ae235b
		          _("Existing file “%s” could not be removed: g_unlink() failed: %s"),
Packit ae235b
                          saved_errno);
Packit ae235b
	  g_unlink (tmp_filename);
Packit ae235b
	  retval = FALSE;
Packit ae235b
	  goto out;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (!rename_file (tmp_filename, filename, error))
Packit ae235b
	{
Packit ae235b
	  g_unlink (tmp_filename);
Packit ae235b
	  retval = FALSE;
Packit ae235b
	  goto out;
Packit ae235b
	}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  retval = TRUE;
Packit ae235b
  
Packit ae235b
 out:
Packit ae235b
  g_free (tmp_filename);
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * get_tmp_file based on the mkstemp implementation from the GNU C library.
Packit ae235b
 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
Packit ae235b
 */
Packit ae235b
typedef gint (*GTmpFileCallback) (const gchar *, gint, gint);
Packit ae235b
Packit ae235b
static gint
Packit ae235b
get_tmp_file (gchar            *tmpl,
Packit ae235b
              GTmpFileCallback  f,
Packit ae235b
              int               flags,
Packit ae235b
              int               mode)
Packit ae235b
{
Packit ae235b
  char *XXXXXX;
Packit ae235b
  int count, fd;
Packit ae235b
  static const char letters[] =
Packit ae235b
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Packit ae235b
  static const int NLETTERS = sizeof (letters) - 1;
Packit ae235b
  glong value;
Packit ae235b
  GTimeVal tv;
Packit ae235b
  static int counter = 0;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (tmpl != NULL, -1);
Packit ae235b
Packit ae235b
  /* find the last occurrence of "XXXXXX" */
Packit ae235b
  XXXXXX = g_strrstr (tmpl, "XXXXXX");
Packit ae235b
Packit ae235b
  if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6))
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Get some more or less random data.  */
Packit ae235b
  g_get_current_time (&tv;;
Packit ae235b
  value = (tv.tv_usec ^ tv.tv_sec) + counter++;
Packit ae235b
Packit ae235b
  for (count = 0; count < 100; value += 7777, ++count)
Packit ae235b
    {
Packit ae235b
      glong v = value;
Packit ae235b
Packit ae235b
      /* Fill in the random bits.  */
Packit ae235b
      XXXXXX[0] = letters[v % NLETTERS];
Packit ae235b
      v /= NLETTERS;
Packit ae235b
      XXXXXX[1] = letters[v % NLETTERS];
Packit ae235b
      v /= NLETTERS;
Packit ae235b
      XXXXXX[2] = letters[v % NLETTERS];
Packit ae235b
      v /= NLETTERS;
Packit ae235b
      XXXXXX[3] = letters[v % NLETTERS];
Packit ae235b
      v /= NLETTERS;
Packit ae235b
      XXXXXX[4] = letters[v % NLETTERS];
Packit ae235b
      v /= NLETTERS;
Packit ae235b
      XXXXXX[5] = letters[v % NLETTERS];
Packit ae235b
Packit ae235b
      fd = f (tmpl, flags, mode);
Packit ae235b
Packit ae235b
      if (fd >= 0)
Packit ae235b
        return fd;
Packit ae235b
      else if (errno != EEXIST)
Packit ae235b
        /* Any other error will apply also to other names we might
Packit ae235b
         *  try, and there are 2^32 or so of them, so give up now.
Packit ae235b
         */
Packit ae235b
        return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* We got out of the loop because we ran out of combinations to try.  */
Packit ae235b
  errno = EEXIST;
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Some GTmpFileCallback implementations.
Packit ae235b
 *
Packit ae235b
 * Note: we cannot use open() or g_open() directly because even though
Packit ae235b
 * they appear compatible, they may be vararg functions and calling
Packit ae235b
 * varargs functions through a non-varargs type is undefined.
Packit ae235b
 */
Packit ae235b
static gint
Packit ae235b
wrap_g_mkdir (const gchar *filename,
Packit ae235b
              int          flags G_GNUC_UNUSED,
Packit ae235b
              int          mode)
Packit ae235b
{
Packit ae235b
  /* tmpl is in UTF-8 on Windows, thus use g_mkdir() */
Packit ae235b
  return g_mkdir (filename, mode);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
wrap_g_open (const gchar *filename,
Packit ae235b
                int          flags,
Packit ae235b
                int          mode)
Packit ae235b
{
Packit ae235b
  return g_open (filename, flags, mode);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mkdtemp_full: (skip)
Packit ae235b
 * @tmpl: (type filename): template directory name
Packit ae235b
 * @mode: permissions to create the temporary directory with
Packit ae235b
 *
Packit ae235b
 * Creates a temporary directory. See the mkdtemp() documentation
Packit ae235b
 * on most UNIX-like systems.
Packit ae235b
 *
Packit ae235b
 * The parameter is a string that should follow the rules for
Packit ae235b
 * mkdtemp() templates, i.e. contain the string "XXXXXX".
Packit ae235b
 * g_mkdtemp_full() is slightly more flexible than mkdtemp() in that the
Packit ae235b
 * sequence does not have to occur at the very end of the template
Packit ae235b
 * and you can pass a @mode. The X string will be modified to form
Packit ae235b
 * the name of a directory that didn't exist. The string should be
Packit ae235b
 * in the GLib file name encoding. Most importantly, on Windows it
Packit ae235b
 * should be in UTF-8.
Packit ae235b
 *
Packit ae235b
 * If you are going to be creating a temporary directory inside the
Packit ae235b
 * directory returned by g_get_tmp_dir(), you might want to use
Packit ae235b
 * g_dir_make_tmp() instead.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable) (type filename): A pointer to @tmpl, which has been
Packit ae235b
 *     modified to hold the directory name. In case of errors, %NULL is
Packit ae235b
 *     returned, and %errno will be set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_mkdtemp_full (gchar *tmpl,
Packit ae235b
                gint   mode)
Packit ae235b
{
Packit ae235b
  if (get_tmp_file (tmpl, wrap_g_mkdir, 0, mode) == -1)
Packit ae235b
    return NULL;
Packit ae235b
  else
Packit ae235b
    return tmpl;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mkdtemp: (skip)
Packit ae235b
 * @tmpl: (type filename): template directory name
Packit ae235b
 *
Packit ae235b
 * Creates a temporary directory. See the mkdtemp() documentation
Packit ae235b
 * on most UNIX-like systems.
Packit ae235b
 *
Packit ae235b
 * The parameter is a string that should follow the rules for
Packit ae235b
 * mkdtemp() templates, i.e. contain the string "XXXXXX".
Packit ae235b
 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the
Packit ae235b
 * sequence does not have to occur at the very end of the template.
Packit ae235b
 * The X string will be modified to form the name of a directory that
Packit ae235b
 * didn't exist.
Packit ae235b
 * The string should be in the GLib file name encoding. Most importantly,
Packit ae235b
 * on Windows it should be in UTF-8.
Packit ae235b
 *
Packit ae235b
 * If you are going to be creating a temporary directory inside the
Packit ae235b
 * directory returned by g_get_tmp_dir(), you might want to use
Packit ae235b
 * g_dir_make_tmp() instead.
Packit ae235b
 *
Packit ae235b
 * Returns: (nullable) (type filename): A pointer to @tmpl, which has been
Packit ae235b
 *     modified to hold the directory name.  In case of errors, %NULL is
Packit ae235b
 *     returned and %errno will be set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_mkdtemp (gchar *tmpl)
Packit ae235b
{
Packit ae235b
  return g_mkdtemp_full (tmpl, 0700);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mkstemp_full: (skip)
Packit ae235b
 * @tmpl: (type filename): template filename
Packit ae235b
 * @flags: flags to pass to an open() call in addition to O_EXCL
Packit ae235b
 *     and O_CREAT, which are passed automatically
Packit ae235b
 * @mode: permissions to create the temporary file with
Packit ae235b
 *
Packit ae235b
 * Opens a temporary file. See the mkstemp() documentation
Packit ae235b
 * on most UNIX-like systems.
Packit ae235b
 *
Packit ae235b
 * The parameter is a string that should follow the rules for
Packit ae235b
 * mkstemp() templates, i.e. contain the string "XXXXXX".
Packit ae235b
 * g_mkstemp_full() is slightly more flexible than mkstemp()
Packit ae235b
 * in that the sequence does not have to occur at the very end of the
Packit ae235b
 * template and you can pass a @mode and additional @flags. The X
Packit ae235b
 * string will be modified to form the name of a file that didn't exist.
Packit ae235b
 * The string should be in the GLib file name encoding. Most importantly,
Packit ae235b
 * on Windows it should be in UTF-8.
Packit ae235b
 *
Packit ae235b
 * Returns: A file handle (as from open()) to the file
Packit ae235b
 *     opened for reading and writing. The file handle should be
Packit ae235b
 *     closed with close(). In case of errors, -1 is returned
Packit ae235b
 *     and %errno will be set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_mkstemp_full (gchar *tmpl,
Packit ae235b
                gint   flags,
Packit ae235b
                gint   mode)
Packit ae235b
{
Packit ae235b
  /* tmpl is in UTF-8 on Windows, thus use g_open() */
Packit ae235b
  return get_tmp_file (tmpl, wrap_g_open,
Packit ae235b
                       flags | O_CREAT | O_EXCL, mode);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mkstemp: (skip)
Packit ae235b
 * @tmpl: (type filename): template filename
Packit ae235b
 *
Packit ae235b
 * Opens a temporary file. See the mkstemp() documentation
Packit ae235b
 * on most UNIX-like systems.
Packit ae235b
 *
Packit ae235b
 * The parameter is a string that should follow the rules for
Packit ae235b
 * mkstemp() templates, i.e. contain the string "XXXXXX".
Packit ae235b
 * g_mkstemp() is slightly more flexible than mkstemp() in that the
Packit ae235b
 * sequence does not have to occur at the very end of the template.
Packit ae235b
 * The X string will be modified to form the name of a file that
Packit ae235b
 * didn't exist. The string should be in the GLib file name encoding.
Packit ae235b
 * Most importantly, on Windows it should be in UTF-8.
Packit ae235b
 *
Packit ae235b
 * Returns: A file handle (as from open()) to the file
Packit ae235b
 *     opened for reading and writing. The file is opened in binary
Packit ae235b
 *     mode on platforms where there is a difference. The file handle
Packit ae235b
 *     should be closed with close(). In case of errors, -1 is
Packit ae235b
 *     returned and %errno will be set.
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_mkstemp (gchar *tmpl)
Packit ae235b
{
Packit ae235b
  return g_mkstemp_full (tmpl, O_RDWR | O_BINARY, 0600);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
g_get_tmp_name (const gchar      *tmpl,
Packit ae235b
                gchar           **name_used,
Packit ae235b
                GTmpFileCallback  f,
Packit ae235b
                gint              flags,
Packit ae235b
                gint              mode,
Packit ae235b
                GError          **error)
Packit ae235b
{
Packit ae235b
  int retval;
Packit ae235b
  const char *tmpdir;
Packit ae235b
  const char *sep;
Packit ae235b
  char *fulltemplate;
Packit ae235b
  const char *slash;
Packit ae235b
Packit ae235b
  if (tmpl == NULL)
Packit ae235b
    tmpl = ".XXXXXX";
Packit ae235b
Packit ae235b
  if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      || (strchr (tmpl, '/') != NULL && (slash = "/"))
Packit ae235b
#endif
Packit ae235b
      )
Packit ae235b
    {
Packit ae235b
      gchar *display_tmpl = g_filename_display_name (tmpl);
Packit ae235b
      char c[2];
Packit ae235b
      c[0] = *slash;
Packit ae235b
      c[1] = '\0';
Packit ae235b
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_FILE_ERROR,
Packit ae235b
                   G_FILE_ERROR_FAILED,
Packit ae235b
                   _("Template “%s” invalid, should not contain a “%s”"),
Packit ae235b
                   display_tmpl, c);
Packit ae235b
      g_free (display_tmpl);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (strstr (tmpl, "XXXXXX") == NULL)
Packit ae235b
    {
Packit ae235b
      gchar *display_tmpl = g_filename_display_name (tmpl);
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_FILE_ERROR,
Packit ae235b
                   G_FILE_ERROR_FAILED,
Packit ae235b
                   _("Template “%s” doesn’t contain XXXXXX"),
Packit ae235b
                   display_tmpl);
Packit ae235b
      g_free (display_tmpl);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  tmpdir = g_get_tmp_dir ();
Packit ae235b
Packit ae235b
  if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
Packit ae235b
    sep = "";
Packit ae235b
  else
Packit ae235b
    sep = G_DIR_SEPARATOR_S;
Packit ae235b
Packit ae235b
  fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
Packit ae235b
Packit ae235b
  retval = get_tmp_file (fulltemplate, f, flags, mode);
Packit ae235b
  if (retval == -1)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
      set_file_error (error,
Packit ae235b
                      fulltemplate,
Packit ae235b
                      _("Failed to create file “%s”: %s"),
Packit ae235b
                      saved_errno);
Packit ae235b
      g_free (fulltemplate);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *name_used = fulltemplate;
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_open_tmp:
Packit ae235b
 * @tmpl: (type filename) (nullable): Template for file name, as in
Packit ae235b
 *     g_mkstemp(), basename only, or %NULL for a default template
Packit ae235b
 * @name_used: (out) (type filename): location to store actual name used,
Packit ae235b
 *     or %NULL
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Opens a file for writing in the preferred directory for temporary
Packit ae235b
 * files (as returned by g_get_tmp_dir()).
Packit ae235b
 *
Packit ae235b
 * @tmpl should be a string in the GLib file name encoding containing
Packit ae235b
 * a sequence of six 'X' characters, as the parameter to g_mkstemp().
Packit ae235b
 * However, unlike these functions, the template should only be a
Packit ae235b
 * basename, no directory components are allowed. If template is
Packit ae235b
 * %NULL, a default template is used.
Packit ae235b
 *
Packit ae235b
 * Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
Packit ae235b
 * modified, and might thus be a read-only literal string.
Packit ae235b
 *
Packit ae235b
 * Upon success, and if @name_used is non-%NULL, the actual name used
Packit ae235b
 * is returned in @name_used. This string should be freed with g_free()
Packit ae235b
 * when not needed any longer. The returned name is in the GLib file
Packit ae235b
 * name encoding.
Packit ae235b
 *
Packit ae235b
 * Returns: A file handle (as from open()) to the file opened for
Packit ae235b
 *     reading and writing. The file is opened in binary mode on platforms
Packit ae235b
 *     where there is a difference. The file handle should be closed with
Packit ae235b
 *     close(). In case of errors, -1 is returned and @error will be set.
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_file_open_tmp (const gchar  *tmpl,
Packit ae235b
                 gchar       **name_used,
Packit ae235b
                 GError      **error)
Packit ae235b
{
Packit ae235b
  gchar *fulltemplate;
Packit ae235b
  gint result;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
Packit ae235b
Packit ae235b
  result = g_get_tmp_name (tmpl, &fulltemplate,
Packit ae235b
                           wrap_g_open,
Packit ae235b
                           O_CREAT | O_EXCL | O_RDWR | O_BINARY,
Packit ae235b
                           0600,
Packit ae235b
                           error);
Packit ae235b
  if (result != -1)
Packit ae235b
    {
Packit ae235b
      if (name_used)
Packit ae235b
        *name_used = fulltemplate;
Packit ae235b
      else
Packit ae235b
        g_free (fulltemplate);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dir_make_tmp:
Packit ae235b
 * @tmpl: (type filename) (nullable): Template for directory name,
Packit ae235b
 *     as in g_mkdtemp(), basename only, or %NULL for a default template
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Creates a subdirectory in the preferred directory for temporary
Packit ae235b
 * files (as returned by g_get_tmp_dir()).
Packit ae235b
 *
Packit ae235b
 * @tmpl should be a string in the GLib file name encoding containing
Packit ae235b
 * a sequence of six 'X' characters, as the parameter to g_mkstemp().
Packit ae235b
 * However, unlike these functions, the template should only be a
Packit ae235b
 * basename, no directory components are allowed. If template is
Packit ae235b
 * %NULL, a default template is used.
Packit ae235b
 *
Packit ae235b
 * Note that in contrast to g_mkdtemp() (and mkdtemp()) @tmpl is not
Packit ae235b
 * modified, and might thus be a read-only literal string.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): The actual name used. This string
Packit ae235b
 *     should be freed with g_free() when not needed any longer and is
Packit ae235b
 *     is in the GLib file name encoding. In case of errors, %NULL is
Packit ae235b
 *     returned and @error will be set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_dir_make_tmp (const gchar  *tmpl,
Packit ae235b
                GError      **error)
Packit ae235b
{
Packit ae235b
  gchar *fulltemplate;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  if (g_get_tmp_name (tmpl, &fulltemplate, wrap_g_mkdir, 0, 0700, error) == -1)
Packit ae235b
    return NULL;
Packit ae235b
  else
Packit ae235b
    return fulltemplate;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
g_build_path_va (const gchar  *separator,
Packit ae235b
		 const gchar  *first_element,
Packit ae235b
		 va_list      *args,
Packit ae235b
		 gchar       **str_array)
Packit ae235b
{
Packit ae235b
  GString *result;
Packit ae235b
  gint separator_len = strlen (separator);
Packit ae235b
  gboolean is_first = TRUE;
Packit ae235b
  gboolean have_leading = FALSE;
Packit ae235b
  const gchar *single_element = NULL;
Packit ae235b
  const gchar *next_element;
Packit ae235b
  const gchar *last_trailing = NULL;
Packit ae235b
  gint i = 0;
Packit ae235b
Packit ae235b
  result = g_string_new (NULL);
Packit ae235b
Packit ae235b
  if (str_array)
Packit ae235b
    next_element = str_array[i++];
Packit ae235b
  else
Packit ae235b
    next_element = first_element;
Packit ae235b
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      const gchar *element;
Packit ae235b
      const gchar *start;
Packit ae235b
      const gchar *end;
Packit ae235b
Packit ae235b
      if (next_element)
Packit ae235b
	{
Packit ae235b
	  element = next_element;
Packit ae235b
	  if (str_array)
Packit ae235b
	    next_element = str_array[i++];
Packit ae235b
	  else
Packit ae235b
	    next_element = va_arg (*args, gchar *);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      /* Ignore empty elements */
Packit ae235b
      if (!*element)
Packit ae235b
	continue;
Packit ae235b
      
Packit ae235b
      start = element;
Packit ae235b
Packit ae235b
      if (separator_len)
Packit ae235b
	{
Packit ae235b
	  while (strncmp (start, separator, separator_len) == 0)
Packit ae235b
	    start += separator_len;
Packit ae235b
      	}
Packit ae235b
Packit ae235b
      end = start + strlen (start);
Packit ae235b
      
Packit ae235b
      if (separator_len)
Packit ae235b
	{
Packit ae235b
	  while (end >= start + separator_len &&
Packit ae235b
		 strncmp (end - separator_len, separator, separator_len) == 0)
Packit ae235b
	    end -= separator_len;
Packit ae235b
	  
Packit ae235b
	  last_trailing = end;
Packit ae235b
	  while (last_trailing >= element + separator_len &&
Packit ae235b
		 strncmp (last_trailing - separator_len, separator, separator_len) == 0)
Packit ae235b
	    last_trailing -= separator_len;
Packit ae235b
Packit ae235b
	  if (!have_leading)
Packit ae235b
	    {
Packit ae235b
	      /* If the leading and trailing separator strings are in the
Packit ae235b
	       * same element and overlap, the result is exactly that element
Packit ae235b
	       */
Packit ae235b
	      if (last_trailing <= start)
Packit ae235b
		single_element = element;
Packit ae235b
		  
Packit ae235b
	      g_string_append_len (result, element, start - element);
Packit ae235b
	      have_leading = TRUE;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    single_element = NULL;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (end == start)
Packit ae235b
	continue;
Packit ae235b
Packit ae235b
      if (!is_first)
Packit ae235b
	g_string_append (result, separator);
Packit ae235b
      
Packit ae235b
      g_string_append_len (result, start, end - start);
Packit ae235b
      is_first = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (single_element)
Packit ae235b
    {
Packit ae235b
      g_string_free (result, TRUE);
Packit ae235b
      return g_strdup (single_element);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (last_trailing)
Packit ae235b
	g_string_append (result, last_trailing);
Packit ae235b
  
Packit ae235b
      return g_string_free (result, FALSE);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_build_pathv:
Packit ae235b
 * @separator: a string used to separator the elements of the path.
Packit ae235b
 * @args: (array zero-terminated=1) (element-type filename): %NULL-terminated
Packit ae235b
 *     array of strings containing the path elements.
Packit ae235b
 * 
Packit ae235b
 * Behaves exactly like g_build_path(), but takes the path elements 
Packit ae235b
 * as a string array, instead of varargs. This function is mainly
Packit ae235b
 * meant for language bindings.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): a newly-allocated string that must be freed
Packit ae235b
 *     with g_free().
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_build_pathv (const gchar  *separator,
Packit ae235b
	       gchar       **args)
Packit ae235b
{
Packit ae235b
  if (!args)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return g_build_path_va (separator, NULL, NULL, args);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_build_path:
Packit ae235b
 * @separator: (type filename): a string used to separator the elements of the path.
Packit ae235b
 * @first_element: (type filename): the first element in the path
Packit ae235b
 * @...: remaining elements in path, terminated by %NULL
Packit ae235b
 * 
Packit ae235b
 * Creates a path from a series of elements using @separator as the
Packit ae235b
 * separator between elements. At the boundary between two elements,
Packit ae235b
 * any trailing occurrences of separator in the first element, or
Packit ae235b
 * leading occurrences of separator in the second element are removed
Packit ae235b
 * and exactly one copy of the separator is inserted.
Packit ae235b
 *
Packit ae235b
 * Empty elements are ignored.
Packit ae235b
 *
Packit ae235b
 * The number of leading copies of the separator on the result is
Packit ae235b
 * the same as the number of leading copies of the separator on
Packit ae235b
 * the first non-empty element.
Packit ae235b
 *
Packit ae235b
 * The number of trailing copies of the separator on the result is
Packit ae235b
 * the same as the number of trailing copies of the separator on
Packit ae235b
 * the last non-empty element. (Determination of the number of
Packit ae235b
 * trailing copies is done without stripping leading copies, so
Packit ae235b
 * if the separator is `ABA`, then `ABABA` has 1 trailing copy.)
Packit ae235b
 *
Packit ae235b
 * However, if there is only a single non-empty element, and there
Packit ae235b
 * are no characters in that element not part of the leading or
Packit ae235b
 * trailing separators, then the result is exactly the original value
Packit ae235b
 * of that element.
Packit ae235b
 *
Packit ae235b
 * Other than for determination of the number of leading and trailing
Packit ae235b
 * copies of the separator, elements consisting only of copies
Packit ae235b
 * of the separator are ignored.
Packit ae235b
 * 
Packit ae235b
 * Returns: (type filename): a newly-allocated string that must be freed with
Packit ae235b
 *     g_free().
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_build_path (const gchar *separator,
Packit ae235b
	      const gchar *first_element,
Packit ae235b
	      ...)
Packit ae235b
{
Packit ae235b
  gchar *str;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (separator != NULL, NULL);
Packit ae235b
Packit ae235b
  va_start (args, first_element);
Packit ae235b
  str = g_build_path_va (separator, first_element, &args, NULL);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  return str;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
g_build_pathname_va (const gchar  *first_element,
Packit ae235b
		     va_list      *args,
Packit ae235b
		     gchar       **str_array)
Packit ae235b
{
Packit ae235b
  /* Code copied from g_build_pathv(), and modified to use two
Packit ae235b
   * alternative single-character separators.
Packit ae235b
   */
Packit ae235b
  GString *result;
Packit ae235b
  gboolean is_first = TRUE;
Packit ae235b
  gboolean have_leading = FALSE;
Packit ae235b
  const gchar *single_element = NULL;
Packit ae235b
  const gchar *next_element;
Packit ae235b
  const gchar *last_trailing = NULL;
Packit ae235b
  gchar current_separator = '\\';
Packit ae235b
  gint i = 0;
Packit ae235b
Packit ae235b
  result = g_string_new (NULL);
Packit ae235b
Packit ae235b
  if (str_array)
Packit ae235b
    next_element = str_array[i++];
Packit ae235b
  else
Packit ae235b
    next_element = first_element;
Packit ae235b
  
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      const gchar *element;
Packit ae235b
      const gchar *start;
Packit ae235b
      const gchar *end;
Packit ae235b
Packit ae235b
      if (next_element)
Packit ae235b
	{
Packit ae235b
	  element = next_element;
Packit ae235b
	  if (str_array)
Packit ae235b
	    next_element = str_array[i++];
Packit ae235b
	  else
Packit ae235b
	    next_element = va_arg (*args, gchar *);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      /* Ignore empty elements */
Packit ae235b
      if (!*element)
Packit ae235b
	continue;
Packit ae235b
      
Packit ae235b
      start = element;
Packit ae235b
Packit ae235b
      if (TRUE)
Packit ae235b
	{
Packit ae235b
	  while (start &&
Packit ae235b
		 (*start == '\\' || *start == '/'))
Packit ae235b
	    {
Packit ae235b
	      current_separator = *start;
Packit ae235b
	      start++;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      end = start + strlen (start);
Packit ae235b
      
Packit ae235b
      if (TRUE)
Packit ae235b
	{
Packit ae235b
	  while (end >= start + 1 &&
Packit ae235b
		 (end[-1] == '\\' || end[-1] == '/'))
Packit ae235b
	    {
Packit ae235b
	      current_separator = end[-1];
Packit ae235b
	      end--;
Packit ae235b
	    }
Packit ae235b
	  
Packit ae235b
	  last_trailing = end;
Packit ae235b
	  while (last_trailing >= element + 1 &&
Packit ae235b
		 (last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
Packit ae235b
	    last_trailing--;
Packit ae235b
Packit ae235b
	  if (!have_leading)
Packit ae235b
	    {
Packit ae235b
	      /* If the leading and trailing separator strings are in the
Packit ae235b
	       * same element and overlap, the result is exactly that element
Packit ae235b
	       */
Packit ae235b
	      if (last_trailing <= start)
Packit ae235b
		single_element = element;
Packit ae235b
		  
Packit ae235b
	      g_string_append_len (result, element, start - element);
Packit ae235b
	      have_leading = TRUE;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    single_element = NULL;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (end == start)
Packit ae235b
	continue;
Packit ae235b
Packit ae235b
      if (!is_first)
Packit ae235b
	g_string_append_len (result, &current_separator, 1);
Packit ae235b
      
Packit ae235b
      g_string_append_len (result, start, end - start);
Packit ae235b
      is_first = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (single_element)
Packit ae235b
    {
Packit ae235b
      g_string_free (result, TRUE);
Packit ae235b
      return g_strdup (single_element);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (last_trailing)
Packit ae235b
	g_string_append (result, last_trailing);
Packit ae235b
  
Packit ae235b
      return g_string_free (result, FALSE);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
g_build_filename_va (const gchar  *first_argument,
Packit ae235b
                     va_list      *args,
Packit ae235b
                     gchar       **str_array)
Packit ae235b
{
Packit ae235b
  gchar *str;
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  str = g_build_path_va (G_DIR_SEPARATOR_S, first_argument, args, str_array);
Packit ae235b
#else
Packit ae235b
  str = g_build_pathname_va (first_argument, args, str_array);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return str;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_build_filename_valist:
Packit ae235b
 * @first_element: (type filename): the first element in the path
Packit ae235b
 * @args: va_list of remaining elements in path
Packit ae235b
 *
Packit ae235b
 * Behaves exactly like g_build_filename(), but takes the path elements
Packit ae235b
 * as a va_list. This function is mainly meant for language bindings.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): a newly-allocated string that must be freed
Packit ae235b
 *     with g_free().
Packit ae235b
 *
Packit ae235b
 * Since: 2.56
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_build_filename_valist (const gchar  *first_element,
Packit ae235b
                         va_list      *args)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (first_element != NULL, NULL);
Packit ae235b
Packit ae235b
  return g_build_filename_va (first_element, args, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_build_filenamev:
Packit ae235b
 * @args: (array zero-terminated=1) (element-type filename): %NULL-terminated
Packit ae235b
 *     array of strings containing the path elements.
Packit ae235b
 * 
Packit ae235b
 * Behaves exactly like g_build_filename(), but takes the path elements 
Packit ae235b
 * as a string array, instead of varargs. This function is mainly
Packit ae235b
 * meant for language bindings.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): a newly-allocated string that must be freed
Packit ae235b
 *     with g_free().
Packit ae235b
 * 
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_build_filenamev (gchar **args)
Packit ae235b
{
Packit ae235b
  return g_build_filename_va (NULL, NULL, args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_build_filename:
Packit ae235b
 * @first_element: (type filename): the first element in the path
Packit ae235b
 * @...: remaining elements in path, terminated by %NULL
Packit ae235b
 * 
Packit ae235b
 * Creates a filename from a series of elements using the correct
Packit ae235b
 * separator for filenames.
Packit ae235b
 *
Packit ae235b
 * On Unix, this function behaves identically to `g_build_path
Packit ae235b
 * (G_DIR_SEPARATOR_S, first_element, ....)`.
Packit ae235b
 *
Packit ae235b
 * On Windows, it takes into account that either the backslash
Packit ae235b
 * (`\` or slash (`/`) can be used as separator in filenames, but
Packit ae235b
 * otherwise behaves as on UNIX. When file pathname separators need
Packit ae235b
 * to be inserted, the one that last previously occurred in the
Packit ae235b
 * parameters (reading from left to right) is used.
Packit ae235b
 *
Packit ae235b
 * No attempt is made to force the resulting filename to be an absolute
Packit ae235b
 * path. If the first element is a relative path, the result will
Packit ae235b
 * be a relative path. 
Packit ae235b
 * 
Packit ae235b
 * Returns: (type filename): a newly-allocated string that must be freed with
Packit ae235b
 *     g_free().
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_build_filename (const gchar *first_element, 
Packit ae235b
		  ...)
Packit ae235b
{
Packit ae235b
  gchar *str;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, first_element);
Packit ae235b
  str = g_build_filename_va (first_element, &args, NULL);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  return str;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_read_link:
Packit ae235b
 * @filename: (type filename): the symbolic link
Packit ae235b
 * @error: return location for a #GError
Packit ae235b
 *
Packit ae235b
 * Reads the contents of the symbolic link @filename like the POSIX
Packit ae235b
 * readlink() function.  The returned string is in the encoding used
Packit ae235b
 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): A newly-allocated string with the contents of
Packit ae235b
 *     the symbolic link, or %NULL if an error occurred.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_file_read_link (const gchar  *filename,
Packit ae235b
	          GError      **error)
Packit ae235b
{
Packit ae235b
#if defined (HAVE_READLINK) || defined (G_OS_WIN32)
Packit ae235b
  gchar *buffer;
Packit ae235b
  size_t size;
Packit ae235b
  gssize read_size;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (filename != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  size = 256;
Packit ae235b
  buffer = g_malloc (size);
Packit ae235b
  
Packit ae235b
  while (TRUE) 
Packit ae235b
    {
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
      read_size = readlink (filename, buffer, size);
Packit ae235b
#else
Packit ae235b
      read_size = g_win32_readlink_utf8 (filename, buffer, size);
Packit ae235b
#endif
Packit ae235b
      if (read_size < 0)
Packit ae235b
        {
Packit ae235b
          int saved_errno = errno;
Packit ae235b
          set_file_error (error,
Packit ae235b
                          filename,
Packit ae235b
                          _("Failed to read the symbolic link “%s”: %s"),
Packit ae235b
                          saved_errno);
Packit ae235b
          g_free (buffer);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
    
Packit ae235b
      if ((size_t) read_size < size)
Packit ae235b
        {
Packit ae235b
          buffer[read_size] = 0;
Packit ae235b
          return buffer;
Packit ae235b
        }
Packit ae235b
      
Packit ae235b
      size *= 2;
Packit ae235b
      buffer = g_realloc (buffer, size);
Packit ae235b
    }
Packit ae235b
#else
Packit ae235b
  g_return_val_if_fail (filename != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_FILE_ERROR,
Packit ae235b
                       G_FILE_ERROR_INVAL,
Packit ae235b
                       _("Symbolic links not supported"));
Packit ae235b
	
Packit ae235b
  return NULL;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_path_is_absolute:
Packit ae235b
 * @file_name: (type filename): a file name
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if the given @file_name is an absolute file name.
Packit ae235b
 * Note that this is a somewhat vague concept on Windows.
Packit ae235b
 *
Packit ae235b
 * On POSIX systems, an absolute file name is well-defined. It always
Packit ae235b
 * starts from the single root directory. For example "/usr/local".
Packit ae235b
 *
Packit ae235b
 * On Windows, the concepts of current drive and drive-specific
Packit ae235b
 * current directory introduce vagueness. This function interprets as
Packit ae235b
 * an absolute file name one that either begins with a directory
Packit ae235b
 * separator such as "\Users\tml" or begins with the root on a drive,
Packit ae235b
 * for example "C:\Windows". The first case also includes UNC paths
Packit ae235b
 * such as "\\\\myserver\docs\foo". In all cases, either slashes or
Packit ae235b
 * backslashes are accepted.
Packit ae235b
 *
Packit ae235b
 * Note that a file name relative to the current drive root does not
Packit ae235b
 * truly specify a file uniquely over time and across processes, as
Packit ae235b
 * the current drive is a per-process value and can be changed.
Packit ae235b
 *
Packit ae235b
 * File names relative the current directory on some specific drive,
Packit ae235b
 * such as "D:foo/bar", are not interpreted as absolute by this
Packit ae235b
 * function, but they obviously are not relative to the normal current
Packit ae235b
 * directory as returned by getcwd() or g_get_current_dir()
Packit ae235b
 * either. Such paths should be avoided, or need to be handled using
Packit ae235b
 * Windows-specific code.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @file_name is absolute
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_path_is_absolute (const gchar *file_name)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (file_name != NULL, FALSE);
Packit ae235b
Packit ae235b
  if (G_IS_DIR_SEPARATOR (file_name[0]))
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  /* Recognize drive letter on native Windows */
Packit ae235b
  if (g_ascii_isalpha (file_name[0]) &&
Packit ae235b
      file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
Packit ae235b
    return TRUE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_path_skip_root:
Packit ae235b
 * @file_name: (type filename): a file name
Packit ae235b
 *
Packit ae235b
 * Returns a pointer into @file_name after the root component,
Packit ae235b
 * i.e. after the "/" in UNIX or "C:\" under Windows. If @file_name
Packit ae235b
 * is not an absolute path it returns %NULL.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename) (nullable): a pointer into @file_name after the
Packit ae235b
 *     root component
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_path_skip_root (const gchar *file_name)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (file_name != NULL, NULL);
Packit ae235b
Packit ae235b
#ifdef G_PLATFORM_WIN32
Packit ae235b
  /* Skip \\server\share or //server/share */
Packit ae235b
  if (G_IS_DIR_SEPARATOR (file_name[0]) &&
Packit ae235b
      G_IS_DIR_SEPARATOR (file_name[1]) &&
Packit ae235b
      file_name[2] &&
Packit ae235b
      !G_IS_DIR_SEPARATOR (file_name[2]))
Packit ae235b
    {
Packit ae235b
      gchar *p;
Packit ae235b
      p = strchr (file_name + 2, G_DIR_SEPARATOR);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      {
Packit ae235b
        gchar *q;
Packit ae235b
Packit ae235b
        q = strchr (file_name + 2, '/');
Packit ae235b
        if (p == NULL || (q != NULL && q < p))
Packit ae235b
        p = q;
Packit ae235b
      }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      if (p && p > file_name + 2 && p[1])
Packit ae235b
        {
Packit ae235b
          file_name = p + 1;
Packit ae235b
Packit ae235b
          while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
Packit ae235b
            file_name++;
Packit ae235b
Packit ae235b
          /* Possibly skip a backslash after the share name */
Packit ae235b
          if (G_IS_DIR_SEPARATOR (file_name[0]))
Packit ae235b
            file_name++;
Packit ae235b
Packit ae235b
          return (gchar *)file_name;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  /* Skip initial slashes */
Packit ae235b
  if (G_IS_DIR_SEPARATOR (file_name[0]))
Packit ae235b
    {
Packit ae235b
      while (G_IS_DIR_SEPARATOR (file_name[0]))
Packit ae235b
        file_name++;
Packit ae235b
      return (gchar *)file_name;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  /* Skip X:\ */
Packit ae235b
  if (g_ascii_isalpha (file_name[0]) &&
Packit ae235b
      file_name[1] == ':' &&
Packit ae235b
      G_IS_DIR_SEPARATOR (file_name[2]))
Packit ae235b
    return (gchar *)file_name + 3;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_basename:
Packit ae235b
 * @file_name: (type filename): the name of the file
Packit ae235b
 *
Packit ae235b
 * Gets the name of the file without any leading directory
Packit ae235b
 * components. It returns a pointer into the given file name
Packit ae235b
 * string.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): the name of the file without any leading
Packit ae235b
 *     directory components
Packit ae235b
 *
Packit ae235b
 * Deprecated:2.2: Use g_path_get_basename() instead, but notice
Packit ae235b
 *     that g_path_get_basename() allocates new memory for the
Packit ae235b
 *     returned string, unlike this function which returns a pointer
Packit ae235b
 *     into the argument.
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_basename (const gchar *file_name)
Packit ae235b
{
Packit ae235b
  gchar *base;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (file_name != NULL, NULL);
Packit ae235b
Packit ae235b
  base = strrchr (file_name, G_DIR_SEPARATOR);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  {
Packit ae235b
    gchar *q;
Packit ae235b
    q = strrchr (file_name, '/');
Packit ae235b
    if (base == NULL || (q != NULL && q > base))
Packit ae235b
      base = q;
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (base)
Packit ae235b
    return base + 1;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
Packit ae235b
    return (gchar*) file_name + 2;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return (gchar*) file_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_path_get_basename:
Packit ae235b
 * @file_name: (type filename): the name of the file
Packit ae235b
 *
Packit ae235b
 * Gets the last component of the filename.
Packit ae235b
 *
Packit ae235b
 * If @file_name ends with a directory separator it gets the component
Packit ae235b
 * before the last slash. If @file_name consists only of directory
Packit ae235b
 * separators (and on Windows, possibly a drive letter), a single
Packit ae235b
 * separator is returned. If @file_name is empty, it gets ".".
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): a newly allocated string containing the last
Packit ae235b
 *    component of the filename
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_path_get_basename (const gchar *file_name)
Packit ae235b
{
Packit ae235b
  gssize base;
Packit ae235b
  gssize last_nonslash;
Packit ae235b
  gsize len;
Packit ae235b
  gchar *retval;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (file_name != NULL, NULL);
Packit ae235b
Packit ae235b
  if (file_name[0] == '\0')
Packit ae235b
    return g_strdup (".");
Packit ae235b
Packit ae235b
  last_nonslash = strlen (file_name) - 1;
Packit ae235b
Packit ae235b
  while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
Packit ae235b
    last_nonslash--;
Packit ae235b
Packit ae235b
  if (last_nonslash == -1)
Packit ae235b
    /* string only containing slashes */
Packit ae235b
    return g_strdup (G_DIR_SEPARATOR_S);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  if (last_nonslash == 1 &&
Packit ae235b
      g_ascii_isalpha (file_name[0]) &&
Packit ae235b
      file_name[1] == ':')
Packit ae235b
    /* string only containing slashes and a drive */
Packit ae235b
    return g_strdup (G_DIR_SEPARATOR_S);
Packit ae235b
#endif
Packit ae235b
  base = last_nonslash;
Packit ae235b
Packit ae235b
  while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
Packit ae235b
    base--;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  if (base == -1 &&
Packit ae235b
      g_ascii_isalpha (file_name[0]) &&
Packit ae235b
      file_name[1] == ':')
Packit ae235b
    base = 1;
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
  len = last_nonslash - base;
Packit ae235b
  retval = g_malloc (len + 1);
Packit ae235b
  memcpy (retval, file_name + base + 1, len);
Packit ae235b
  retval [len] = '\0';
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dirname:
Packit ae235b
 * @file_name: (type filename): the name of the file
Packit ae235b
 *
Packit ae235b
 * Gets the directory components of a file name.
Packit ae235b
 *
Packit ae235b
 * If the file name has no directory components "." is returned.
Packit ae235b
 * The returned string should be freed when no longer needed.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): the directory components of the file
Packit ae235b
 *
Packit ae235b
 * Deprecated: use g_path_get_dirname() instead
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_path_get_dirname:
Packit ae235b
 * @file_name: (type filename): the name of the file
Packit ae235b
 *
Packit ae235b
 * Gets the directory components of a file name.
Packit ae235b
 *
Packit ae235b
 * If the file name has no directory components "." is returned.
Packit ae235b
 * The returned string should be freed when no longer needed.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): the directory components of the file
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_path_get_dirname (const gchar *file_name)
Packit ae235b
{
Packit ae235b
  gchar *base;
Packit ae235b
  gsize len;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (file_name != NULL, NULL);
Packit ae235b
Packit ae235b
  base = strrchr (file_name, G_DIR_SEPARATOR);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  {
Packit ae235b
    gchar *q;
Packit ae235b
    q = strrchr (file_name, '/');
Packit ae235b
    if (base == NULL || (q != NULL && q > base))
Packit ae235b
      base = q;
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (!base)
Packit ae235b
    {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
Packit ae235b
        {
Packit ae235b
          gchar drive_colon_dot[4];
Packit ae235b
Packit ae235b
          drive_colon_dot[0] = file_name[0];
Packit ae235b
          drive_colon_dot[1] = ':';
Packit ae235b
          drive_colon_dot[2] = '.';
Packit ae235b
          drive_colon_dot[3] = '\0';
Packit ae235b
Packit ae235b
          return g_strdup (drive_colon_dot);
Packit ae235b
        }
Packit ae235b
#endif
Packit ae235b
    return g_strdup (".");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (base > file_name && G_IS_DIR_SEPARATOR (*base))
Packit ae235b
    base--;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  /* base points to the char before the last slash.
Packit ae235b
   *
Packit ae235b
   * In case file_name is the root of a drive (X:\) or a child of the
Packit ae235b
   * root of a drive (X:\foo), include the slash.
Packit ae235b
   *
Packit ae235b
   * In case file_name is the root share of an UNC path
Packit ae235b
   * (\\server\share), add a slash, returning \\server\share\ .
Packit ae235b
   *
Packit ae235b
   * In case file_name is a direct child of a share in an UNC path
Packit ae235b
   * (\\server\share\foo), include the slash after the share name,
Packit ae235b
   * returning \\server\share\ .
Packit ae235b
   */
Packit ae235b
  if (base == file_name + 1 &&
Packit ae235b
      g_ascii_isalpha (file_name[0]) &&
Packit ae235b
      file_name[1] == ':')
Packit ae235b
    base++;
Packit ae235b
  else if (G_IS_DIR_SEPARATOR (file_name[0]) &&
Packit ae235b
           G_IS_DIR_SEPARATOR (file_name[1]) &&
Packit ae235b
           file_name[2] &&
Packit ae235b
           !G_IS_DIR_SEPARATOR (file_name[2]) &&
Packit ae235b
           base >= file_name + 2)
Packit ae235b
    {
Packit ae235b
      const gchar *p = file_name + 2;
Packit ae235b
      while (*p && !G_IS_DIR_SEPARATOR (*p))
Packit ae235b
        p++;
Packit ae235b
      if (p == base + 1)
Packit ae235b
        {
Packit ae235b
          len = (guint) strlen (file_name) + 1;
Packit ae235b
          base = g_new (gchar, len + 1);
Packit ae235b
          strcpy (base, file_name);
Packit ae235b
          base[len-1] = G_DIR_SEPARATOR;
Packit ae235b
          base[len] = 0;
Packit ae235b
          return base;
Packit ae235b
        }
Packit ae235b
      if (G_IS_DIR_SEPARATOR (*p))
Packit ae235b
        {
Packit ae235b
          p++;
Packit ae235b
          while (*p && !G_IS_DIR_SEPARATOR (*p))
Packit ae235b
            p++;
Packit ae235b
          if (p == base + 1)
Packit ae235b
            base++;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  len = (guint) 1 + base - file_name;
Packit ae235b
  base = g_new (gchar, len + 1);
Packit ae235b
  memmove (base, file_name, len);
Packit ae235b
  base[len] = 0;
Packit ae235b
Packit ae235b
  return base;
Packit ae235b
}
Packit ae235b
Packit ae235b
#if defined(MAXPATHLEN)
Packit ae235b
#define G_PATH_LENGTH MAXPATHLEN
Packit ae235b
#elif defined(PATH_MAX)
Packit ae235b
#define G_PATH_LENGTH PATH_MAX
Packit ae235b
#elif defined(_PC_PATH_MAX)
Packit ae235b
#define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
Packit ae235b
#else
Packit ae235b
#define G_PATH_LENGTH 2048
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_current_dir:
Packit ae235b
 *
Packit ae235b
 * Gets the current directory.
Packit ae235b
 *
Packit ae235b
 * The returned string should be freed when no longer needed.
Packit ae235b
 * The encoding of the returned string is system defined.
Packit ae235b
 * On Windows, it is always UTF-8.
Packit ae235b
 *
Packit ae235b
 * Since GLib 2.40, this function will return the value of the "PWD"
Packit ae235b
 * environment variable if it is set and it happens to be the same as
Packit ae235b
 * the current directory.  This can make a difference in the case that
Packit ae235b
 * the current directory is the target of a symbolic link.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): the current directory
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_get_current_dir (void)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
  gchar *dir = NULL;
Packit ae235b
  wchar_t dummy[2], *wdir;
Packit ae235b
  int len;
Packit ae235b
Packit ae235b
  len = GetCurrentDirectoryW (2, dummy);
Packit ae235b
  wdir = g_new (wchar_t, len);
Packit ae235b
Packit ae235b
  if (GetCurrentDirectoryW (len, wdir) == len - 1)
Packit ae235b
    dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  g_free (wdir);
Packit ae235b
Packit ae235b
  if (dir == NULL)
Packit ae235b
    dir = g_strdup ("\\");
Packit ae235b
Packit ae235b
  return dir;
Packit ae235b
Packit ae235b
#else
Packit ae235b
  const gchar *pwd;
Packit ae235b
  gchar *buffer = NULL;
Packit ae235b
  gchar *dir = NULL;
Packit ae235b
  static gulong max_len = 0;
Packit ae235b
  struct stat pwdbuf, dotbuf;
Packit ae235b
Packit ae235b
  pwd = g_getenv ("PWD");
Packit ae235b
  if (pwd != NULL &&
Packit ae235b
      g_stat (".", &dotbuf) == 0 && g_stat (pwd, &pwdbuf) == 0 &&
Packit ae235b
      dotbuf.st_dev == pwdbuf.st_dev && dotbuf.st_ino == pwdbuf.st_ino)
Packit ae235b
    return g_strdup (pwd);
Packit ae235b
Packit ae235b
  if (max_len == 0)
Packit ae235b
    max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
Packit ae235b
Packit ae235b
  while (max_len < G_MAXULONG / 2)
Packit ae235b
    {
Packit ae235b
      g_free (buffer);
Packit ae235b
      buffer = g_new (gchar, max_len + 1);
Packit ae235b
      *buffer = 0;
Packit ae235b
      dir = getcwd (buffer, max_len);
Packit ae235b
Packit ae235b
      if (dir || errno != ERANGE)
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      max_len *= 2;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!dir || !*buffer)
Packit ae235b
    {
Packit ae235b
      /* hm, should we g_error() out here?
Packit ae235b
       * this can happen if e.g. "./" has mode \0000
Packit ae235b
       */
Packit ae235b
      buffer[0] = G_DIR_SEPARATOR;
Packit ae235b
      buffer[1] = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  dir = g_strdup (buffer);
Packit ae235b
  g_free (buffer);
Packit ae235b
Packit ae235b
  return dir;
Packit ae235b
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* Binary compatibility versions. Not for newly compiled code. */
Packit ae235b
Packit ae235b
_GLIB_EXTERN gboolean g_file_test_utf8         (const gchar  *filename,
Packit ae235b
                                                GFileTest     test);
Packit ae235b
_GLIB_EXTERN gboolean g_file_get_contents_utf8 (const gchar  *filename,
Packit ae235b
                                                gchar       **contents,
Packit ae235b
                                                gsize        *length,
Packit ae235b
                                                GError      **error);
Packit ae235b
_GLIB_EXTERN gint     g_mkstemp_utf8           (gchar        *tmpl);
Packit ae235b
_GLIB_EXTERN gint     g_file_open_tmp_utf8     (const gchar  *tmpl,
Packit ae235b
                                                gchar       **name_used,
Packit ae235b
                                                GError      **error);
Packit ae235b
_GLIB_EXTERN gchar   *g_get_current_dir_utf8   (void);
Packit ae235b
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_file_test_utf8 (const gchar *filename,
Packit ae235b
                  GFileTest    test)
Packit ae235b
{
Packit ae235b
  return g_file_test (filename, test);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_file_get_contents_utf8 (const gchar  *filename,
Packit ae235b
                          gchar       **contents,
Packit ae235b
                          gsize        *length,
Packit ae235b
                          GError      **error)
Packit ae235b
{
Packit ae235b
  return g_file_get_contents (filename, contents, length, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_mkstemp_utf8 (gchar *tmpl)
Packit ae235b
{
Packit ae235b
  return g_mkstemp (tmpl);
Packit ae235b
}
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_file_open_tmp_utf8 (const gchar  *tmpl,
Packit ae235b
                      gchar       **name_used,
Packit ae235b
                      GError      **error)
Packit ae235b
{
Packit ae235b
  return g_file_open_tmp (tmpl, name_used, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_get_current_dir_utf8 (void)
Packit ae235b
{
Packit ae235b
  return g_get_current_dir ();
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif