Blame gio/glocalfile.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 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
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#if G_OS_UNIX
Packit ae235b
#include <dirent.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if HAVE_SYS_STATFS_H
Packit ae235b
#include <sys/statfs.h>
Packit ae235b
#endif
Packit ae235b
#if HAVE_SYS_STATVFS_H
Packit ae235b
#include <sys/statvfs.h>
Packit ae235b
#endif
Packit ae235b
#if HAVE_SYS_VFS_H
Packit ae235b
#include <sys/vfs.h>
Packit ae235b
#elif HAVE_SYS_MOUNT_H
Packit ae235b
#if HAVE_SYS_PARAM_H
Packit ae235b
#include <sys/param.h>
Packit ae235b
#endif
Packit ae235b
#include <sys/mount.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef O_BINARY
Packit ae235b
#define O_BINARY 0
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gfileattribute.h"
Packit ae235b
#include "glocalfile.h"
Packit ae235b
#include "glocalfileprivate.h"
Packit ae235b
#include "glocalfileinfo.h"
Packit ae235b
#include "glocalfileenumerator.h"
Packit ae235b
#include "glocalfileinputstream.h"
Packit ae235b
#include "glocalfileoutputstream.h"
Packit ae235b
#include "glocalfileiostream.h"
Packit ae235b
#include "glocalfilemonitor.h"
Packit ae235b
#include "gmountprivate.h"
Packit ae235b
#include "gunixmounts.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
#include <glib/gstdioprivate.h>
Packit ae235b
#include "glibintl.h"
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include "glib-unix.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glib-private.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#include <io.h>
Packit ae235b
#include <direct.h>
Packit ae235b
Packit ae235b
#ifndef FILE_READ_ONLY_VOLUME
Packit ae235b
#define FILE_READ_ONLY_VOLUME           0x00080000
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef S_ISDIR
Packit ae235b
#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
Packit ae235b
#endif
Packit ae235b
#ifndef S_ISLNK
Packit ae235b
#define S_ISLNK(m) (0)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef ECANCELED
Packit ae235b
#define ECANCELED 105
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
static void g_local_file_file_iface_init (GFileIface *iface);
Packit ae235b
Packit ae235b
static GFileAttributeInfoList *local_writable_attributes = NULL;
Packit ae235b
static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
Packit ae235b
Packit ae235b
struct _GLocalFile
Packit ae235b
{
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  char *filename;
Packit ae235b
};
Packit ae235b
Packit ae235b
#define g_local_file_get_type _g_local_file_get_type
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
Packit ae235b
						g_local_file_file_iface_init))
Packit ae235b
Packit ae235b
static char *find_mountpoint_for (const char *file, dev_t dev);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_local_file_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GLocalFile *local;
Packit ae235b
Packit ae235b
  local = G_LOCAL_FILE (object);
Packit ae235b
Packit ae235b
  g_free (local->filename);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_local_file_class_init (GLocalFileClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
  GFileAttributeInfoList *list;
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_local_file_finalize;
Packit ae235b
Packit ae235b
  /* Set up attribute lists */
Packit ae235b
Packit ae235b
  /* Writable attributes: */
Packit ae235b
Packit ae235b
  list = g_file_attribute_info_list_new ();
Packit ae235b
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_UNIX_MODE,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_UINT32,
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
  
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_UNIX_UID,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_UINT32,
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_UNIX_GID,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_UINT32,
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
#ifdef HAVE_SYMLINK
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
Packit ae235b
				  0);
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
#ifdef HAVE_UTIMES
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_TIME_MODIFIED,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_UINT64,
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_UINT32,
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
  /* When copying, the target file is accessed. Replicating
Packit ae235b
   * the source access time does not make sense in this case.
Packit ae235b
   */
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_TIME_ACCESS,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_UINT64,
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
  g_file_attribute_info_list_add (list,
Packit ae235b
				  G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
Packit ae235b
				  G_FILE_ATTRIBUTE_TYPE_UINT32,
Packit ae235b
				  G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  local_writable_attributes = list;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_local_file_init (GLocalFile *local)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
const char *
Packit ae235b
_g_local_file_get_filename (GLocalFile *file)
Packit ae235b
{
Packit ae235b
  return file->filename;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
canonicalize_filename (const char *filename)
Packit ae235b
{
Packit ae235b
  char *canon, *start, *p, *q;
Packit ae235b
  char *cwd;
Packit ae235b
  int i;
Packit ae235b
  
Packit ae235b
  if (!g_path_is_absolute (filename))
Packit ae235b
    {
Packit ae235b
      cwd = g_get_current_dir ();
Packit ae235b
      canon = g_build_filename (cwd, filename, NULL);
Packit ae235b
      g_free (cwd);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    canon = g_strdup (filename);
Packit ae235b
Packit ae235b
  start = (char *)g_path_skip_root (canon);
Packit ae235b
Packit ae235b
  if (start == NULL)
Packit ae235b
    {
Packit ae235b
      /* This shouldn't really happen, as g_get_current_dir() should
Packit ae235b
	 return an absolute pathname, but bug 573843 shows this is
Packit ae235b
	 not always happening */
Packit ae235b
      g_free (canon);
Packit ae235b
      return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* POSIX allows double slashes at the start to
Packit ae235b
   * mean something special (as does windows too).
Packit ae235b
   * So, "//" != "/", but more than two slashes
Packit ae235b
   * is treated as "/".
Packit ae235b
   */
Packit ae235b
  i = 0;
Packit ae235b
  for (p = start - 1;
Packit ae235b
       (p >= canon) &&
Packit ae235b
	 G_IS_DIR_SEPARATOR (*p);
Packit ae235b
       p--)
Packit ae235b
    i++;
Packit ae235b
  if (i > 2)
Packit ae235b
    {
Packit ae235b
      i -= 1;
Packit ae235b
      start -= i;
Packit ae235b
      memmove (start, start+i, strlen (start+i)+1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Make sure we're using the canonical dir separator */
Packit ae235b
  p++;
Packit ae235b
  while (p < start && G_IS_DIR_SEPARATOR (*p))
Packit ae235b
    *p++ = G_DIR_SEPARATOR;
Packit ae235b
  
Packit ae235b
  p = start;
Packit ae235b
  while (*p != 0)
Packit ae235b
    {
Packit ae235b
      if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
Packit ae235b
	{
Packit ae235b
	  memmove (p, p+1, strlen (p+1)+1);
Packit ae235b
	}
Packit ae235b
      else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
Packit ae235b
	{
Packit ae235b
	  q = p + 2;
Packit ae235b
	  /* Skip previous separator */
Packit ae235b
	  p = p - 2;
Packit ae235b
	  if (p < start)
Packit ae235b
	    p = start;
Packit ae235b
	  while (p > start && !G_IS_DIR_SEPARATOR (*p))
Packit ae235b
	    p--;
Packit ae235b
	  if (G_IS_DIR_SEPARATOR (*p))
Packit ae235b
	    *p++ = G_DIR_SEPARATOR;
Packit ae235b
	  memmove (p, q, strlen (q)+1);
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  /* Skip until next separator */
Packit ae235b
	  while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
Packit ae235b
	    p++;
Packit ae235b
	  
Packit ae235b
	  if (*p != 0)
Packit ae235b
	    {
Packit ae235b
	      /* Canonicalize one separator */
Packit ae235b
	      *p++ = G_DIR_SEPARATOR;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      /* Remove additional separators */
Packit ae235b
      q = p;
Packit ae235b
      while (*q && G_IS_DIR_SEPARATOR (*q))
Packit ae235b
	q++;
Packit ae235b
Packit ae235b
      if (p != q)
Packit ae235b
	memmove (p, q, strlen (q)+1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Remove trailing slashes */
Packit ae235b
  if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
Packit ae235b
    *(p-1) = 0;
Packit ae235b
  
Packit ae235b
  return canon;
Packit ae235b
}
Packit ae235b
Packit ae235b
GFile *
Packit ae235b
_g_local_file_new (const char *filename)
Packit ae235b
{
Packit ae235b
  GLocalFile *local;
Packit ae235b
Packit ae235b
  local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
Packit ae235b
  local->filename = canonicalize_filename (filename);
Packit ae235b
  
Packit ae235b
  return G_FILE (local);
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< internal >
Packit ae235b
 * g_local_file_new_from_dirname_and_basename:
Packit ae235b
 * @dirname: an absolute, canonical directory name
Packit ae235b
 * @basename: the name of a child inside @dirname
Packit ae235b
 *
Packit ae235b
 * Creates a #GFile from @dirname and @basename.
Packit ae235b
 *
Packit ae235b
 * This is more efficient than pasting the fields together for yourself
Packit ae235b
 * and creating a #GFile from the result, and also more efficient than
Packit ae235b
 * creating a #GFile for the dirname and using g_file_get_child().
Packit ae235b
 *
Packit ae235b
 * @dirname must be canonical, as per GLocalFile's opinion of what
Packit ae235b
 * canonical means.  This means that you should only pass strings that
Packit ae235b
 * were returned by _g_local_file_get_filename().
Packit ae235b
 *
Packit ae235b
 * Returns: a #GFile
Packit ae235b
 */
Packit ae235b
GFile *
Packit ae235b
g_local_file_new_from_dirname_and_basename (const gchar *dirname,
Packit ae235b
                                            const gchar *basename)
Packit ae235b
{
Packit ae235b
  GLocalFile *local;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (dirname != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (basename && basename[0] && !strchr (basename, '/'), NULL);
Packit ae235b
Packit ae235b
  local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
Packit ae235b
  local->filename = g_build_filename (dirname, basename, NULL);
Packit ae235b
Packit ae235b
  return G_FILE (local);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_is_native (GFile *file)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_has_uri_scheme (GFile      *file,
Packit ae235b
			     const char *uri_scheme)
Packit ae235b
{
Packit ae235b
  return g_ascii_strcasecmp (uri_scheme, "file") == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_local_file_get_uri_scheme (GFile *file)
Packit ae235b
{
Packit ae235b
  return g_strdup ("file");
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_local_file_get_basename (GFile *file)
Packit ae235b
{
Packit ae235b
  return g_path_get_basename (G_LOCAL_FILE (file)->filename);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_local_file_get_path (GFile *file)
Packit ae235b
{
Packit ae235b
  return g_strdup (G_LOCAL_FILE (file)->filename);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_local_file_get_uri (GFile *file)
Packit ae235b
{
Packit ae235b
  return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_filename_charset (const gchar **filename_charset)
Packit ae235b
{
Packit ae235b
  const gchar **charsets;
Packit ae235b
  gboolean is_utf8;
Packit ae235b
  
Packit ae235b
  is_utf8 = g_get_filename_charsets (&charsets);
Packit ae235b
Packit ae235b
  if (filename_charset)
Packit ae235b
    *filename_charset = charsets[0];
Packit ae235b
  
Packit ae235b
  return is_utf8;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
name_is_valid_for_display (const char *string,
Packit ae235b
			   gboolean    is_valid_utf8)
Packit ae235b
{
Packit ae235b
  char c;
Packit ae235b
Packit ae235b
  if (!is_valid_utf8 &&
Packit ae235b
      !g_utf8_validate (string, -1, NULL))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  while ((c = *string++) != 0)
Packit ae235b
    {
Packit ae235b
      if (g_ascii_iscntrl (c))
Packit ae235b
	return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_local_file_get_parse_name (GFile *file)
Packit ae235b
{
Packit ae235b
  const char *filename;
Packit ae235b
  char *parse_name;
Packit ae235b
  const gchar *charset;
Packit ae235b
  char *utf8_filename;
Packit ae235b
  char *roundtripped_filename;
Packit ae235b
  gboolean free_utf8_filename;
Packit ae235b
  gboolean is_valid_utf8;
Packit ae235b
  char *escaped_path;
Packit ae235b
  
Packit ae235b
  filename = G_LOCAL_FILE (file)->filename;
Packit ae235b
  if (get_filename_charset (&charset))
Packit ae235b
    {
Packit ae235b
      utf8_filename = (char *)filename;
Packit ae235b
      free_utf8_filename = FALSE;
Packit ae235b
      is_valid_utf8 = FALSE; /* Can't guarantee this */
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      utf8_filename = g_convert (filename, -1, 
Packit ae235b
				 "UTF-8", charset, NULL, NULL, NULL);
Packit ae235b
      free_utf8_filename = TRUE;
Packit ae235b
      is_valid_utf8 = TRUE;
Packit ae235b
Packit ae235b
      if (utf8_filename != NULL)
Packit ae235b
	{
Packit ae235b
	  /* Make sure we can roundtrip: */
Packit ae235b
	  roundtripped_filename = g_convert (utf8_filename, -1,
Packit ae235b
					     charset, "UTF-8", NULL, NULL, NULL);
Packit ae235b
	  
Packit ae235b
	  if (roundtripped_filename == NULL ||
Packit ae235b
	      strcmp (filename, roundtripped_filename) != 0)
Packit ae235b
	    {
Packit ae235b
	      g_free (utf8_filename);
Packit ae235b
	      utf8_filename = NULL;
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  g_free (roundtripped_filename);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (utf8_filename != NULL &&
Packit ae235b
      name_is_valid_for_display (utf8_filename, is_valid_utf8))
Packit ae235b
    {
Packit ae235b
      if (free_utf8_filename)
Packit ae235b
	parse_name = utf8_filename;
Packit ae235b
      else
Packit ae235b
	parse_name = g_strdup (utf8_filename);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      char *dup_filename, *p, *backslash;
Packit ae235b
Packit ae235b
      /* Turn backslashes into forward slashes like
Packit ae235b
       * g_filename_to_uri() would do (but we can't use that because
Packit ae235b
       * it doesn't output IRIs).
Packit ae235b
       */
Packit ae235b
      dup_filename = g_strdup (filename);
Packit ae235b
      filename = p = dup_filename;
Packit ae235b
Packit ae235b
      while ((backslash = strchr (p, '\\')) != NULL)
Packit ae235b
	{
Packit ae235b
	  *backslash = '/';
Packit ae235b
	  p = backslash + 1;
Packit ae235b
	}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      escaped_path = g_uri_escape_string (filename,
Packit ae235b
					  G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
Packit ae235b
					  TRUE);
Packit ae235b
      parse_name = g_strconcat ("file://",
Packit ae235b
				(*escaped_path != '/') ? "/" : "",
Packit ae235b
				escaped_path,
Packit ae235b
				NULL);
Packit ae235b
      
Packit ae235b
      g_free (escaped_path);
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      g_free (dup_filename);
Packit ae235b
#endif
Packit ae235b
      if (free_utf8_filename)
Packit ae235b
	g_free (utf8_filename);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return parse_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
g_local_file_get_parent (GFile *file)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  const char *non_root;
Packit ae235b
  char *dirname;
Packit ae235b
  GFile *parent;
Packit ae235b
Packit ae235b
  /* Check for root; local->filename is guaranteed to be absolute, so
Packit ae235b
   * g_path_skip_root() should never return NULL. */
Packit ae235b
  non_root = g_path_skip_root (local->filename);
Packit ae235b
  g_assert (non_root != NULL);
Packit ae235b
Packit ae235b
  if (*non_root == 0)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  dirname = g_path_get_dirname (local->filename);
Packit ae235b
  parent = _g_local_file_new (dirname);
Packit ae235b
  g_free (dirname);
Packit ae235b
  return parent;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
g_local_file_dup (GFile *file)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
Packit ae235b
  return _g_local_file_new (local->filename);
Packit ae235b
}
Packit ae235b
Packit ae235b
static guint
Packit ae235b
g_local_file_hash (GFile *file)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  
Packit ae235b
  return g_str_hash (local->filename);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_equal (GFile *file1,
Packit ae235b
		    GFile *file2)
Packit ae235b
{
Packit ae235b
  GLocalFile *local1 = G_LOCAL_FILE (file1);
Packit ae235b
  GLocalFile *local2 = G_LOCAL_FILE (file2);
Packit ae235b
Packit ae235b
  return g_str_equal (local1->filename, local2->filename);
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
match_prefix (const char *path, 
Packit ae235b
              const char *prefix)
Packit ae235b
{
Packit ae235b
  int prefix_len;
Packit ae235b
Packit ae235b
  prefix_len = strlen (prefix);
Packit ae235b
  if (strncmp (path, prefix, prefix_len) != 0)
Packit ae235b
    return NULL;
Packit ae235b
  
Packit ae235b
  /* Handle the case where prefix is the root, so that
Packit ae235b
   * the IS_DIR_SEPRARATOR check below works */
Packit ae235b
  if (prefix_len > 0 &&
Packit ae235b
      G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
Packit ae235b
    prefix_len--;
Packit ae235b
  
Packit ae235b
  return path + prefix_len;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_prefix_matches (GFile *parent,
Packit ae235b
			     GFile *descendant)
Packit ae235b
{
Packit ae235b
  GLocalFile *parent_local = G_LOCAL_FILE (parent);
Packit ae235b
  GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
Packit ae235b
  const char *remainder;
Packit ae235b
Packit ae235b
  remainder = match_prefix (descendant_local->filename, parent_local->filename);
Packit ae235b
  if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
Packit ae235b
    return TRUE;
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_local_file_get_relative_path (GFile *parent,
Packit ae235b
				GFile *descendant)
Packit ae235b
{
Packit ae235b
  GLocalFile *parent_local = G_LOCAL_FILE (parent);
Packit ae235b
  GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
Packit ae235b
  const char *remainder;
Packit ae235b
Packit ae235b
  remainder = match_prefix (descendant_local->filename, parent_local->filename);
Packit ae235b
  
Packit ae235b
  if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
Packit ae235b
    return g_strdup (remainder + 1);
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
g_local_file_resolve_relative_path (GFile      *file,
Packit ae235b
				    const char *relative_path)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  char *filename;
Packit ae235b
  GFile *child;
Packit ae235b
Packit ae235b
  if (g_path_is_absolute (relative_path))
Packit ae235b
    return _g_local_file_new (relative_path);
Packit ae235b
  
Packit ae235b
  filename = g_build_filename (local->filename, relative_path, NULL);
Packit ae235b
  child = _g_local_file_new (filename);
Packit ae235b
  g_free (filename);
Packit ae235b
  
Packit ae235b
  return child;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileEnumerator *
Packit ae235b
g_local_file_enumerate_children (GFile                *file,
Packit ae235b
				 const char           *attributes,
Packit ae235b
				 GFileQueryInfoFlags   flags,
Packit ae235b
				 GCancellable         *cancellable,
Packit ae235b
				 GError              **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  return _g_local_file_enumerator_new (local,
Packit ae235b
				       attributes, flags,
Packit ae235b
				       cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
g_local_file_get_child_for_display_name (GFile        *file,
Packit ae235b
					 const char   *display_name,
Packit ae235b
					 GError      **error)
Packit ae235b
{
Packit ae235b
  GFile *new_file;
Packit ae235b
  char *basename;
Packit ae235b
Packit ae235b
  basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
Packit ae235b
  if (basename == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
		   G_IO_ERROR_INVALID_FILENAME,
Packit ae235b
		   _("Invalid filename %s"), display_name);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  new_file = g_file_get_child (file, basename);
Packit ae235b
  g_free (basename);
Packit ae235b
  
Packit ae235b
  return new_file;
Packit ae235b
}
Packit ae235b
Packit ae235b
#if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
Packit ae235b
static const char *
Packit ae235b
get_fs_type (long f_type)
Packit ae235b
{
Packit ae235b
  /* filesystem ids taken from linux manpage */
Packit ae235b
  switch (f_type) 
Packit ae235b
    {
Packit ae235b
    case 0xadf5:
Packit ae235b
      return "adfs";
Packit ae235b
    case 0x5346414f:
Packit ae235b
      return "afs";
Packit ae235b
    case 0x0187:
Packit ae235b
      return "autofs";
Packit ae235b
    case 0xADFF:
Packit ae235b
      return "affs";
Packit ae235b
    case 0x62646576:
Packit ae235b
      return "bdevfs";
Packit ae235b
    case 0x42465331:
Packit ae235b
      return "befs";
Packit ae235b
    case 0x1BADFACE:
Packit ae235b
      return "bfs";
Packit ae235b
    case 0x42494e4d:
Packit ae235b
      return "binfmt_misc";
Packit ae235b
    case 0x9123683E:
Packit ae235b
      return "btrfs";
Packit ae235b
    case 0x73727279:
Packit ae235b
      return "btrfs_test_fs";
Packit ae235b
    case 0x27e0eb:
Packit ae235b
      return "cgroup";
Packit ae235b
    case 0x63677270:
Packit ae235b
      return "cgroup2";
Packit ae235b
    case 0xFF534D42:
Packit ae235b
      return "cifs";
Packit ae235b
    case 0x73757245:
Packit ae235b
      return "coda";
Packit ae235b
    case 0x012FF7B7:
Packit ae235b
      return "coh";
Packit ae235b
    case 0x62656570:
Packit ae235b
      return "configfs";
Packit ae235b
    case 0x28cd3d45:
Packit ae235b
      return "cramfs";
Packit ae235b
    case 0x64626720:
Packit ae235b
      return "debugfs";
Packit ae235b
    case 0x1373:
Packit ae235b
      return "devfs";
Packit ae235b
    case 0x1cd1:
Packit ae235b
      return "devpts";
Packit ae235b
    case 0xf15f:
Packit ae235b
      return "ecryptfs";
Packit ae235b
    case 0xde5e81e4:
Packit ae235b
      return "efivarfs";
Packit ae235b
    case 0x00414A53:
Packit ae235b
      return "efs";
Packit ae235b
    case 0x137D:
Packit ae235b
      return "ext";
Packit ae235b
    case 0xEF51:
Packit ae235b
      return "ext2";
Packit ae235b
    case 0xEF53:
Packit ae235b
      return "ext3/ext4";
Packit ae235b
    case 0xF2F52010:
Packit ae235b
      return "f2fs";
Packit ae235b
    case 0x65735546:
Packit ae235b
      return "fuse";
Packit ae235b
    case 0x65735543:
Packit ae235b
      return "fusectl";
Packit ae235b
    case 0xBAD1DEA:
Packit ae235b
      return "futexfs";
Packit ae235b
    case 0x4244:
Packit ae235b
      return "hfs";
Packit ae235b
    case 0x00c0ffee:
Packit ae235b
      return "hostfs";
Packit ae235b
    case 0xF995E849:
Packit ae235b
      return "hpfs";
Packit ae235b
    case 0x958458f6:
Packit ae235b
      return "hugetlbfs";
Packit ae235b
    case 0x9660:
Packit ae235b
      return "isofs";
Packit ae235b
    case 0x72b6:
Packit ae235b
      return "jffs2";
Packit ae235b
    case 0x3153464a:
Packit ae235b
      return "jfs";
Packit ae235b
    case 0x137F:
Packit ae235b
      return "minix";
Packit ae235b
    case 0x138F:
Packit ae235b
      return "minix2";
Packit ae235b
    case 0x2468:
Packit ae235b
      return "minix2";
Packit ae235b
    case 0x2478:
Packit ae235b
      return "minix22";
Packit ae235b
    case 0x4d5a:
Packit ae235b
      return "minix3";
Packit ae235b
    case 0x19800202:
Packit ae235b
      return "mqueue";
Packit ae235b
    case 0x4d44:
Packit ae235b
      return "msdos";
Packit ae235b
    case 0x564c:
Packit ae235b
      return "ncp";
Packit ae235b
    case 0x6969:
Packit ae235b
      return "nfs";
Packit ae235b
    case 0x3434:
Packit ae235b
      return "nilfs";
Packit ae235b
    case 0x6e736673:
Packit ae235b
      return "nsfs";
Packit ae235b
    case 0x5346544e:
Packit ae235b
      return "ntfs";
Packit ae235b
    case 0x7461636f:
Packit ae235b
      return "ocfs2";
Packit ae235b
    case 0x9fa1:
Packit ae235b
      return "openprom";
Packit ae235b
    case 0x794c7630:
Packit ae235b
      return "overlay";
Packit ae235b
    case 0x50495045:
Packit ae235b
      return "pipefs";
Packit ae235b
    case 0x9fa0:
Packit ae235b
      return "proc";
Packit ae235b
    case 0x6165676C:
Packit ae235b
      return "pstore";
Packit ae235b
    case 0x002f:
Packit ae235b
      return "qnx4";
Packit ae235b
    case 0x68191122:
Packit ae235b
      return "qnx6";
Packit ae235b
    case 0x858458f6:
Packit ae235b
      return "ramfs";
Packit ae235b
    case 0x52654973:
Packit ae235b
      return "reiserfs";
Packit ae235b
    case 0x7275:
Packit ae235b
      return "romfs";
Packit ae235b
    case 0x67596969:
Packit ae235b
      return "rpc_pipefs";
Packit ae235b
    case 0x73636673:
Packit ae235b
      return "securityfs";
Packit ae235b
    case 0xf97cff8c:
Packit ae235b
      return "selinuxfs";
Packit ae235b
    case 0x43415d53:
Packit ae235b
      return "smackfs";
Packit ae235b
    case 0x517B:
Packit ae235b
      return "smb";
Packit ae235b
    case 0x534F434B:
Packit ae235b
      return "sockfs";
Packit ae235b
    case 0x73717368:
Packit ae235b
      return "squashfs";
Packit ae235b
    case 0x62656572:
Packit ae235b
      return "sysfs";
Packit ae235b
    case 0x012FF7B6:
Packit ae235b
      return "sysv2";
Packit ae235b
    case 0x012FF7B5:
Packit ae235b
      return "sysv4";
Packit ae235b
    case 0x01021994:
Packit ae235b
      return "tmpfs";
Packit ae235b
    case 0x74726163:
Packit ae235b
      return "tracefs";
Packit ae235b
    case 0x15013346:
Packit ae235b
      return "udf";
Packit ae235b
    case 0x00011954:
Packit ae235b
      return "ufs";
Packit ae235b
    case 0x9fa2:
Packit ae235b
      return "usbdevice";
Packit ae235b
    case 0x01021997:
Packit ae235b
      return "v9fs";
Packit ae235b
    case 0xa501FCF5:
Packit ae235b
      return "vxfs";
Packit ae235b
    case 0xabba1974:
Packit ae235b
      return "xenfs";
Packit ae235b
    case 0x012FF7B4:
Packit ae235b
      return "xenix";
Packit ae235b
    case 0x58465342:
Packit ae235b
      return "xfs";
Packit ae235b
    case 0x012FD16D:
Packit ae235b
      return "xiafs";
Packit ae235b
    case 0x52345362:
Packit ae235b
      return "reiser4";
Packit ae235b
    default:
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC(mount_info_hash);
Packit ae235b
static GHashTable *mount_info_hash = NULL;
Packit ae235b
static guint64 mount_info_hash_cache_time = 0;
Packit ae235b
Packit ae235b
typedef enum {
Packit ae235b
  MOUNT_INFO_READONLY = 1<<0
Packit ae235b
} MountInfo;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
device_equal (gconstpointer v1,
Packit ae235b
              gconstpointer v2)
Packit ae235b
{
Packit ae235b
  return *(dev_t *)v1 == *(dev_t *)v2;
Packit ae235b
}
Packit ae235b
Packit ae235b
static guint
Packit ae235b
device_hash (gconstpointer v)
Packit ae235b
{
Packit ae235b
  return (guint) *(dev_t *)v;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_mount_info (GFileInfo             *fs_info,
Packit ae235b
		const char            *path,
Packit ae235b
		GFileAttributeMatcher *matcher)
Packit ae235b
{
Packit ae235b
  GStatBuf buf;
Packit ae235b
  gboolean got_info;
Packit ae235b
  gpointer info_as_ptr;
Packit ae235b
  guint mount_info;
Packit ae235b
  char *mountpoint;
Packit ae235b
  dev_t *dev;
Packit ae235b
  GUnixMountEntry *mount;
Packit ae235b
  guint64 cache_time;
Packit ae235b
Packit ae235b
  if (g_lstat (path, &buf) != 0)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  G_LOCK (mount_info_hash);
Packit ae235b
Packit ae235b
  if (mount_info_hash == NULL)
Packit ae235b
    mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
Packit ae235b
					     g_free, NULL);
Packit ae235b
Packit ae235b
Packit ae235b
  if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
Packit ae235b
    g_hash_table_remove_all (mount_info_hash);
Packit ae235b
  
Packit ae235b
  got_info = g_hash_table_lookup_extended (mount_info_hash,
Packit ae235b
					   &buf.st_dev,
Packit ae235b
					   NULL,
Packit ae235b
					   &info_as_ptr);
Packit ae235b
  
Packit ae235b
  G_UNLOCK (mount_info_hash);
Packit ae235b
  
Packit ae235b
  mount_info = GPOINTER_TO_UINT (info_as_ptr);
Packit ae235b
  
Packit ae235b
  if (!got_info)
Packit ae235b
    {
Packit ae235b
      mount_info = 0;
Packit ae235b
Packit ae235b
      mountpoint = find_mountpoint_for (path, buf.st_dev);
Packit ae235b
      if (mountpoint == NULL)
Packit ae235b
	mountpoint = g_strdup ("/");
Packit ae235b
Packit ae235b
      mount = g_unix_mount_at (mountpoint, &cache_time);
Packit ae235b
      if (mount)
Packit ae235b
	{
Packit ae235b
	  if (g_unix_mount_is_readonly (mount))
Packit ae235b
	    mount_info |= MOUNT_INFO_READONLY;
Packit ae235b
	  
Packit ae235b
	  g_unix_mount_free (mount);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_free (mountpoint);
Packit ae235b
Packit ae235b
      dev = g_new0 (dev_t, 1);
Packit ae235b
      *dev = buf.st_dev;
Packit ae235b
      
Packit ae235b
      G_LOCK (mount_info_hash);
Packit ae235b
      mount_info_hash_cache_time = cache_time;
Packit ae235b
      g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
Packit ae235b
      G_UNLOCK (mount_info_hash);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (mount_info & MOUNT_INFO_READONLY)
Packit ae235b
    g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_xp_or_later (void)
Packit ae235b
{
Packit ae235b
  static int result = -1;
Packit ae235b
Packit ae235b
  if (result == -1)
Packit ae235b
    {
Packit ae235b
#ifndef _MSC_VER    
Packit ae235b
      OSVERSIONINFOEX ver_info = {0};
Packit ae235b
      DWORDLONG cond_mask = 0;
Packit ae235b
      int op = VER_GREATER_EQUAL;
Packit ae235b
Packit ae235b
      ver_info.dwOSVersionInfoSize = sizeof ver_info;
Packit ae235b
      ver_info.dwMajorVersion = 5;
Packit ae235b
      ver_info.dwMinorVersion = 1;
Packit ae235b
Packit ae235b
      VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
Packit ae235b
      VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
Packit ae235b
Packit ae235b
      result = VerifyVersionInfo (&ver_info,
Packit ae235b
				  VER_MAJORVERSION | VER_MINORVERSION, 
Packit ae235b
				  cond_mask) != 0;
Packit ae235b
#else
Packit ae235b
      result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;  
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static wchar_t *
Packit ae235b
get_volume_for_path (const char *path)
Packit ae235b
{
Packit ae235b
  long len;
Packit ae235b
  wchar_t *wpath;
Packit ae235b
  wchar_t *result;
Packit ae235b
Packit ae235b
  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
Packit ae235b
  result = g_new (wchar_t, MAX_PATH);
Packit ae235b
Packit ae235b
  if (!GetVolumePathNameW (wpath, result, MAX_PATH))
Packit ae235b
    {
Packit ae235b
      char *msg = g_win32_error_message (GetLastError ());
Packit ae235b
      g_critical ("GetVolumePathName failed: %s", msg);
Packit ae235b
      g_free (msg);
Packit ae235b
      g_free (result);
Packit ae235b
      g_free (wpath);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  len = wcslen (result);
Packit ae235b
  if (len > 0 && result[len-1] != L'\\')
Packit ae235b
    {
Packit ae235b
      result = g_renew (wchar_t, result, len + 2);
Packit ae235b
      result[len] = L'\\';
Packit ae235b
      result[len + 1] = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (wpath);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
find_mountpoint_for (const char *file, dev_t dev)
Packit ae235b
{
Packit ae235b
  wchar_t *wpath;
Packit ae235b
  char *utf8_path;
Packit ae235b
Packit ae235b
  wpath = get_volume_for_path (file);
Packit ae235b
  if (!wpath)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  g_free (wpath);
Packit ae235b
  return utf8_path;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_filesystem_readonly (GFileInfo  *info,
Packit ae235b
			 const char *path)
Packit ae235b
{
Packit ae235b
  wchar_t *rootdir;
Packit ae235b
Packit ae235b
  rootdir = get_volume_for_path (path);
Packit ae235b
Packit ae235b
  if (rootdir)
Packit ae235b
    {
Packit ae235b
      if (is_xp_or_later ())
Packit ae235b
        {
Packit ae235b
          DWORD flags;
Packit ae235b
          if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
Packit ae235b
	    g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
Packit ae235b
					       (flags & FILE_READ_ONLY_VOLUME) != 0);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
Packit ae235b
	    g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (rootdir);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#pragma GCC diagnostic push
Packit ae235b
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
Packit ae235b
static void
Packit ae235b
g_set_io_error (GError      **error,
Packit ae235b
                const gchar  *msg,
Packit ae235b
                GFile        *file,
Packit ae235b
                gint          errsv)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  gchar *display_name;
Packit ae235b
Packit ae235b
  display_name = g_filename_display_name (local->filename);
Packit ae235b
  g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
Packit ae235b
               msg, display_name, g_strerror (errsv));
Packit ae235b
  g_free (display_name);
Packit ae235b
}
Packit ae235b
#pragma GCC diagnostic pop
Packit ae235b
Packit ae235b
static GFileInfo *
Packit ae235b
g_local_file_query_filesystem_info (GFile         *file,
Packit ae235b
				    const char    *attributes,
Packit ae235b
				    GCancellable  *cancellable,
Packit ae235b
				    GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  GFileInfo *info;
Packit ae235b
  int statfs_result = 0;
Packit ae235b
  gboolean no_size;
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  const char *fstype;
Packit ae235b
#ifdef USE_STATFS
Packit ae235b
  guint64 block_size;
Packit ae235b
  struct statfs statfs_buffer;
Packit ae235b
#elif defined(USE_STATVFS)
Packit ae235b
  guint64 block_size;
Packit ae235b
  struct statvfs statfs_buffer;
Packit ae235b
#endif /* USE_STATFS */
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
  GFileAttributeMatcher *attribute_matcher;
Packit ae235b
	
Packit ae235b
  no_size = FALSE;
Packit ae235b
  
Packit ae235b
#ifdef USE_STATFS
Packit ae235b
  
Packit ae235b
#if STATFS_ARGS == 2
Packit ae235b
  statfs_result = statfs (local->filename, &statfs_buffer);
Packit ae235b
#elif STATFS_ARGS == 4
Packit ae235b
  statfs_result = statfs (local->filename, &statfs_buffer,
Packit ae235b
			  sizeof (statfs_buffer), 0);
Packit ae235b
#endif /* STATFS_ARGS == 2 */
Packit ae235b
  block_size = statfs_buffer.f_bsize;
Packit ae235b
  
Packit ae235b
  /* Many backends can't report free size (for instance the gvfs fuse
Packit ae235b
     backend for backend not supporting this), and set f_bfree to 0,
Packit ae235b
     but it can be 0 for real too. We treat the available == 0 and
Packit ae235b
     free == 0 case as "both of these are invalid".
Packit ae235b
   */
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  if (statfs_result == 0 &&
Packit ae235b
      statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
Packit ae235b
    no_size = TRUE;
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
  
Packit ae235b
#elif defined(USE_STATVFS)
Packit ae235b
  statfs_result = statvfs (local->filename, &statfs_buffer);
Packit ae235b
  block_size = statfs_buffer.f_frsize; 
Packit ae235b
#endif /* USE_STATFS */
Packit ae235b
Packit ae235b
  if (statfs_result == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_set_io_error (error,
Packit ae235b
                      _("Error getting filesystem info for %s: %s"),
Packit ae235b
                      file, errsv);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  info = g_file_info_new ();
Packit ae235b
Packit ae235b
  attribute_matcher = g_file_attribute_matcher_new (attributes);
Packit ae235b
  
Packit ae235b
  if (!no_size &&
Packit ae235b
      g_file_attribute_matcher_matches (attribute_matcher,
Packit ae235b
					G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
Packit ae235b
    {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      gchar *localdir = g_path_get_dirname (local->filename);
Packit ae235b
      wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
Packit ae235b
      ULARGE_INTEGER li;
Packit ae235b
      
Packit ae235b
      g_free (localdir);
Packit ae235b
      if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
Packit ae235b
        g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
Packit ae235b
      g_free (wdirname);
Packit ae235b
#else
Packit ae235b
#if defined(USE_STATFS) || defined(USE_STATVFS)
Packit ae235b
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
  if (!no_size &&
Packit ae235b
      g_file_attribute_matcher_matches (attribute_matcher,
Packit ae235b
					G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
Packit ae235b
    {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      gchar *localdir = g_path_get_dirname (local->filename);
Packit ae235b
      wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
Packit ae235b
      ULARGE_INTEGER li;
Packit ae235b
      
Packit ae235b
      g_free (localdir);
Packit ae235b
      if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
Packit ae235b
        g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE,  (guint64)li.QuadPart);
Packit ae235b
      g_free (wdirname);
Packit ae235b
#else
Packit ae235b
#if defined(USE_STATFS) || defined(USE_STATVFS)
Packit ae235b
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
Packit ae235b
#endif
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!no_size &&
Packit ae235b
      g_file_attribute_matcher_matches (attribute_matcher,
Packit ae235b
                                        G_FILE_ATTRIBUTE_FILESYSTEM_USED))
Packit ae235b
    {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      gchar *localdir = g_path_get_dirname (local->filename);
Packit ae235b
      wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
Packit ae235b
      ULARGE_INTEGER li_free;
Packit ae235b
      ULARGE_INTEGER li_total;
Packit ae235b
Packit ae235b
      g_free (localdir);
Packit ae235b
      if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
Packit ae235b
        g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED,  (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
Packit ae235b
      g_free (wdirname);
Packit ae235b
#else
Packit ae235b
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
#ifdef USE_STATFS
Packit ae235b
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
Packit ae235b
  fstype = statfs_buffer.f_fstypename;
Packit ae235b
#else
Packit ae235b
  fstype = get_fs_type (statfs_buffer.f_type);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#elif defined(USE_STATVFS)
Packit ae235b
#if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
Packit ae235b
  fstype = statfs_buffer.f_fstypename;
Packit ae235b
#elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
Packit ae235b
  fstype = statfs_buffer.f_basetype;
Packit ae235b
#else
Packit ae235b
  fstype = NULL;
Packit ae235b
#endif
Packit ae235b
#endif /* USE_STATFS */
Packit ae235b
Packit ae235b
  if (fstype &&
Packit ae235b
      g_file_attribute_matcher_matches (attribute_matcher,
Packit ae235b
					G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
Packit ae235b
    g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
  if (g_file_attribute_matcher_matches (attribute_matcher,
Packit ae235b
					G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
Packit ae235b
    {
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      get_filesystem_readonly (info, local->filename);
Packit ae235b
#else
Packit ae235b
      get_mount_info (info, local->filename, attribute_matcher);
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (g_file_attribute_matcher_matches (attribute_matcher,
Packit ae235b
					G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE))
Packit ae235b
      g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE,
Packit ae235b
					 g_local_file_is_remote (local->filename));
Packit ae235b
Packit ae235b
  g_file_attribute_matcher_unref (attribute_matcher);
Packit ae235b
  
Packit ae235b
  return info;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GMount *
Packit ae235b
g_local_file_find_enclosing_mount (GFile         *file,
Packit ae235b
                                   GCancellable  *cancellable,
Packit ae235b
                                   GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  GStatBuf buf;
Packit ae235b
  char *mountpoint;
Packit ae235b
  GMount *mount;
Packit ae235b
Packit ae235b
  if (g_lstat (local->filename, &buf) != 0)
Packit ae235b
    goto error;
Packit ae235b
Packit ae235b
  mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
Packit ae235b
  if (mountpoint == NULL)
Packit ae235b
    goto error;
Packit ae235b
Packit ae235b
  mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
Packit ae235b
  g_free (mountpoint);
Packit ae235b
  if (mount)
Packit ae235b
    return mount;
Packit ae235b
Packit ae235b
error:
Packit ae235b
  g_set_io_error (error,
Packit ae235b
		  /* Translators: This is an error message when trying to find
Packit ae235b
		   * the enclosing (user visible) mount of a file, but none
Packit ae235b
		   * exists.
Packit ae235b
		   */
Packit ae235b
		  _("Containing mount for file %s not found"),
Packit ae235b
                  file, 0);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
g_local_file_set_display_name (GFile         *file,
Packit ae235b
			       const char    *display_name,
Packit ae235b
			       GCancellable  *cancellable,
Packit ae235b
			       GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local, *new_local;
Packit ae235b
  GFile *new_file, *parent;
Packit ae235b
  GStatBuf statbuf;
Packit ae235b
  GVfsClass *class;
Packit ae235b
  GVfs *vfs;
Packit ae235b
  int errsv;
Packit ae235b
Packit ae235b
  parent = g_file_get_parent (file);
Packit ae235b
  if (parent == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
                           _("Can’t rename root directory"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  new_file = g_file_get_child_for_display_name (parent, display_name, error);
Packit ae235b
  g_object_unref (parent);
Packit ae235b
  
Packit ae235b
  if (new_file == NULL)
Packit ae235b
    return NULL;
Packit ae235b
  local = G_LOCAL_FILE (file);
Packit ae235b
  new_local = G_LOCAL_FILE (new_file);
Packit ae235b
Packit ae235b
  if (g_lstat (new_local->filename, &statbuf) == -1) 
Packit ae235b
    {
Packit ae235b
      errsv = errno;
Packit ae235b
Packit ae235b
      if (errsv != ENOENT)
Packit ae235b
        {
Packit ae235b
          g_set_io_error (error, _("Error renaming file %s: %s"), new_file, errsv);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
Packit ae235b
                           _("Can’t rename file, filename already exists"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_rename (local->filename, new_local->filename) == -1)
Packit ae235b
    {
Packit ae235b
      errsv = errno;
Packit ae235b
Packit ae235b
      if (errsv == EINVAL)
Packit ae235b
	/* We can't get a rename file into itself error here,
Packit ae235b
	 * so this must be an invalid filename, on e.g. FAT
Packit ae235b
         */
Packit ae235b
	g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME,
Packit ae235b
                             _("Invalid filename"));
Packit ae235b
      else
Packit ae235b
        g_set_io_error (error,
Packit ae235b
		        _("Error renaming file %s: %s"),
Packit ae235b
                        file, errsv);
Packit ae235b
      g_object_unref (new_file);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  vfs = g_vfs_get_default ();
Packit ae235b
  class = G_VFS_GET_CLASS (vfs);
Packit ae235b
  if (class->local_file_moved)
Packit ae235b
    class->local_file_moved (vfs, local->filename, new_local->filename);
Packit ae235b
Packit ae235b
  return new_file;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileInfo *
Packit ae235b
g_local_file_query_info (GFile                *file,
Packit ae235b
			 const char           *attributes,
Packit ae235b
			 GFileQueryInfoFlags   flags,
Packit ae235b
			 GCancellable         *cancellable,
Packit ae235b
			 GError              **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  GFileInfo *info;
Packit ae235b
  GFileAttributeMatcher *matcher;
Packit ae235b
  char *basename, *dirname;
Packit ae235b
  GLocalParentFileInfo parent_info;
Packit ae235b
Packit ae235b
  matcher = g_file_attribute_matcher_new (attributes);
Packit ae235b
  
Packit ae235b
  basename = g_path_get_basename (local->filename);
Packit ae235b
  
Packit ae235b
  dirname = g_path_get_dirname (local->filename);
Packit ae235b
  _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
Packit ae235b
  g_free (dirname);
Packit ae235b
  
Packit ae235b
  info = _g_local_file_info_get (basename, local->filename,
Packit ae235b
				 matcher, flags, &parent_info,
Packit ae235b
				 error);
Packit ae235b
  
Packit ae235b
Packit ae235b
  _g_local_file_info_free_parent_info (&parent_info);
Packit ae235b
  g_free (basename);
Packit ae235b
Packit ae235b
  g_file_attribute_matcher_unref (matcher);
Packit ae235b
Packit ae235b
  return info;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileAttributeInfoList *
Packit ae235b
g_local_file_query_settable_attributes (GFile         *file,
Packit ae235b
					GCancellable  *cancellable,
Packit ae235b
					GError       **error)
Packit ae235b
{
Packit ae235b
  return g_file_attribute_info_list_ref (local_writable_attributes);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileAttributeInfoList *
Packit ae235b
g_local_file_query_writable_namespaces (GFile         *file,
Packit ae235b
					GCancellable  *cancellable,
Packit ae235b
					GError       **error)
Packit ae235b
{
Packit ae235b
  GFileAttributeInfoList *list;
Packit ae235b
  GVfsClass *class;
Packit ae235b
  GVfs *vfs;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&local_writable_namespaces))
Packit ae235b
    {
Packit ae235b
      /* Writable namespaces: */
Packit ae235b
Packit ae235b
      list = g_file_attribute_info_list_new ();
Packit ae235b
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
      g_file_attribute_info_list_add (list,
Packit ae235b
				      "xattr",
Packit ae235b
				      G_FILE_ATTRIBUTE_TYPE_STRING,
Packit ae235b
				      G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
Packit ae235b
				      G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
      g_file_attribute_info_list_add (list,
Packit ae235b
				      "xattr-sys",
Packit ae235b
				      G_FILE_ATTRIBUTE_TYPE_STRING,
Packit ae235b
				      G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      vfs = g_vfs_get_default ();
Packit ae235b
      class = G_VFS_GET_CLASS (vfs);
Packit ae235b
      if (class->add_writable_namespaces)
Packit ae235b
	class->add_writable_namespaces (vfs, list);
Packit ae235b
Packit ae235b
      g_once_init_leave (&local_writable_namespaces, (gsize)list);
Packit ae235b
    }
Packit ae235b
  list = (GFileAttributeInfoList *)local_writable_namespaces;
Packit ae235b
Packit ae235b
  return g_file_attribute_info_list_ref (list);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_set_attribute (GFile                *file,
Packit ae235b
			    const char           *attribute,
Packit ae235b
			    GFileAttributeType    type,
Packit ae235b
			    gpointer              value_p,
Packit ae235b
			    GFileQueryInfoFlags   flags,
Packit ae235b
			    GCancellable         *cancellable,
Packit ae235b
			    GError              **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
Packit ae235b
  return _g_local_file_info_set_attribute (local->filename,
Packit ae235b
					   attribute,
Packit ae235b
					   type,
Packit ae235b
					   value_p,
Packit ae235b
					   flags,
Packit ae235b
					   cancellable,
Packit ae235b
					   error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_set_attributes_from_info (GFile                *file,
Packit ae235b
				       GFileInfo            *info,
Packit ae235b
				       GFileQueryInfoFlags   flags,
Packit ae235b
				       GCancellable         *cancellable,
Packit ae235b
				       GError              **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  int res, chained_res;
Packit ae235b
  GFileIface *default_iface;
Packit ae235b
Packit ae235b
  res = _g_local_file_info_set_attributes (local->filename,
Packit ae235b
					   info, flags, 
Packit ae235b
					   cancellable,
Packit ae235b
					   error);
Packit ae235b
Packit ae235b
  if (!res)
Packit ae235b
    error = NULL; /* Don't write over error if further errors */
Packit ae235b
Packit ae235b
  default_iface = g_type_default_interface_peek (G_TYPE_FILE);
Packit ae235b
Packit ae235b
  chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
Packit ae235b
  
Packit ae235b
  return res && chained_res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileInputStream *
Packit ae235b
g_local_file_read (GFile         *file,
Packit ae235b
		   GCancellable  *cancellable,
Packit ae235b
		   GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  int fd, ret;
Packit ae235b
  GLocalFileStat buf;
Packit ae235b
  
Packit ae235b
  fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
Packit ae235b
  if (fd == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      if (errsv == EACCES)
Packit ae235b
	{
Packit ae235b
	  /* Exploit the fact that on W32 the glib filename encoding is UTF8 */
Packit ae235b
	  ret = GLIB_PRIVATE_CALL (g_win32_stat_utf8) (local->filename, &buf;;
Packit ae235b
	  if (ret == 0 && S_ISDIR (buf.st_mode))
Packit ae235b
            errsv = EISDIR;
Packit ae235b
	}
Packit ae235b
#endif
Packit ae235b
      g_set_io_error (error,
Packit ae235b
		      _("Error opening file %s: %s"),
Packit ae235b
                      file, errsv);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  ret = GLIB_PRIVATE_CALL (g_win32_fstat) (fd, &buf;;
Packit ae235b
#else
Packit ae235b
  ret = fstat (fd, &buf;;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (ret == 0 && S_ISDIR (buf.st_mode))
Packit ae235b
    {
Packit ae235b
      (void) g_close (fd, NULL);
Packit ae235b
      g_set_io_error (error,
Packit ae235b
		      _("Error opening file %s: %s"),
Packit ae235b
                      file, EISDIR);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return _g_local_file_input_stream_new (fd);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileOutputStream *
Packit ae235b
g_local_file_append_to (GFile             *file,
Packit ae235b
			GFileCreateFlags   flags,
Packit ae235b
			GCancellable      *cancellable,
Packit ae235b
			GError           **error)
Packit ae235b
{
Packit ae235b
  return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
Packit ae235b
					     flags, cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileOutputStream *
Packit ae235b
g_local_file_create (GFile             *file,
Packit ae235b
		     GFileCreateFlags   flags,
Packit ae235b
		     GCancellable      *cancellable,
Packit ae235b
		     GError           **error)
Packit ae235b
{
Packit ae235b
  return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
Packit ae235b
                                             FALSE, flags, NULL,
Packit ae235b
                                             cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileOutputStream *
Packit ae235b
g_local_file_replace (GFile             *file,
Packit ae235b
		      const char        *etag,
Packit ae235b
		      gboolean           make_backup,
Packit ae235b
		      GFileCreateFlags   flags,
Packit ae235b
		      GCancellable      *cancellable,
Packit ae235b
		      GError           **error)
Packit ae235b
{
Packit ae235b
  return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
Packit ae235b
                                              FALSE,
Packit ae235b
                                              etag, make_backup, flags, NULL,
Packit ae235b
                                              cancellable, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileIOStream *
Packit ae235b
g_local_file_open_readwrite (GFile                      *file,
Packit ae235b
			     GCancellable               *cancellable,
Packit ae235b
			     GError                    **error)
Packit ae235b
{
Packit ae235b
  GFileOutputStream *output;
Packit ae235b
  GFileIOStream *res;
Packit ae235b
Packit ae235b
  output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
Packit ae235b
					     TRUE,
Packit ae235b
					     cancellable, error);
Packit ae235b
  if (output == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
Packit ae235b
  g_object_unref (output);
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileIOStream *
Packit ae235b
g_local_file_create_readwrite (GFile                      *file,
Packit ae235b
			       GFileCreateFlags            flags,
Packit ae235b
			       GCancellable               *cancellable,
Packit ae235b
			       GError                    **error)
Packit ae235b
{
Packit ae235b
  GFileOutputStream *output;
Packit ae235b
  GFileIOStream *res;
Packit ae235b
Packit ae235b
  output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
Packit ae235b
					       TRUE, flags, NULL,
Packit ae235b
					       cancellable, error);
Packit ae235b
  if (output == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
Packit ae235b
  g_object_unref (output);
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileIOStream *
Packit ae235b
g_local_file_replace_readwrite (GFile                      *file,
Packit ae235b
				const char                 *etag,
Packit ae235b
				gboolean                    make_backup,
Packit ae235b
				GFileCreateFlags            flags,
Packit ae235b
				GCancellable               *cancellable,
Packit ae235b
				GError                    **error)
Packit ae235b
{
Packit ae235b
  GFileOutputStream *output;
Packit ae235b
  GFileIOStream *res;
Packit ae235b
Packit ae235b
  output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
Packit ae235b
                                                TRUE,
Packit ae235b
                                                etag, make_backup, flags, NULL,
Packit ae235b
                                                cancellable, error);
Packit ae235b
  if (output == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
Packit ae235b
  g_object_unref (output);
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_delete (GFile         *file,
Packit ae235b
		     GCancellable  *cancellable,
Packit ae235b
		     GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  GVfsClass *class;
Packit ae235b
  GVfs *vfs;
Packit ae235b
Packit ae235b
  if (g_remove (local->filename) == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      /* Posix allows EEXIST too, but the more sane error
Packit ae235b
	 is G_IO_ERROR_NOT_FOUND, and it's what nautilus
Packit ae235b
	 expects */
Packit ae235b
      if (errsv == EEXIST)
Packit ae235b
	errsv = ENOTEMPTY;
Packit ae235b
Packit ae235b
      g_set_io_error (error,
Packit ae235b
		      _("Error removing file %s: %s"),
Packit ae235b
                      file, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  vfs = g_vfs_get_default ();
Packit ae235b
  class = G_VFS_GET_CLASS (vfs);
Packit ae235b
  if (class->local_file_removed)
Packit ae235b
    class->local_file_removed (vfs, local->filename);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
static char *
Packit ae235b
strip_trailing_slashes (const char *path)
Packit ae235b
{
Packit ae235b
  char *path_copy;
Packit ae235b
  int len;
Packit ae235b
Packit ae235b
  path_copy = g_strdup (path);
Packit ae235b
  len = strlen (path_copy);
Packit ae235b
  while (len > 1 && path_copy[len-1] == '/')
Packit ae235b
    path_copy[--len] = 0;
Packit ae235b
Packit ae235b
  return path_copy;
Packit ae235b
 }
Packit ae235b
Packit ae235b
static char *
Packit ae235b
expand_symlink (const char *link)
Packit ae235b
{
Packit ae235b
  char *resolved, *canonical, *parent, *link2;
Packit ae235b
  char symlink_value[4096];
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#else
Packit ae235b
  gssize res;
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#else
Packit ae235b
  res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
Packit ae235b
  
Packit ae235b
  if (res == -1)
Packit ae235b
    return g_strdup (link);
Packit ae235b
  symlink_value[res] = 0;
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
  if (g_path_is_absolute (symlink_value))
Packit ae235b
    return canonicalize_filename (symlink_value);
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      link2 = strip_trailing_slashes (link);
Packit ae235b
      parent = g_path_get_dirname (link2);
Packit ae235b
      g_free (link2);
Packit ae235b
      
Packit ae235b
      resolved = g_build_filename (parent, symlink_value, NULL);
Packit ae235b
      g_free (parent);
Packit ae235b
      
Packit ae235b
      canonical = canonicalize_filename (resolved);
Packit ae235b
      
Packit ae235b
      g_free (resolved);
Packit ae235b
Packit ae235b
      return canonical;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
get_parent (const char *path, 
Packit ae235b
            dev_t      *parent_dev)
Packit ae235b
{
Packit ae235b
  char *parent, *tmp;
Packit ae235b
  GStatBuf parent_stat;
Packit ae235b
  int num_recursions;
Packit ae235b
  char *path_copy;
Packit ae235b
Packit ae235b
  path_copy = strip_trailing_slashes (path);
Packit ae235b
  
Packit ae235b
  parent = g_path_get_dirname (path_copy);
Packit ae235b
  if (strcmp (parent, ".") == 0 ||
Packit ae235b
      strcmp (parent, path_copy) == 0)
Packit ae235b
    {
Packit ae235b
      g_free (parent);
Packit ae235b
      g_free (path_copy);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  g_free (path_copy);
Packit ae235b
Packit ae235b
  num_recursions = 0;
Packit ae235b
  do {
Packit ae235b
    if (g_lstat (parent, &parent_stat) != 0)
Packit ae235b
      {
Packit ae235b
	g_free (parent);
Packit ae235b
	return NULL;
Packit ae235b
      }
Packit ae235b
    
Packit ae235b
    if (S_ISLNK (parent_stat.st_mode))
Packit ae235b
      {
Packit ae235b
	tmp = parent;
Packit ae235b
	parent = expand_symlink (parent);
Packit ae235b
	g_free (tmp);
Packit ae235b
      }
Packit ae235b
    
Packit ae235b
    num_recursions++;
Packit ae235b
    if (num_recursions > 12)
Packit ae235b
      {
Packit ae235b
	g_free (parent);
Packit ae235b
	return NULL;
Packit ae235b
      }
Packit ae235b
  } while (S_ISLNK (parent_stat.st_mode));
Packit ae235b
Packit ae235b
  *parent_dev = parent_stat.st_dev;
Packit ae235b
  
Packit ae235b
  return parent;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
expand_all_symlinks (const char *path)
Packit ae235b
{
Packit ae235b
  char *parent, *parent_expanded;
Packit ae235b
  char *basename, *res;
Packit ae235b
  dev_t parent_dev;
Packit ae235b
Packit ae235b
  parent = get_parent (path, &parent_dev);
Packit ae235b
  if (parent)
Packit ae235b
    {
Packit ae235b
      parent_expanded = expand_all_symlinks (parent);
Packit ae235b
      g_free (parent);
Packit ae235b
      basename = g_path_get_basename (path);
Packit ae235b
      res = g_build_filename (parent_expanded, basename, NULL);
Packit ae235b
      g_free (basename);
Packit ae235b
      g_free (parent_expanded);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    res = g_strdup (path);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
find_mountpoint_for (const char *file, 
Packit ae235b
                     dev_t       dev)
Packit ae235b
{
Packit ae235b
  char *dir, *parent;
Packit ae235b
  dev_t dir_dev, parent_dev;
Packit ae235b
Packit ae235b
  dir = g_strdup (file);
Packit ae235b
  dir_dev = dev;
Packit ae235b
Packit ae235b
  while (1) 
Packit ae235b
    {
Packit ae235b
      parent = get_parent (dir, &parent_dev);
Packit ae235b
      if (parent == NULL)
Packit ae235b
        return dir;
Packit ae235b
    
Packit ae235b
      if (parent_dev != dir_dev)
Packit ae235b
        {
Packit ae235b
          g_free (parent);
Packit ae235b
          return dir;
Packit ae235b
        }
Packit ae235b
    
Packit ae235b
      g_free (dir);
Packit ae235b
      dir = parent;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
char *
Packit ae235b
_g_local_file_find_topdir_for (const char *file)
Packit ae235b
{
Packit ae235b
  char *dir;
Packit ae235b
  char *mountpoint = NULL;
Packit ae235b
  dev_t dir_dev;
Packit ae235b
Packit ae235b
  dir = get_parent (file, &dir_dev);
Packit ae235b
  if (dir == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  mountpoint = find_mountpoint_for (dir, dir_dev);
Packit ae235b
  g_free (dir);
Packit ae235b
Packit ae235b
  return mountpoint;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
get_unique_filename (const char *basename, 
Packit ae235b
                     int         id)
Packit ae235b
{
Packit ae235b
  const char *dot;
Packit ae235b
      
Packit ae235b
  if (id == 1)
Packit ae235b
    return g_strdup (basename);
Packit ae235b
Packit ae235b
  dot = strchr (basename, '.');
Packit ae235b
  if (dot)
Packit ae235b
    return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
Packit ae235b
  else
Packit ae235b
    return g_strdup_printf ("%s.%d", basename, id);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
path_has_prefix (const char *path, 
Packit ae235b
                 const char *prefix)
Packit ae235b
{
Packit ae235b
  int prefix_len;
Packit ae235b
Packit ae235b
  if (prefix == NULL)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  prefix_len = strlen (prefix);
Packit ae235b
  
Packit ae235b
  if (strncmp (path, prefix, prefix_len) == 0 &&
Packit ae235b
      (prefix_len == 0 || /* empty prefix always matches */
Packit ae235b
       prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
Packit ae235b
       path[prefix_len] == 0 ||
Packit ae235b
       path[prefix_len] == '/'))
Packit ae235b
    return TRUE;
Packit ae235b
  
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
try_make_relative (const char *path, 
Packit ae235b
                   const char *base)
Packit ae235b
{
Packit ae235b
  char *path2, *base2;
Packit ae235b
  char *relative;
Packit ae235b
Packit ae235b
  path2 = expand_all_symlinks (path);
Packit ae235b
  base2 = expand_all_symlinks (base);
Packit ae235b
Packit ae235b
  relative = NULL;
Packit ae235b
  if (path_has_prefix (path2, base2))
Packit ae235b
    {
Packit ae235b
      relative = path2 + strlen (base2);
Packit ae235b
      while (*relative == '/')
Packit ae235b
	relative ++;
Packit ae235b
      relative = g_strdup (relative);
Packit ae235b
    }
Packit ae235b
  g_free (path2);
Packit ae235b
  g_free (base2);
Packit ae235b
Packit ae235b
  if (relative)
Packit ae235b
    return relative;
Packit ae235b
  
Packit ae235b
  /* Failed, use abs path */
Packit ae235b
  return g_strdup (path);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
_g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
Packit ae235b
{
Packit ae235b
  static gsize home_dev_set = 0;
Packit ae235b
  static dev_t home_dev;
Packit ae235b
  char *topdir, *globaldir, *trashdir, *tmpname;
Packit ae235b
  uid_t uid;
Packit ae235b
  char uid_str[32];
Packit ae235b
  GStatBuf global_stat, trash_stat;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&home_dev_set))
Packit ae235b
    {
Packit ae235b
      GStatBuf home_stat;
Packit ae235b
Packit ae235b
      g_stat (g_get_home_dir (), &home_stat);
Packit ae235b
      home_dev = home_stat.st_dev;
Packit ae235b
      g_once_init_leave (&home_dev_set, 1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Assume we can trash to the home */
Packit ae235b
  if (dir_dev == home_dev)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  topdir = find_mountpoint_for (dirname, dir_dev);
Packit ae235b
  if (topdir == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  globaldir = g_build_filename (topdir, ".Trash", NULL);
Packit ae235b
  if (g_lstat (globaldir, &global_stat) == 0 &&
Packit ae235b
      S_ISDIR (global_stat.st_mode) &&
Packit ae235b
      (global_stat.st_mode & S_ISVTX) != 0)
Packit ae235b
    {
Packit ae235b
      /* got a toplevel sysadmin created dir, assume we
Packit ae235b
       * can trash to it (we should be able to create a dir)
Packit ae235b
       * This fails for the FAT case where the ownership of
Packit ae235b
       * that dir would be wrong though..
Packit ae235b
       */
Packit ae235b
      g_free (globaldir);
Packit ae235b
      g_free (topdir);
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
  g_free (globaldir);
Packit ae235b
Packit ae235b
  /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
Packit ae235b
  uid = geteuid ();
Packit ae235b
  g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
Packit ae235b
Packit ae235b
  tmpname = g_strdup_printf (".Trash-%s", uid_str);
Packit ae235b
  trashdir = g_build_filename (topdir, tmpname, NULL);
Packit ae235b
  g_free (tmpname);
Packit ae235b
Packit ae235b
  if (g_lstat (trashdir, &trash_stat) == 0)
Packit ae235b
    {
Packit ae235b
      g_free (topdir);
Packit ae235b
      g_free (trashdir);
Packit ae235b
      return S_ISDIR (trash_stat.st_mode) &&
Packit ae235b
	     trash_stat.st_uid == uid;
Packit ae235b
    }
Packit ae235b
  g_free (trashdir);
Packit ae235b
Packit ae235b
  /* User specific trash didn't exist, can we create it? */
Packit ae235b
  res = g_access (topdir, W_OK) == 0;
Packit ae235b
  g_free (topdir);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
gboolean
Packit ae235b
_g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
Packit ae235b
{
Packit ae235b
  gboolean ret = FALSE;
Packit ae235b
  gchar *mount_dir = NULL;
Packit ae235b
  size_t mount_dir_len;
Packit ae235b
  GStatBuf statbuf;
Packit ae235b
Packit ae235b
  if (!g_str_has_suffix (path, "/lost+found"))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  mount_dir = find_mountpoint_for (path, path_dev);
Packit ae235b
  if (mount_dir == NULL)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  mount_dir_len = strlen (mount_dir);
Packit ae235b
  /* We special-case rootfs ('/') since it's the only case where
Packit ae235b
   * mount_dir ends in '/'
Packit ae235b
   */
Packit ae235b
  if (mount_dir_len == 1)
Packit ae235b
    mount_dir_len--;
Packit ae235b
  if (mount_dir_len + strlen ("/lost+found") != strlen (path))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  if (g_lstat (path, &statbuf) != 0)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  if (!(S_ISDIR (statbuf.st_mode) &&
Packit ae235b
        statbuf.st_uid == 0 &&
Packit ae235b
        statbuf.st_gid == 0))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_free (mount_dir);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_trash (GFile         *file,
Packit ae235b
		    GCancellable  *cancellable,
Packit ae235b
		    GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  GStatBuf file_stat, home_stat;
Packit ae235b
  const char *homedir;
Packit ae235b
  char *trashdir, *topdir, *infodir, *filesdir;
Packit ae235b
  char *basename, *trashname, *trashfile, *infoname, *infofile;
Packit ae235b
  char *original_name, *original_name_escaped;
Packit ae235b
  int i;
Packit ae235b
  char *data;
Packit ae235b
  gboolean is_homedir_trash;
Packit ae235b
  char delete_time[32];
Packit ae235b
  int fd;
Packit ae235b
  GStatBuf trash_stat, global_stat;
Packit ae235b
  char *dirname, *globaldir;
Packit ae235b
  GVfsClass *class;
Packit ae235b
  GVfs *vfs;
Packit ae235b
  int errsv;
Packit ae235b
Packit ae235b
  if (g_lstat (local->filename, &file_stat) != 0)
Packit ae235b
    {
Packit ae235b
      errsv = errno;
Packit ae235b
Packit ae235b
      g_set_io_error (error,
Packit ae235b
		      _("Error trashing file %s: %s"),
Packit ae235b
                      file, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
    
Packit ae235b
  homedir = g_get_home_dir ();
Packit ae235b
  g_stat (homedir, &home_stat);
Packit ae235b
Packit ae235b
  is_homedir_trash = FALSE;
Packit ae235b
  trashdir = NULL;
Packit ae235b
  if (file_stat.st_dev == home_stat.st_dev)
Packit ae235b
    {
Packit ae235b
      is_homedir_trash = TRUE;
Packit ae235b
      errno = 0;
Packit ae235b
      trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
Packit ae235b
      if (g_mkdir_with_parents (trashdir, 0700) < 0)
Packit ae235b
	{
Packit ae235b
          char *display_name;
Packit ae235b
          errsv = errno;
Packit ae235b
Packit ae235b
          display_name = g_filename_display_name (trashdir);
Packit ae235b
          g_set_error (error, G_IO_ERROR,
Packit ae235b
                       g_io_error_from_errno (errsv),
Packit ae235b
                       _("Unable to create trash dir %s: %s"),
Packit ae235b
                       display_name, g_strerror (errsv));
Packit ae235b
          g_free (display_name);
Packit ae235b
          g_free (trashdir);
Packit ae235b
          return FALSE;
Packit ae235b
	}
Packit ae235b
      topdir = g_strdup (g_get_user_data_dir ());
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      uid_t uid;
Packit ae235b
      char uid_str[32];
Packit ae235b
Packit ae235b
      uid = geteuid ();
Packit ae235b
      g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
Packit ae235b
Packit ae235b
      topdir = _g_local_file_find_topdir_for (local->filename);
Packit ae235b
      if (topdir == NULL)
Packit ae235b
	{
Packit ae235b
          g_set_io_error (error,
Packit ae235b
                          _("Unable to find toplevel directory to trash %s"),
Packit ae235b
                          file, G_IO_ERROR_NOT_SUPPORTED);
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      /* Try looking for global trash dir $topdir/.Trash/$uid */
Packit ae235b
      globaldir = g_build_filename (topdir, ".Trash", NULL);
Packit ae235b
      if (g_lstat (globaldir, &global_stat) == 0 &&
Packit ae235b
	  S_ISDIR (global_stat.st_mode) &&
Packit ae235b
	  (global_stat.st_mode & S_ISVTX) != 0)
Packit ae235b
	{
Packit ae235b
	  trashdir = g_build_filename (globaldir, uid_str, NULL);
Packit ae235b
Packit ae235b
	  if (g_lstat (trashdir, &trash_stat) == 0)
Packit ae235b
	    {
Packit ae235b
	      if (!S_ISDIR (trash_stat.st_mode) ||
Packit ae235b
		  trash_stat.st_uid != uid)
Packit ae235b
		{
Packit ae235b
		  /* Not a directory or not owned by user, ignore */
Packit ae235b
		  g_free (trashdir);
Packit ae235b
		  trashdir = NULL;
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	  else if (g_mkdir (trashdir, 0700) == -1)
Packit ae235b
	    {
Packit ae235b
	      g_free (trashdir);
Packit ae235b
	      trashdir = NULL;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      g_free (globaldir);
Packit ae235b
Packit ae235b
      if (trashdir == NULL)
Packit ae235b
	{
Packit ae235b
	  gboolean tried_create;
Packit ae235b
	  
Packit ae235b
	  /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
Packit ae235b
	  dirname = g_strdup_printf (".Trash-%s", uid_str);
Packit ae235b
	  trashdir = g_build_filename (topdir, dirname, NULL);
Packit ae235b
	  g_free (dirname);
Packit ae235b
Packit ae235b
	  tried_create = FALSE;
Packit ae235b
Packit ae235b
	retry:
Packit ae235b
	  if (g_lstat (trashdir, &trash_stat) == 0)
Packit ae235b
	    {
Packit ae235b
	      if (!S_ISDIR (trash_stat.st_mode) ||
Packit ae235b
		  trash_stat.st_uid != uid)
Packit ae235b
		{
Packit ae235b
		  /* Remove the failed directory */
Packit ae235b
		  if (tried_create)
Packit ae235b
		    g_remove (trashdir);
Packit ae235b
		  
Packit ae235b
		  /* Not a directory or not owned by user, ignore */
Packit ae235b
		  g_free (trashdir);
Packit ae235b
		  trashdir = NULL;
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    {
Packit ae235b
	      if (!tried_create &&
Packit ae235b
		  g_mkdir (trashdir, 0700) != -1)
Packit ae235b
		{
Packit ae235b
		  /* Ensure that the created dir has the right uid etc.
Packit ae235b
		     This might fail on e.g. a FAT dir */
Packit ae235b
		  tried_create = TRUE;
Packit ae235b
		  goto retry;
Packit ae235b
		}
Packit ae235b
	      else
Packit ae235b
		{
Packit ae235b
		  g_free (trashdir);
Packit ae235b
		  trashdir = NULL;
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (trashdir == NULL)
Packit ae235b
	{
Packit ae235b
	  g_free (topdir);
Packit ae235b
          g_set_io_error (error,
Packit ae235b
                          _("Unable to find or create trash directory for %s"),
Packit ae235b
                          file, G_IO_ERROR_NOT_SUPPORTED);
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
Packit ae235b
Packit ae235b
  infodir = g_build_filename (trashdir, "info", NULL);
Packit ae235b
  filesdir = g_build_filename (trashdir, "files", NULL);
Packit ae235b
  g_free (trashdir);
Packit ae235b
Packit ae235b
  /* Make sure we have the subdirectories */
Packit ae235b
  if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
Packit ae235b
      (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
Packit ae235b
    {
Packit ae235b
      g_free (topdir);
Packit ae235b
      g_free (infodir);
Packit ae235b
      g_free (filesdir);
Packit ae235b
      g_set_io_error (error,
Packit ae235b
                      _("Unable to find or create trash directory for %s"),
Packit ae235b
                      file, G_IO_ERROR_NOT_SUPPORTED);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  basename = g_path_get_basename (local->filename);
Packit ae235b
  i = 1;
Packit ae235b
  trashname = NULL;
Packit ae235b
  infofile = NULL;
Packit ae235b
  do {
Packit ae235b
    g_free (trashname);
Packit ae235b
    g_free (infofile);
Packit ae235b
    
Packit ae235b
    trashname = get_unique_filename (basename, i++);
Packit ae235b
    infoname = g_strconcat (trashname, ".trashinfo", NULL);
Packit ae235b
    infofile = g_build_filename (infodir, infoname, NULL);
Packit ae235b
    g_free (infoname);
Packit ae235b
Packit ae235b
    fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
Packit ae235b
    errsv = errno;
Packit ae235b
  } while (fd == -1 && errsv == EEXIST);
Packit ae235b
Packit ae235b
  g_free (basename);
Packit ae235b
  g_free (infodir);
Packit ae235b
Packit ae235b
  if (fd == -1)
Packit ae235b
    {
Packit ae235b
      errsv = errno;
Packit ae235b
Packit ae235b
      g_free (filesdir);
Packit ae235b
      g_free (topdir);
Packit ae235b
      g_free (trashname);
Packit ae235b
      g_free (infofile);
Packit ae235b
Packit ae235b
      g_set_io_error (error,
Packit ae235b
		      _("Unable to create trashing info file for %s: %s"),
Packit ae235b
                      file, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  (void) g_close (fd, NULL);
Packit ae235b
Packit ae235b
  /* Write the full content of the info file before trashing to make
Packit ae235b
   * sure someone doesn't read an empty file.  See #749314
Packit ae235b
   */
Packit ae235b
Packit ae235b
  /* Use absolute names for homedir */
Packit ae235b
  if (is_homedir_trash)
Packit ae235b
    original_name = g_strdup (local->filename);
Packit ae235b
  else
Packit ae235b
    original_name = try_make_relative (local->filename, topdir);
Packit ae235b
  original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
Packit ae235b
  
Packit ae235b
  g_free (original_name);
Packit ae235b
  g_free (topdir);
Packit ae235b
  
Packit ae235b
  {
Packit ae235b
    time_t t;
Packit ae235b
    struct tm now;
Packit ae235b
    t = time (NULL);
Packit ae235b
    localtime_r (&t, &now;;
Packit ae235b
    delete_time[0] = 0;
Packit ae235b
    strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now;;
Packit ae235b
  }
Packit ae235b
Packit ae235b
  data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
Packit ae235b
			  original_name_escaped, delete_time);
Packit ae235b
Packit ae235b
  g_file_set_contents (infofile, data, -1, NULL);
Packit ae235b
Packit ae235b
  /* TODO: Maybe we should verify that you can delete the file from the trash
Packit ae235b
   * before moving it? OTOH, that is hard, as it needs a recursive scan
Packit ae235b
   */
Packit ae235b
Packit ae235b
  trashfile = g_build_filename (filesdir, trashname, NULL);
Packit ae235b
Packit ae235b
  g_free (filesdir);
Packit ae235b
Packit ae235b
  if (g_rename (local->filename, trashfile) == -1)
Packit ae235b
    {
Packit ae235b
      errsv = errno;
Packit ae235b
Packit ae235b
      g_unlink (infofile);
Packit ae235b
Packit ae235b
      g_free (trashname);
Packit ae235b
      g_free (infofile);
Packit ae235b
      g_free (trashfile);
Packit ae235b
Packit ae235b
      if (errsv == EXDEV)
Packit ae235b
	/* The trash dir was actually on another fs anyway!?
Packit ae235b
	 * This can happen when the same device is mounted multiple
Packit ae235b
	 * times, or with bind mounts of the same fs.
Packit ae235b
	 */
Packit ae235b
        g_set_io_error (error,
Packit ae235b
                        _("Unable to trash file %s across filesystem boundaries"),
Packit ae235b
                        file, ENOTSUP);
Packit ae235b
      else
Packit ae235b
        g_set_io_error (error,
Packit ae235b
		        _("Unable to trash file %s: %s"),
Packit ae235b
                        file, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  vfs = g_vfs_get_default ();
Packit ae235b
  class = G_VFS_GET_CLASS (vfs);
Packit ae235b
  if (class->local_file_moved)
Packit ae235b
    class->local_file_moved (vfs, local->filename, trashfile);
Packit ae235b
Packit ae235b
  g_free (trashfile);
Packit ae235b
Packit ae235b
  /* TODO: Do we need to update mtime/atime here after the move? */
Packit ae235b
Packit ae235b
  g_free (infofile);
Packit ae235b
  g_free (data);
Packit ae235b
  
Packit ae235b
  g_free (original_name_escaped);
Packit ae235b
  g_free (trashname);
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#else /* G_OS_WIN32 */
Packit ae235b
gboolean
Packit ae235b
_g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
Packit ae235b
{
Packit ae235b
  return FALSE;			/* XXX ??? */
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_trash (GFile         *file,
Packit ae235b
		    GCancellable  *cancellable,
Packit ae235b
		    GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  SHFILEOPSTRUCTW op = {0};
Packit ae235b
  gboolean success;
Packit ae235b
  wchar_t *wfilename;
Packit ae235b
  long len;
Packit ae235b
Packit ae235b
  wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
Packit ae235b
  /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
Packit ae235b
  wfilename = g_renew (wchar_t, wfilename, len + 2);
Packit ae235b
  wfilename[len + 1] = 0;
Packit ae235b
Packit ae235b
  op.wFunc = FO_DELETE;
Packit ae235b
  op.pFrom = wfilename;
Packit ae235b
  op.fFlags = FOF_ALLOWUNDO;
Packit ae235b
Packit ae235b
  success = SHFileOperationW (&op) == 0;
Packit ae235b
Packit ae235b
  if (success && op.fAnyOperationsAborted)
Packit ae235b
    {
Packit ae235b
      if (cancellable && !g_cancellable_is_cancelled (cancellable))
Packit ae235b
	g_cancellable_cancel (cancellable);
Packit ae235b
      g_set_io_error (error,
Packit ae235b
                      _("Unable to trash file %s: %s"),
Packit ae235b
                      file, ECANCELED);
Packit ae235b
      success = FALSE;
Packit ae235b
    }
Packit ae235b
  else if (!success)
Packit ae235b
    g_set_io_error (error,
Packit ae235b
                    _("Unable to trash file %s"),
Packit ae235b
                    file, 0);
Packit ae235b
Packit ae235b
  g_free (wfilename);
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_make_directory (GFile         *file,
Packit ae235b
			     GCancellable  *cancellable,
Packit ae235b
			     GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  
Packit ae235b
  if (g_mkdir (local->filename, 0777) == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      if (errsv == EINVAL)
Packit ae235b
	/* This must be an invalid filename, on e.g. FAT */
Packit ae235b
	g_set_error_literal (error, G_IO_ERROR,
Packit ae235b
                             G_IO_ERROR_INVALID_FILENAME,
Packit ae235b
                             _("Invalid filename"));
Packit ae235b
      else
Packit ae235b
        g_set_io_error (error,
Packit ae235b
		        _("Error creating directory %s: %s"),
Packit ae235b
                        file, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_make_symbolic_link (GFile         *file,
Packit ae235b
				 const char    *symlink_value,
Packit ae235b
				 GCancellable  *cancellable,
Packit ae235b
				 GError       **error)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_SYMLINK
Packit ae235b
  GLocalFile *local = G_LOCAL_FILE (file);
Packit ae235b
  
Packit ae235b
  if (symlink (symlink_value, local->filename) == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      if (errsv == EINVAL)
Packit ae235b
	/* This must be an invalid filename, on e.g. FAT */
Packit ae235b
	g_set_error_literal (error, G_IO_ERROR,
Packit ae235b
                             G_IO_ERROR_INVALID_FILENAME,
Packit ae235b
                             _("Invalid filename"));
Packit ae235b
      else if (errsv == EPERM)
Packit ae235b
	g_set_error (error, G_IO_ERROR,
Packit ae235b
		     G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
		     _("Filesystem does not support symbolic links"));
Packit ae235b
      else
Packit ae235b
        g_set_io_error (error,
Packit ae235b
		        _("Error making symbolic link %s: %s"),
Packit ae235b
                        file, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
#else
Packit ae235b
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Symbolic links not supported"));
Packit ae235b
  return FALSE;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_copy (GFile                  *source,
Packit ae235b
		   GFile                  *destination,
Packit ae235b
		   GFileCopyFlags          flags,
Packit ae235b
		   GCancellable           *cancellable,
Packit ae235b
		   GFileProgressCallback   progress_callback,
Packit ae235b
		   gpointer                progress_callback_data,
Packit ae235b
		   GError                **error)
Packit ae235b
{
Packit ae235b
  /* Fall back to default copy */
Packit ae235b
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_move (GFile                  *source,
Packit ae235b
		   GFile                  *destination,
Packit ae235b
		   GFileCopyFlags          flags,
Packit ae235b
		   GCancellable           *cancellable,
Packit ae235b
		   GFileProgressCallback   progress_callback,
Packit ae235b
		   gpointer                progress_callback_data,
Packit ae235b
		   GError                **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local_source, *local_destination;
Packit ae235b
  GStatBuf statbuf;
Packit ae235b
  gboolean destination_exist, source_is_dir;
Packit ae235b
  char *backup_name;
Packit ae235b
  int res;
Packit ae235b
  off_t source_size;
Packit ae235b
  GVfsClass *class;
Packit ae235b
  GVfs *vfs;
Packit ae235b
Packit ae235b
  if (!G_IS_LOCAL_FILE (source) ||
Packit ae235b
      !G_IS_LOCAL_FILE (destination))
Packit ae235b
    {
Packit ae235b
      /* Fall back to default move */
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  local_source = G_LOCAL_FILE (source);
Packit ae235b
  local_destination = G_LOCAL_FILE (destination);
Packit ae235b
  
Packit ae235b
  res = g_lstat (local_source->filename, &statbuf);
Packit ae235b
  if (res == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_set_io_error (error,
Packit ae235b
                      _("Error moving file %s: %s"),
Packit ae235b
                      source, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  source_is_dir = S_ISDIR (statbuf.st_mode);
Packit ae235b
  source_size = statbuf.st_size;
Packit ae235b
  
Packit ae235b
  destination_exist = FALSE;
Packit ae235b
  res = g_lstat (local_destination->filename, &statbuf);
Packit ae235b
  if (res == 0)
Packit ae235b
    {
Packit ae235b
      destination_exist = TRUE; /* Target file exists */
Packit ae235b
Packit ae235b
      if (flags & G_FILE_COPY_OVERWRITE)
Packit ae235b
	{
Packit ae235b
	  /* Always fail on dirs, even with overwrite */
Packit ae235b
	  if (S_ISDIR (statbuf.st_mode))
Packit ae235b
	    {
Packit ae235b
	      if (source_is_dir)
Packit ae235b
		g_set_error_literal (error,
Packit ae235b
                                     G_IO_ERROR,
Packit ae235b
                                     G_IO_ERROR_WOULD_MERGE,
Packit ae235b
                                     _("Can’t move directory over directory"));
Packit ae235b
              else
Packit ae235b
		g_set_error_literal (error,
Packit ae235b
                                     G_IO_ERROR,
Packit ae235b
                                     G_IO_ERROR_IS_DIRECTORY,
Packit ae235b
                                     _("Can’t copy over directory"));
Packit ae235b
	      return FALSE;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
          g_set_io_error (error,
Packit ae235b
                          _("Error moving file %s: %s"),
Packit ae235b
                          source, EEXIST);
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (flags & G_FILE_COPY_BACKUP && destination_exist)
Packit ae235b
    {
Packit ae235b
      backup_name = g_strconcat (local_destination->filename, "~", NULL);
Packit ae235b
      if (g_rename (local_destination->filename, backup_name) == -1)
Packit ae235b
	{
Packit ae235b
      	  g_set_error_literal (error,
Packit ae235b
                               G_IO_ERROR,
Packit ae235b
                               G_IO_ERROR_CANT_CREATE_BACKUP,
Packit ae235b
                               _("Backup file creation failed"));
Packit ae235b
	  g_free (backup_name);
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
      g_free (backup_name);
Packit ae235b
      destination_exist = FALSE; /* It did, but no more */
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
Packit ae235b
    {
Packit ae235b
      /* Source is a dir, destination exists (and is not a dir, because that would have failed
Packit ae235b
	 earlier), and we're overwriting. Manually remove the target so we can do the rename. */
Packit ae235b
      res = g_unlink (local_destination->filename);
Packit ae235b
      if (res == -1)
Packit ae235b
	{
Packit ae235b
          int errsv = errno;
Packit ae235b
Packit ae235b
	  g_set_error (error, G_IO_ERROR,
Packit ae235b
		       g_io_error_from_errno (errsv),
Packit ae235b
		       _("Error removing target file: %s"),
Packit ae235b
		       g_strerror (errsv));
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (g_rename (local_source->filename, local_destination->filename) == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      if (errsv == EXDEV)
Packit ae235b
	/* This will cause the fallback code to run */
Packit ae235b
	g_set_error_literal (error, G_IO_ERROR,
Packit ae235b
                             G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                             _("Move between mounts not supported"));
Packit ae235b
      else if (errsv == EINVAL)
Packit ae235b
	/* This must be an invalid filename, on e.g. FAT, or
Packit ae235b
	   we're trying to move the file into itself...
Packit ae235b
	   We return invalid filename for both... */
Packit ae235b
	g_set_error_literal (error, G_IO_ERROR,
Packit ae235b
                             G_IO_ERROR_INVALID_FILENAME,
Packit ae235b
                             _("Invalid filename"));
Packit ae235b
      else
Packit ae235b
        g_set_io_error (error,
Packit ae235b
                        _("Error moving file %s: %s"),
Packit ae235b
                        source, errsv);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  vfs = g_vfs_get_default ();
Packit ae235b
  class = G_VFS_GET_CLASS (vfs);
Packit ae235b
  if (class->local_file_moved)
Packit ae235b
    class->local_file_moved (vfs, local_source->filename, local_destination->filename);
Packit ae235b
Packit ae235b
  /* Make sure we send full copied size */
Packit ae235b
  if (progress_callback)
Packit ae235b
    progress_callback (source_size, source_size, progress_callback_data);
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_local_file_is_remote (const gchar *filename)
Packit ae235b
{
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#else
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_remote_fs (const gchar *filename)
Packit ae235b
{
Packit ae235b
  const char *fsname = NULL;
Packit ae235b
Packit ae235b
#ifdef USE_STATFS
Packit ae235b
  struct statfs statfs_buffer;
Packit ae235b
  int statfs_result = 0;
Packit ae235b
Packit ae235b
#if STATFS_ARGS == 2
Packit ae235b
  statfs_result = statfs (filename, &statfs_buffer);
Packit ae235b
#elif STATFS_ARGS == 4
Packit ae235b
  statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#elif defined(USE_STATVFS)
Packit ae235b
  struct statvfs statfs_buffer;
Packit ae235b
  int statfs_result = 0;
Packit ae235b
Packit ae235b
  statfs_result = statvfs (filename, &statfs_buffer);
Packit ae235b
#else
Packit ae235b
  return FALSE;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (statfs_result == -1)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
#ifdef USE_STATFS
Packit ae235b
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
Packit ae235b
  fsname = statfs_buffer.f_fstypename;
Packit ae235b
#else
Packit ae235b
  fsname = get_fs_type (statfs_buffer.f_type);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
Packit ae235b
  fsname = statfs_buffer.f_basetype;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (fsname != NULL)
Packit ae235b
    {
Packit ae235b
      if (strcmp (fsname, "nfs") == 0)
Packit ae235b
        return TRUE;
Packit ae235b
      if (strcmp (fsname, "nfs4") == 0)
Packit ae235b
        return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_local_file_is_remote (const gchar *filename)
Packit ae235b
{
Packit ae235b
  static gboolean remote_home;
Packit ae235b
  static gsize initialized;
Packit ae235b
  const gchar *home;
Packit ae235b
Packit ae235b
  home = g_get_home_dir ();
Packit ae235b
  if (path_has_prefix (filename, home))
Packit ae235b
    {
Packit ae235b
      if (g_once_init_enter (&initialized))
Packit ae235b
        {
Packit ae235b
          remote_home = is_remote_fs (home);
Packit ae235b
          g_once_init_leave (&initialized, TRUE);
Packit ae235b
        }
Packit ae235b
      return remote_home;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
static GFileMonitor*
Packit ae235b
g_local_file_monitor_dir (GFile             *file,
Packit ae235b
			  GFileMonitorFlags  flags,
Packit ae235b
			  GCancellable      *cancellable,
Packit ae235b
			  GError           **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local_file = G_LOCAL_FILE (file);
Packit ae235b
Packit ae235b
  return g_local_file_monitor_new_for_path (local_file->filename, TRUE, flags, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFileMonitor*
Packit ae235b
g_local_file_monitor_file (GFile             *file,
Packit ae235b
			   GFileMonitorFlags  flags,
Packit ae235b
			   GCancellable      *cancellable,
Packit ae235b
			   GError           **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local_file = G_LOCAL_FILE (file);
Packit ae235b
Packit ae235b
  return g_local_file_monitor_new_for_path (local_file->filename, FALSE, flags, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Here is the GLocalFile implementation of g_file_measure_disk_usage().
Packit ae235b
 *
Packit ae235b
 * If available, we use fopenat() in preference to filenames for
Packit ae235b
 * efficiency and safety reasons.  We know that fopenat() is available
Packit ae235b
 * based on if AT_FDCWD is defined.  POSIX guarantees that this will be
Packit ae235b
 * defined as a macro.
Packit ae235b
 *
Packit ae235b
 * We use a linked list of stack-allocated GSList nodes in order to be
Packit ae235b
 * able to reconstruct the filename for error messages.  We actually
Packit ae235b
 * pass the filename to operate on through the top node of the list.
Packit ae235b
 *
Packit ae235b
 * In case we're using openat(), this top filename will be a basename
Packit ae235b
 * which should be opened in the directory which has also had its fd
Packit ae235b
 * passed along.  If we're not using openat() then it will be a full
Packit ae235b
 * absolute filename.
Packit ae235b
 */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_measure_size_error (GFileMeasureFlags   flags,
Packit ae235b
                                 gint                saved_errno,
Packit ae235b
                                 GSList             *name,
Packit ae235b
                                 GError            **error)
Packit ae235b
{
Packit ae235b
  /* Only report an error if we were at the toplevel or if the caller
Packit ae235b
   * requested reporting of all errors.
Packit ae235b
   */
Packit ae235b
  if ((name->next == NULL) || (flags & G_FILE_MEASURE_REPORT_ANY_ERROR))
Packit ae235b
    {
Packit ae235b
      GString *filename;
Packit ae235b
      GSList *node;
Packit ae235b
Packit ae235b
      /* Skip some work if there is no error return */
Packit ae235b
      if (!error)
Packit ae235b
        return FALSE;
Packit ae235b
Packit ae235b
#ifdef AT_FDCWD
Packit ae235b
      /* If using openat() we need to rebuild the filename for the message */
Packit ae235b
      filename = g_string_new (name->data);
Packit ae235b
      for (node = name->next; node; node = node->next)
Packit ae235b
        {
Packit ae235b
          gchar *utf8;
Packit ae235b
Packit ae235b
          g_string_prepend_c (filename, G_DIR_SEPARATOR);
Packit ae235b
          utf8 = g_filename_display_name (node->data);
Packit ae235b
          g_string_prepend (filename, utf8);
Packit ae235b
          g_free (utf8);
Packit ae235b
        }
Packit ae235b
#else
Packit ae235b
      {
Packit ae235b
        gchar *utf8;
Packit ae235b
Packit ae235b
        /* Otherwise, we already have it, so just use it. */
Packit ae235b
        node = name;
Packit ae235b
        filename = g_string_new (NULL);
Packit ae235b
        utf8 = g_filename_display_name (node->data);
Packit ae235b
        g_string_append (filename, utf8);
Packit ae235b
        g_free (utf8);
Packit ae235b
      }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
Packit ae235b
                   _("Could not determine the disk usage of %s: %s"),
Packit ae235b
                   filename->str, g_strerror (saved_errno));
Packit ae235b
Packit ae235b
      g_string_free (filename, TRUE);
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else
Packit ae235b
    /* We're not reporting this error... */
Packit ae235b
    return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GFileMeasureFlags  flags;
Packit ae235b
  dev_t              contained_on;
Packit ae235b
  GCancellable      *cancellable;
Packit ae235b
Packit ae235b
  GFileMeasureProgressCallback progress_callback;
Packit ae235b
  gpointer                     progress_data;
Packit ae235b
Packit ae235b
  guint64 disk_usage;
Packit ae235b
  guint64 num_dirs;
Packit ae235b
  guint64 num_files;
Packit ae235b
Packit ae235b
  guint64 last_progress_report;
Packit ae235b
} MeasureState;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_measure_size_of_contents (gint           fd,
Packit ae235b
                                       GSList        *dir_name,
Packit ae235b
                                       MeasureState  *state,
Packit ae235b
                                       GError       **error);
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_measure_size_of_file (gint           parent_fd,
Packit ae235b
                                   GSList        *name,
Packit ae235b
                                   MeasureState  *state,
Packit ae235b
                                   GError       **error)
Packit ae235b
{
Packit ae235b
  GLocalFileStat buf;
Packit ae235b
Packit ae235b
  if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
#if defined (AT_FDCWD)
Packit ae235b
  if (fstatat (parent_fd, name->data, &buf, AT_SYMLINK_NOFOLLOW) != 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      return g_local_file_measure_size_error (state->flags, errsv, name, error);
Packit ae235b
    }
Packit ae235b
#elif defined (HAVE_LSTAT) || !defined (G_OS_WIN32)
Packit ae235b
  if (g_lstat (name->data, &buf) != 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      return g_local_file_measure_size_error (state->flags, errsv, name, error);
Packit ae235b
    }
Packit ae235b
#else /* !AT_FDCWD && !HAVE_LSTAT && G_OS_WIN32 */
Packit ae235b
  if (GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (name->data, &buf) != 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      return g_local_file_measure_size_error (state->flags, errsv, name, error);
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (name->next)
Packit ae235b
    {
Packit ae235b
      /* If not at the toplevel, check for a device boundary. */
Packit ae235b
Packit ae235b
      if (state->flags & G_FILE_MEASURE_NO_XDEV)
Packit ae235b
        if (state->contained_on != buf.st_dev)
Packit ae235b
          return TRUE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* If, however, this is the toplevel, set the device number so
Packit ae235b
       * that recursive invocations can compare against it.
Packit ae235b
       */
Packit ae235b
      state->contained_on = buf.st_dev;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#if defined (G_OS_WIN32)
Packit ae235b
  if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
Packit ae235b
    state->disk_usage += buf.allocated_size;
Packit ae235b
  else
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_BLOCKS)
Packit ae235b
  if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
Packit ae235b
    state->disk_usage += buf.st_blocks * G_GUINT64_CONSTANT (512);
Packit ae235b
  else
Packit ae235b
#endif
Packit ae235b
    state->disk_usage += buf.st_size;
Packit ae235b
Packit ae235b
  if (S_ISDIR (buf.st_mode))
Packit ae235b
    state->num_dirs++;
Packit ae235b
  else
Packit ae235b
    state->num_files++;
Packit ae235b
Packit ae235b
  if (state->progress_callback)
Packit ae235b
    {
Packit ae235b
      /* We could attempt to do some cleverness here in order to avoid
Packit ae235b
       * calling clock_gettime() so much, but we're doing stats and opens
Packit ae235b
       * all over the place already...
Packit ae235b
       */
Packit ae235b
      if (state->last_progress_report)
Packit ae235b
        {
Packit ae235b
          guint64 now;
Packit ae235b
Packit ae235b
          now = g_get_monotonic_time ();
Packit ae235b
Packit ae235b
          if (state->last_progress_report + 200 * G_TIME_SPAN_MILLISECOND < now)
Packit ae235b
            {
Packit ae235b
              (* state->progress_callback) (TRUE,
Packit ae235b
                                            state->disk_usage, state->num_dirs, state->num_files,
Packit ae235b
                                            state->progress_data);
Packit ae235b
              state->last_progress_report = now;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* We must do an initial report to inform that more reports
Packit ae235b
           * will be coming.
Packit ae235b
           */
Packit ae235b
          (* state->progress_callback) (TRUE, 0, 0, 0, state->progress_data);
Packit ae235b
          state->last_progress_report = g_get_monotonic_time ();
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (S_ISDIR (buf.st_mode))
Packit ae235b
    {
Packit ae235b
      int dir_fd = -1;
Packit ae235b
      int errsv;
Packit ae235b
Packit ae235b
      if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
Packit ae235b
        return FALSE;
Packit ae235b
Packit ae235b
#ifdef AT_FDCWD
Packit ae235b
#ifdef HAVE_OPEN_O_DIRECTORY
Packit ae235b
      dir_fd = openat (parent_fd, name->data, O_RDONLY|O_DIRECTORY);
Packit ae235b
#else
Packit ae235b
      dir_fd = openat (parent_fd, name->data, O_RDONLY);
Packit ae235b
#endif
Packit ae235b
      errsv = errno;
Packit ae235b
      if (dir_fd < 0)
Packit ae235b
        return g_local_file_measure_size_error (state->flags, errsv, name, error);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      if (!g_local_file_measure_size_of_contents (dir_fd, name, state, error))
Packit ae235b
        return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_measure_size_of_contents (gint           fd,
Packit ae235b
                                       GSList        *dir_name,
Packit ae235b
                                       MeasureState  *state,
Packit ae235b
                                       GError       **error)
Packit ae235b
{
Packit ae235b
  gboolean success = TRUE;
Packit ae235b
  const gchar *name;
Packit ae235b
  GDir *dir;
Packit ae235b
Packit ae235b
#ifdef AT_FDCWD
Packit ae235b
  {
Packit ae235b
    /* If this fails, we want to preserve the errno from fopendir() */
Packit ae235b
    DIR *dirp;
Packit ae235b
    dirp = fdopendir (fd);
Packit ae235b
    dir = dirp ? GLIB_PRIVATE_CALL(g_dir_new_from_dirp) (dirp) : NULL;
Packit ae235b
  }
Packit ae235b
#else
Packit ae235b
  dir = GLIB_PRIVATE_CALL(g_dir_open_with_errno) (dir_name->data, 0);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (dir == NULL)
Packit ae235b
    {
Packit ae235b
      gint saved_errno = errno;
Packit ae235b
Packit ae235b
#ifdef AT_FDCWD
Packit ae235b
      close (fd);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      return g_local_file_measure_size_error (state->flags, saved_errno, dir_name, error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  while (success && (name = g_dir_read_name (dir)))
Packit ae235b
    {
Packit ae235b
      GSList node;
Packit ae235b
Packit ae235b
      node.next = dir_name;
Packit ae235b
#ifdef AT_FDCWD
Packit ae235b
      node.data = (gchar *) name;
Packit ae235b
#else
Packit ae235b
      node.data = g_build_filename (dir_name->data, name, NULL);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      success = g_local_file_measure_size_of_file (fd, &node, state, error);
Packit ae235b
Packit ae235b
#ifndef AT_FDCWD
Packit ae235b
      g_free (node.data);
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_dir_close (dir);
Packit ae235b
Packit ae235b
  return success;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_local_file_measure_disk_usage (GFile                         *file,
Packit ae235b
                                 GFileMeasureFlags              flags,
Packit ae235b
                                 GCancellable                  *cancellable,
Packit ae235b
                                 GFileMeasureProgressCallback   progress_callback,
Packit ae235b
                                 gpointer                       progress_data,
Packit ae235b
                                 guint64                       *disk_usage,
Packit ae235b
                                 guint64                       *num_dirs,
Packit ae235b
                                 guint64                       *num_files,
Packit ae235b
                                 GError                       **error)
Packit ae235b
{
Packit ae235b
  GLocalFile *local_file = G_LOCAL_FILE (file);
Packit ae235b
  MeasureState state = { 0, };
Packit ae235b
  gint root_fd = -1;
Packit ae235b
  GSList node;
Packit ae235b
Packit ae235b
  state.flags = flags;
Packit ae235b
  state.cancellable = cancellable;
Packit ae235b
  state.progress_callback = progress_callback;
Packit ae235b
  state.progress_data = progress_data;
Packit ae235b
Packit ae235b
#ifdef AT_FDCWD
Packit ae235b
  root_fd = AT_FDCWD;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  node.data = local_file->filename;
Packit ae235b
  node.next = NULL;
Packit ae235b
Packit ae235b
  if (!g_local_file_measure_size_of_file (root_fd, &node, &state, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (disk_usage)
Packit ae235b
    *disk_usage = state.disk_usage;
Packit ae235b
Packit ae235b
  if (num_dirs)
Packit ae235b
    *num_dirs = state.num_dirs;
Packit ae235b
Packit ae235b
  if (num_files)
Packit ae235b
    *num_files = state.num_files;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_local_file_file_iface_init (GFileIface *iface)
Packit ae235b
{
Packit ae235b
  iface->dup = g_local_file_dup;
Packit ae235b
  iface->hash = g_local_file_hash;
Packit ae235b
  iface->equal = g_local_file_equal;
Packit ae235b
  iface->is_native = g_local_file_is_native;
Packit ae235b
  iface->has_uri_scheme = g_local_file_has_uri_scheme;
Packit ae235b
  iface->get_uri_scheme = g_local_file_get_uri_scheme;
Packit ae235b
  iface->get_basename = g_local_file_get_basename;
Packit ae235b
  iface->get_path = g_local_file_get_path;
Packit ae235b
  iface->get_uri = g_local_file_get_uri;
Packit ae235b
  iface->get_parse_name = g_local_file_get_parse_name;
Packit ae235b
  iface->get_parent = g_local_file_get_parent;
Packit ae235b
  iface->prefix_matches = g_local_file_prefix_matches;
Packit ae235b
  iface->get_relative_path = g_local_file_get_relative_path;
Packit ae235b
  iface->resolve_relative_path = g_local_file_resolve_relative_path;
Packit ae235b
  iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
Packit ae235b
  iface->set_display_name = g_local_file_set_display_name;
Packit ae235b
  iface->enumerate_children = g_local_file_enumerate_children;
Packit ae235b
  iface->query_info = g_local_file_query_info;
Packit ae235b
  iface->query_filesystem_info = g_local_file_query_filesystem_info;
Packit ae235b
  iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
Packit ae235b
  iface->query_settable_attributes = g_local_file_query_settable_attributes;
Packit ae235b
  iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
Packit ae235b
  iface->set_attribute = g_local_file_set_attribute;
Packit ae235b
  iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
Packit ae235b
  iface->read_fn = g_local_file_read;
Packit ae235b
  iface->append_to = g_local_file_append_to;
Packit ae235b
  iface->create = g_local_file_create;
Packit ae235b
  iface->replace = g_local_file_replace;
Packit ae235b
  iface->open_readwrite = g_local_file_open_readwrite;
Packit ae235b
  iface->create_readwrite = g_local_file_create_readwrite;
Packit ae235b
  iface->replace_readwrite = g_local_file_replace_readwrite;
Packit ae235b
  iface->delete_file = g_local_file_delete;
Packit ae235b
  iface->trash = g_local_file_trash;
Packit ae235b
  iface->make_directory = g_local_file_make_directory;
Packit ae235b
  iface->make_symbolic_link = g_local_file_make_symbolic_link;
Packit ae235b
  iface->copy = g_local_file_copy;
Packit ae235b
  iface->move = g_local_file_move;
Packit ae235b
  iface->monitor_dir = g_local_file_monitor_dir;
Packit ae235b
  iface->monitor_file = g_local_file_monitor_file;
Packit ae235b
  iface->measure_disk_usage = g_local_file_measure_disk_usage;
Packit ae235b
Packit ae235b
  iface->supports_thread_contexts = TRUE;
Packit ae235b
}