Blame gio/glocalfileinfo.c

Packit ae235b
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
Packit ae235b
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 <glib.h>
Packit ae235b
Packit ae235b
#ifdef HAVE_SYS_TIME_H
Packit ae235b
#include <sys/time.h>
Packit ae235b
#endif
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <grp.h>
Packit ae235b
#include <pwd.h>
Packit ae235b
#endif
Packit ae235b
#ifdef HAVE_SELINUX
Packit ae235b
#include <selinux/selinux.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
Packit ae235b
#if defined HAVE_SYS_XATTR_H
Packit ae235b
  #include <sys/xattr.h>
Packit ae235b
#elif defined HAVE_ATTR_XATTR_H
Packit ae235b
  #include <attr/xattr.h>
Packit ae235b
#else
Packit ae235b
  #error "Neither <sys/xattr.h> nor <attr/xattr.h> is present but extended attribute support is enabled."
Packit ae235b
#endif /* defined HAVE_SYS_XATTR_H || HAVE_ATTR_XATTR_H */
Packit ae235b
Packit ae235b
#endif /* HAVE_XATTR */
Packit ae235b
Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
#include <glib/gstdioprivate.h>
Packit ae235b
#include <gfileattribute-priv.h>
Packit ae235b
#include <gfileinfo-priv.h>
Packit ae235b
#include <gvfs.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#include "glib-unix.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glib-private.h"
Packit ae235b
Packit ae235b
#include "thumbnail-verify.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#include <io.h>
Packit ae235b
#ifndef W_OK
Packit ae235b
#define W_OK 2
Packit ae235b
#endif
Packit ae235b
#ifndef R_OK
Packit ae235b
#define R_OK 4
Packit ae235b
#endif
Packit ae235b
#ifndef X_OK
Packit ae235b
#define X_OK 0 /* not really */
Packit ae235b
#endif
Packit ae235b
#ifndef S_ISREG
Packit ae235b
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
Packit ae235b
#endif
Packit ae235b
#ifndef S_ISDIR
Packit ae235b
#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
Packit ae235b
#endif
Packit ae235b
#ifndef S_IXUSR
Packit ae235b
#define S_IXUSR _S_IEXEC
Packit ae235b
#endif
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glocalfileinfo.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gthemedicon.h"
Packit ae235b
#include "gcontenttypeprivate.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
struct ThumbMD5Context {
Packit ae235b
	guint32 buf[4];
Packit ae235b
	guint32 bits[2];
Packit ae235b
	unsigned char in[64];
Packit ae235b
};
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  char *user_name;
Packit ae235b
  char *real_name;
Packit ae235b
} UidData;
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (uid_cache);
Packit ae235b
static GHashTable *uid_cache = NULL;
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (gid_cache);
Packit ae235b
static GHashTable *gid_cache = NULL;
Packit ae235b
Packit ae235b
#endif  /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
char *
Packit ae235b
_g_local_file_info_create_etag (GLocalFileStat *statbuf)
Packit ae235b
{
Packit ae235b
  glong sec, usec;
Packit ae235b
Packit ae235b
  sec = statbuf->st_mtime;
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
Packit ae235b
  usec = statbuf->st_mtimensec / 1000;
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
Packit ae235b
  usec = statbuf->st_mtim.tv_nsec / 1000;
Packit ae235b
#else
Packit ae235b
  usec = 0;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return g_strdup_printf ("%lu:%lu", sec, usec);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
_g_local_file_info_create_file_id (GLocalFileStat *statbuf)
Packit ae235b
{
Packit ae235b
  guint64 ino;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  ino = statbuf->file_index;
Packit ae235b
#else
Packit ae235b
  ino = statbuf->st_ino;
Packit ae235b
#endif
Packit ae235b
  return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
Packit ae235b
			  (guint64) statbuf->st_dev, 
Packit ae235b
			  ino);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
_g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
Packit ae235b
{
Packit ae235b
  return g_strdup_printf ("l%" G_GUINT64_FORMAT,
Packit ae235b
			  (guint64) statbuf->st_dev);
Packit ae235b
}
Packit ae235b
Packit ae235b
#if defined (S_ISLNK) || defined (G_OS_WIN32)
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
read_link (const gchar *full_name)
Packit ae235b
{
Packit ae235b
#if defined (HAVE_READLINK) || defined (G_OS_WIN32)
Packit ae235b
  gchar *buffer;
Packit ae235b
  guint size;
Packit ae235b
  
Packit ae235b
  size = 256;
Packit ae235b
  buffer = g_malloc (size);
Packit ae235b
  
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      int read_size;
Packit ae235b
      
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
      read_size = readlink (full_name, buffer, size);
Packit ae235b
#else
Packit ae235b
      read_size = GLIB_PRIVATE_CALL (g_win32_readlink_utf8) (full_name, buffer, size);
Packit ae235b
#endif
Packit ae235b
      if (read_size < 0)
Packit ae235b
	{
Packit ae235b
	  g_free (buffer);
Packit ae235b
	  return NULL;
Packit ae235b
	}
Packit ae235b
      if (read_size < size)
Packit ae235b
	{
Packit ae235b
	  buffer[read_size] = 0;
Packit ae235b
	  return buffer;
Packit ae235b
	}
Packit ae235b
      size *= 2;
Packit ae235b
      buffer = g_realloc (buffer, size);
Packit ae235b
    }
Packit ae235b
#else
Packit ae235b
  return NULL;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif  /* S_ISLNK || G_OS_WIN32 */
Packit ae235b
Packit ae235b
#ifdef HAVE_SELINUX
Packit ae235b
/* Get the SELinux security context */
Packit ae235b
static void
Packit ae235b
get_selinux_context (const char            *path,
Packit ae235b
		     GFileInfo             *info,
Packit ae235b
		     GFileAttributeMatcher *attribute_matcher,
Packit ae235b
		     gboolean               follow_symlinks)
Packit ae235b
{
Packit ae235b
  char *context;
Packit ae235b
Packit ae235b
  if (!_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT))
Packit ae235b
    return;
Packit ae235b
  
Packit ae235b
  if (is_selinux_enabled ())
Packit ae235b
    {
Packit ae235b
      if (follow_symlinks)
Packit ae235b
	{
Packit ae235b
	  if (lgetfilecon_raw (path, &context) < 0)
Packit ae235b
	    return;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  if (getfilecon_raw (path, &context) < 0)
Packit ae235b
	    return;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (context)
Packit ae235b
	{
Packit ae235b
	  _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
Packit ae235b
	  freecon (context);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
Packit ae235b
/* Wrappers to hide away differences between (Linux) getxattr/lgetxattr and
Packit ae235b
 * (Mac) getxattr(..., XATTR_NOFOLLOW)
Packit ae235b
 */
Packit ae235b
#ifdef HAVE_XATTR_NOFOLLOW
Packit ae235b
#define g_fgetxattr(fd,name,value,size)  fgetxattr(fd,name,value,size,0,0)
Packit ae235b
#define g_flistxattr(fd,name,size)       flistxattr(fd,name,size,0)
Packit ae235b
#define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0,0)
Packit ae235b
#else
Packit ae235b
#define g_fgetxattr     fgetxattr
Packit ae235b
#define g_flistxattr    flistxattr
Packit ae235b
#define g_setxattr(path,name,value,size) setxattr(path,name,value,size,0)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_getxattr (const char *path, const char *name, void *value, size_t size,
Packit ae235b
            gboolean follow_symlinks)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_XATTR_NOFOLLOW
Packit ae235b
  return getxattr (path, name, value, size, 0, follow_symlinks ? 0 : XATTR_NOFOLLOW);
Packit ae235b
#else
Packit ae235b
  if (follow_symlinks)
Packit ae235b
    return getxattr (path, name, value, size);
Packit ae235b
  else
Packit ae235b
    return lgetxattr (path, name, value, size);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_listxattr(const char *path, char *namebuf, size_t size,
Packit ae235b
            gboolean follow_symlinks)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_XATTR_NOFOLLOW
Packit ae235b
  return listxattr (path, namebuf, size, follow_symlinks ? 0 : XATTR_NOFOLLOW);
Packit ae235b
#else
Packit ae235b
  if (follow_symlinks)
Packit ae235b
    return listxattr (path, namebuf, size);
Packit ae235b
  else
Packit ae235b
    return llistxattr (path, namebuf, size);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
valid_char (char c)
Packit ae235b
{
Packit ae235b
  return c >= 32 && c <= 126 && c != '\\';
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
name_is_valid (const char *str)
Packit ae235b
{
Packit ae235b
  while (*str)
Packit ae235b
    {
Packit ae235b
      if (!valid_char (*str++))
Packit ae235b
	return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
hex_escape_string (const char *str, 
Packit ae235b
                   gboolean   *free_return)
Packit ae235b
{
Packit ae235b
  int num_invalid, i;
Packit ae235b
  char *escaped_str, *p;
Packit ae235b
  unsigned char c;
Packit ae235b
  static char *hex_digits = "0123456789abcdef";
Packit ae235b
  int len;
Packit ae235b
Packit ae235b
  len = strlen (str);
Packit ae235b
  
Packit ae235b
  num_invalid = 0;
Packit ae235b
  for (i = 0; i < len; i++)
Packit ae235b
    {
Packit ae235b
      if (!valid_char (str[i]))
Packit ae235b
	num_invalid++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (num_invalid == 0)
Packit ae235b
    {
Packit ae235b
      *free_return = FALSE;
Packit ae235b
      return (char *)str;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  escaped_str = g_malloc (len + num_invalid*3 + 1);
Packit ae235b
Packit ae235b
  p = escaped_str;
Packit ae235b
  for (i = 0; i < len; i++)
Packit ae235b
    {
Packit ae235b
      if (valid_char (str[i]))
Packit ae235b
	*p++ = str[i];
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  c = str[i];
Packit ae235b
	  *p++ = '\\';
Packit ae235b
	  *p++ = 'x';
Packit ae235b
	  *p++ = hex_digits[(c >> 4) & 0xf];
Packit ae235b
	  *p++ = hex_digits[c & 0xf];
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  *p = 0;
Packit ae235b
Packit ae235b
  *free_return = TRUE;
Packit ae235b
  return escaped_str;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
hex_unescape_string (const char *str, 
Packit ae235b
                     int        *out_len, 
Packit ae235b
                     gboolean   *free_return)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  char *unescaped_str, *p;
Packit ae235b
  unsigned char c;
Packit ae235b
  int len;
Packit ae235b
Packit ae235b
  len = strlen (str);
Packit ae235b
  
Packit ae235b
  if (strchr (str, '\\') == NULL)
Packit ae235b
    {
Packit ae235b
      if (out_len)
Packit ae235b
	*out_len = len;
Packit ae235b
      *free_return = FALSE;
Packit ae235b
      return (char *)str;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  unescaped_str = g_malloc (len + 1);
Packit ae235b
Packit ae235b
  p = unescaped_str;
Packit ae235b
  for (i = 0; i < len; i++)
Packit ae235b
    {
Packit ae235b
      if (str[i] == '\\' &&
Packit ae235b
	  str[i+1] == 'x' &&
Packit ae235b
	  len - i >= 4)
Packit ae235b
	{
Packit ae235b
	  c =
Packit ae235b
	    (g_ascii_xdigit_value (str[i+2]) << 4) |
Packit ae235b
	    g_ascii_xdigit_value (str[i+3]);
Packit ae235b
	  *p++ = c;
Packit ae235b
	  i += 3;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	*p++ = str[i];
Packit ae235b
    }
Packit ae235b
  *p++ = 0;
Packit ae235b
Packit ae235b
  if (out_len)
Packit ae235b
    *out_len = p - unescaped_str;
Packit ae235b
  *free_return = TRUE;
Packit ae235b
  return unescaped_str;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
escape_xattr (GFileInfo  *info,
Packit ae235b
	      const char *gio_attr, /* gio attribute name */
Packit ae235b
	      const char *value, /* Is zero terminated */
Packit ae235b
	      size_t      len /* not including zero termination */)
Packit ae235b
{
Packit ae235b
  char *escaped_val;
Packit ae235b
  gboolean free_escaped_val;
Packit ae235b
  
Packit ae235b
  escaped_val = hex_escape_string (value, &free_escaped_val);
Packit ae235b
  
Packit ae235b
  g_file_info_set_attribute_string (info, gio_attr, escaped_val);
Packit ae235b
  
Packit ae235b
  if (free_escaped_val)
Packit ae235b
    g_free (escaped_val);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_one_xattr (const char *path,
Packit ae235b
	       GFileInfo  *info,
Packit ae235b
	       const char *gio_attr,
Packit ae235b
	       const char *xattr,
Packit ae235b
	       gboolean    follow_symlinks)
Packit ae235b
{
Packit ae235b
  char value[64];
Packit ae235b
  char *value_p;
Packit ae235b
  gssize len;
Packit ae235b
  int errsv;
Packit ae235b
Packit ae235b
  len = g_getxattr (path, xattr, value, sizeof (value)-1, follow_symlinks);
Packit ae235b
  errsv = errno;
Packit ae235b
Packit ae235b
  value_p = NULL;
Packit ae235b
  if (len >= 0)
Packit ae235b
    value_p = value;
Packit ae235b
  else if (len == -1 && errsv == ERANGE)
Packit ae235b
    {
Packit ae235b
      len = g_getxattr (path, xattr, NULL, 0, follow_symlinks);
Packit ae235b
Packit ae235b
      if (len < 0)
Packit ae235b
	return;
Packit ae235b
Packit ae235b
      value_p = g_malloc (len+1);
Packit ae235b
Packit ae235b
      len = g_getxattr (path, xattr, value_p, len, follow_symlinks);
Packit ae235b
Packit ae235b
      if (len < 0)
Packit ae235b
	{
Packit ae235b
	  g_free (value_p);
Packit ae235b
	  return;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return;
Packit ae235b
  
Packit ae235b
  /* Null terminate */
Packit ae235b
  value_p[len] = 0;
Packit ae235b
Packit ae235b
  escape_xattr (info, gio_attr, value_p, len);
Packit ae235b
  
Packit ae235b
  if (value_p != value)
Packit ae235b
    g_free (value_p);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* defined HAVE_XATTR */
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_xattrs (const char            *path,
Packit ae235b
	    gboolean               user,
Packit ae235b
	    GFileInfo             *info,
Packit ae235b
	    GFileAttributeMatcher *matcher,
Packit ae235b
	    gboolean               follow_symlinks)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
  gboolean all;
Packit ae235b
  gsize list_size;
Packit ae235b
  gssize list_res_size;
Packit ae235b
  size_t len;
Packit ae235b
  char *list;
Packit ae235b
  const char *attr, *attr2;
Packit ae235b
Packit ae235b
  if (user)
Packit ae235b
    all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
Packit ae235b
  else
Packit ae235b
    all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
Packit ae235b
Packit ae235b
  if (all)
Packit ae235b
    {
Packit ae235b
      int errsv;
Packit ae235b
Packit ae235b
      list_res_size = g_listxattr (path, NULL, 0, follow_symlinks);
Packit ae235b
Packit ae235b
      if (list_res_size == -1 ||
Packit ae235b
	  list_res_size == 0)
Packit ae235b
	return;
Packit ae235b
Packit ae235b
      list_size = list_res_size;
Packit ae235b
      list = g_malloc (list_size);
Packit ae235b
Packit ae235b
    retry:
Packit ae235b
      
Packit ae235b
      list_res_size = g_listxattr (path, list, list_size, follow_symlinks);
Packit ae235b
      errsv = errno;
Packit ae235b
      
Packit ae235b
      if (list_res_size == -1 && errsv == ERANGE)
Packit ae235b
	{
Packit ae235b
	  list_size = list_size * 2;
Packit ae235b
	  list = g_realloc (list, list_size);
Packit ae235b
	  goto retry;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (list_res_size == -1)
Packit ae235b
	return;
Packit ae235b
Packit ae235b
      attr = list;
Packit ae235b
      while (list_res_size > 0)
Packit ae235b
	{
Packit ae235b
	  if ((user && g_str_has_prefix (attr, "user.")) ||
Packit ae235b
	      (!user && !g_str_has_prefix (attr, "user.")))
Packit ae235b
	    {
Packit ae235b
	      char *escaped_attr, *gio_attr;
Packit ae235b
	      gboolean free_escaped_attr;
Packit ae235b
	      
Packit ae235b
	      if (user)
Packit ae235b
		{
Packit ae235b
		  escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
Packit ae235b
		  gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
Packit ae235b
		}
Packit ae235b
	      else
Packit ae235b
		{
Packit ae235b
		  escaped_attr = hex_escape_string (attr, &free_escaped_attr);
Packit ae235b
		  gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
Packit ae235b
		}
Packit ae235b
	      
Packit ae235b
	      if (free_escaped_attr)
Packit ae235b
		g_free (escaped_attr);
Packit ae235b
	      
Packit ae235b
	      get_one_xattr (path, info, gio_attr, attr, follow_symlinks);
Packit ae235b
Packit ae235b
	      g_free (gio_attr);
Packit ae235b
	    }
Packit ae235b
	      
Packit ae235b
	  len = strlen (attr) + 1;
Packit ae235b
	  attr += len;
Packit ae235b
	  list_res_size -= len;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_free (list);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
Packit ae235b
	{
Packit ae235b
	  char *unescaped_attribute, *a;
Packit ae235b
	  gboolean free_unescaped_attribute;
Packit ae235b
Packit ae235b
	  attr2 = strchr (attr, ':');
Packit ae235b
	  if (attr2)
Packit ae235b
	    {
Packit ae235b
	      attr2 += 2; /* Skip '::' */
Packit ae235b
	      unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
Packit ae235b
	      if (user)
Packit ae235b
		a = g_strconcat ("user.", unescaped_attribute, NULL);
Packit ae235b
	      else
Packit ae235b
		a = unescaped_attribute;
Packit ae235b
	      
Packit ae235b
	      get_one_xattr (path, info, attr, a, follow_symlinks);
Packit ae235b
Packit ae235b
	      if (user)
Packit ae235b
		g_free (a);
Packit ae235b
	      
Packit ae235b
	      if (free_unescaped_attribute)
Packit ae235b
		g_free (unescaped_attribute);
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
#endif /* defined HAVE_XATTR */
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
static void
Packit ae235b
get_one_xattr_from_fd (int         fd,
Packit ae235b
		       GFileInfo  *info,
Packit ae235b
		       const char *gio_attr,
Packit ae235b
		       const char *xattr)
Packit ae235b
{
Packit ae235b
  char value[64];
Packit ae235b
  char *value_p;
Packit ae235b
  gssize len;
Packit ae235b
  int errsv;
Packit ae235b
Packit ae235b
  len = g_fgetxattr (fd, xattr, value, sizeof (value) - 1);
Packit ae235b
  errsv = errno;
Packit ae235b
Packit ae235b
  value_p = NULL;
Packit ae235b
  if (len >= 0)
Packit ae235b
    value_p = value;
Packit ae235b
  else if (len == -1 && errsv == ERANGE)
Packit ae235b
    {
Packit ae235b
      len = g_fgetxattr (fd, xattr, NULL, 0);
Packit ae235b
Packit ae235b
      if (len < 0)
Packit ae235b
	return;
Packit ae235b
Packit ae235b
      value_p = g_malloc (len + 1);
Packit ae235b
Packit ae235b
      len = g_fgetxattr (fd, xattr, value_p, len);
Packit ae235b
Packit ae235b
      if (len < 0)
Packit ae235b
	{
Packit ae235b
	  g_free (value_p);
Packit ae235b
	  return;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return;
Packit ae235b
  
Packit ae235b
  /* Null terminate */
Packit ae235b
  value_p[len] = 0;
Packit ae235b
Packit ae235b
  escape_xattr (info, gio_attr, value_p, len);
Packit ae235b
  
Packit ae235b
  if (value_p != value)
Packit ae235b
    g_free (value_p);
Packit ae235b
}
Packit ae235b
#endif /* defined HAVE_XATTR */
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_xattrs_from_fd (int                    fd,
Packit ae235b
		    gboolean               user,
Packit ae235b
		    GFileInfo             *info,
Packit ae235b
		    GFileAttributeMatcher *matcher)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
  gboolean all;
Packit ae235b
  gsize list_size;
Packit ae235b
  gssize list_res_size;
Packit ae235b
  size_t len;
Packit ae235b
  char *list;
Packit ae235b
  const char *attr, *attr2;
Packit ae235b
Packit ae235b
  if (user)
Packit ae235b
    all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr");
Packit ae235b
  else
Packit ae235b
    all = g_file_attribute_matcher_enumerate_namespace (matcher, "xattr-sys");
Packit ae235b
Packit ae235b
  if (all)
Packit ae235b
    {
Packit ae235b
      int errsv;
Packit ae235b
Packit ae235b
      list_res_size = g_flistxattr (fd, NULL, 0);
Packit ae235b
Packit ae235b
      if (list_res_size == -1 ||
Packit ae235b
	  list_res_size == 0)
Packit ae235b
	return;
Packit ae235b
Packit ae235b
      list_size = list_res_size;
Packit ae235b
      list = g_malloc (list_size);
Packit ae235b
Packit ae235b
    retry:
Packit ae235b
      
Packit ae235b
      list_res_size = g_flistxattr (fd, list, list_size);
Packit ae235b
      errsv = errno;
Packit ae235b
      
Packit ae235b
      if (list_res_size == -1 && errsv == ERANGE)
Packit ae235b
	{
Packit ae235b
	  list_size = list_size * 2;
Packit ae235b
	  list = g_realloc (list, list_size);
Packit ae235b
	  goto retry;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (list_res_size == -1)
Packit ae235b
        {
Packit ae235b
          g_free (list);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      attr = list;
Packit ae235b
      while (list_res_size > 0)
Packit ae235b
	{
Packit ae235b
	  if ((user && g_str_has_prefix (attr, "user.")) ||
Packit ae235b
	      (!user && !g_str_has_prefix (attr, "user.")))
Packit ae235b
	    {
Packit ae235b
	      char *escaped_attr, *gio_attr;
Packit ae235b
	      gboolean free_escaped_attr;
Packit ae235b
	      
Packit ae235b
	      if (user)
Packit ae235b
		{
Packit ae235b
		  escaped_attr = hex_escape_string (attr + 5, &free_escaped_attr);
Packit ae235b
		  gio_attr = g_strconcat ("xattr::", escaped_attr, NULL);
Packit ae235b
		}
Packit ae235b
	      else
Packit ae235b
		{
Packit ae235b
		  escaped_attr = hex_escape_string (attr, &free_escaped_attr);
Packit ae235b
		  gio_attr = g_strconcat ("xattr-sys::", escaped_attr, NULL);
Packit ae235b
		}
Packit ae235b
	      
Packit ae235b
	      if (free_escaped_attr)
Packit ae235b
		g_free (escaped_attr);
Packit ae235b
	      
Packit ae235b
	      get_one_xattr_from_fd (fd, info, gio_attr, attr);
Packit ae235b
	      g_free (gio_attr);
Packit ae235b
	    }
Packit ae235b
	  
Packit ae235b
	  len = strlen (attr) + 1;
Packit ae235b
	  attr += len;
Packit ae235b
	  list_res_size -= len;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      g_free (list);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      while ((attr = g_file_attribute_matcher_enumerate_next (matcher)) != NULL)
Packit ae235b
	{
Packit ae235b
	  char *unescaped_attribute, *a;
Packit ae235b
	  gboolean free_unescaped_attribute;
Packit ae235b
Packit ae235b
	  attr2 = strchr (attr, ':');
Packit ae235b
	  if (attr2)
Packit ae235b
	    {
Packit ae235b
	      attr2++; /* Skip ':' */
Packit ae235b
	      unescaped_attribute = hex_unescape_string (attr2, NULL, &free_unescaped_attribute);
Packit ae235b
	      if (user)
Packit ae235b
		a = g_strconcat ("user.", unescaped_attribute, NULL);
Packit ae235b
	      else
Packit ae235b
		a = unescaped_attribute;
Packit ae235b
	      
Packit ae235b
	      get_one_xattr_from_fd (fd, info, attr, a);
Packit ae235b
Packit ae235b
	      if (user)
Packit ae235b
		g_free (a);
Packit ae235b
	      
Packit ae235b
	      if (free_unescaped_attribute)
Packit ae235b
		g_free (unescaped_attribute);
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
#endif /* defined HAVE_XATTR */
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
static gboolean
Packit ae235b
set_xattr (char                       *filename,
Packit ae235b
	   const char                 *escaped_attribute,
Packit ae235b
	   const GFileAttributeValue  *attr_value,
Packit ae235b
	   GError                    **error)
Packit ae235b
{
Packit ae235b
  char *attribute, *value;
Packit ae235b
  gboolean free_attribute, free_value;
Packit ae235b
  int val_len, res, errsv;
Packit ae235b
  gboolean is_user;
Packit ae235b
  char *a;
Packit ae235b
Packit ae235b
  if (attr_value == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Attribute value must be non-NULL"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (attr_value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Invalid attribute type (string expected)"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!name_is_valid (escaped_attribute))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Invalid extended attribute name"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_str_has_prefix (escaped_attribute, "xattr::"))
Packit ae235b
    {
Packit ae235b
      escaped_attribute += strlen ("xattr::");
Packit ae235b
      is_user = TRUE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_warn_if_fail (g_str_has_prefix (escaped_attribute, "xattr-sys::"));
Packit ae235b
      escaped_attribute += strlen ("xattr-sys::");
Packit ae235b
      is_user = FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  attribute = hex_unescape_string (escaped_attribute, NULL, &free_attribute);
Packit ae235b
  value = hex_unescape_string (attr_value->u.string, &val_len, &free_value);
Packit ae235b
Packit ae235b
  if (is_user)
Packit ae235b
    a = g_strconcat ("user.", attribute, NULL);
Packit ae235b
  else
Packit ae235b
    a = attribute;
Packit ae235b
  
Packit ae235b
  res = g_setxattr (filename, a, value, val_len);
Packit ae235b
  errsv = errno;
Packit ae235b
  
Packit ae235b
  if (is_user)
Packit ae235b
    g_free (a);
Packit ae235b
  
Packit ae235b
  if (free_attribute)
Packit ae235b
    g_free (attribute);
Packit ae235b
  
Packit ae235b
  if (free_value)
Packit ae235b
    g_free (value);
Packit ae235b
Packit ae235b
  if (res == -1)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
		   g_io_error_from_errno (errsv),
Packit ae235b
		   _("Error setting extended attribute “%s”: %s"),
Packit ae235b
		   escaped_attribute, g_strerror (errsv));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_local_file_info_get_parent_info (const char            *dir,
Packit ae235b
				    GFileAttributeMatcher *attribute_matcher,
Packit ae235b
				    GLocalParentFileInfo  *parent_info)
Packit ae235b
{
Packit ae235b
  GStatBuf statbuf;
Packit ae235b
  int res;
Packit ae235b
Packit ae235b
  parent_info->extra_data = NULL;
Packit ae235b
  parent_info->free_extra_data = NULL;
Packit ae235b
  parent_info->writable = FALSE;
Packit ae235b
  parent_info->is_sticky = FALSE;
Packit ae235b
  parent_info->has_trash_dir = FALSE;
Packit ae235b
  parent_info->device = 0;
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME) ||
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE) ||
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH) ||
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT))
Packit ae235b
    {
Packit ae235b
      /* FIXME: Windows: The underlying _waccess() call in the C
Packit ae235b
       * library is mostly pointless as it only looks at the READONLY
Packit ae235b
       * FAT-style attribute of the file, it doesn't check the ACL at
Packit ae235b
       * all.
Packit ae235b
       */
Packit ae235b
      parent_info->writable = (g_access (dir, W_OK) == 0);
Packit ae235b
      
Packit ae235b
      res = g_stat (dir, &statbuf);
Packit ae235b
Packit ae235b
      /*
Packit ae235b
       * The sticky bit (S_ISVTX) on a directory means that a file in that directory can be
Packit ae235b
       * renamed or deleted only by the owner of the file, by the owner of the directory, and
Packit ae235b
       * by a privileged process.
Packit ae235b
       */
Packit ae235b
      if (res == 0)
Packit ae235b
	{
Packit ae235b
#ifdef S_ISVTX
Packit ae235b
	  parent_info->is_sticky = (statbuf.st_mode & S_ISVTX) != 0;
Packit ae235b
#else
Packit ae235b
	  parent_info->is_sticky = FALSE;
Packit ae235b
#endif
Packit ae235b
	  parent_info->owner = statbuf.st_uid;
Packit ae235b
	  parent_info->device = statbuf.st_dev;
Packit ae235b
          /* No need to find trash dir if it's not writable anyway */
Packit ae235b
          if (parent_info->writable &&
Packit ae235b
              _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
Packit ae235b
            parent_info->has_trash_dir = _g_local_file_has_trash_dir (dir, statbuf.st_dev);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info)
Packit ae235b
{
Packit ae235b
  if (parent_info->extra_data &&
Packit ae235b
      parent_info->free_extra_data)
Packit ae235b
    parent_info->free_extra_data (parent_info->extra_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_access_rights (GFileAttributeMatcher *attribute_matcher,
Packit ae235b
		   GFileInfo             *info,
Packit ae235b
		   const gchar           *path,
Packit ae235b
		   GLocalFileStat        *statbuf,
Packit ae235b
		   GLocalParentFileInfo  *parent_info)
Packit ae235b
{
Packit ae235b
  /* FIXME: Windows: The underlyin _waccess() is mostly pointless */
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ))
Packit ae235b
    _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_READ,
Packit ae235b
				             g_access (path, R_OK) == 0);
Packit ae235b
  
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE))
Packit ae235b
    _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_WRITE,
Packit ae235b
				             g_access (path, W_OK) == 0);
Packit ae235b
  
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE))
Packit ae235b
    _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_EXECUTE,
Packit ae235b
				             g_access (path, X_OK) == 0);
Packit ae235b
Packit ae235b
Packit ae235b
  if (parent_info)
Packit ae235b
    {
Packit ae235b
      gboolean writable;
Packit ae235b
Packit ae235b
      writable = FALSE;
Packit ae235b
      if (parent_info->writable)
Packit ae235b
	{
Packit ae235b
	  if (parent_info->is_sticky)
Packit ae235b
	    {
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
	      uid_t uid = geteuid ();
Packit ae235b
Packit ae235b
	      if (uid == statbuf->st_uid ||
Packit ae235b
		  uid == parent_info->owner ||
Packit ae235b
		  uid == 0)
Packit ae235b
#endif
Packit ae235b
		writable = TRUE;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    writable = TRUE;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME))
Packit ae235b
	_g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_RENAME,
Packit ae235b
					         writable);
Packit ae235b
      
Packit ae235b
      if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE))
Packit ae235b
	_g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_DELETE,
Packit ae235b
					         writable);
Packit ae235b
Packit ae235b
      if (_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH))
Packit ae235b
        _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_ACCESS_CAN_TRASH,
Packit ae235b
                                                 writable && parent_info->has_trash_dir);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
set_info_from_stat (GFileInfo             *info, 
Packit ae235b
                    GLocalFileStat        *statbuf,
Packit ae235b
		    GFileAttributeMatcher *attribute_matcher)
Packit ae235b
{
Packit ae235b
  GFileType file_type;
Packit ae235b
Packit ae235b
  file_type = G_FILE_TYPE_UNKNOWN;
Packit ae235b
Packit ae235b
  if (S_ISREG (statbuf->st_mode))
Packit ae235b
    file_type = G_FILE_TYPE_REGULAR;
Packit ae235b
  else if (S_ISDIR (statbuf->st_mode))
Packit ae235b
    file_type = G_FILE_TYPE_DIRECTORY;
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  else if (S_ISCHR (statbuf->st_mode) ||
Packit ae235b
	   S_ISBLK (statbuf->st_mode) ||
Packit ae235b
	   S_ISFIFO (statbuf->st_mode)
Packit ae235b
#ifdef S_ISSOCK
Packit ae235b
	   || S_ISSOCK (statbuf->st_mode)
Packit ae235b
#endif
Packit ae235b
	   )
Packit ae235b
    file_type = G_FILE_TYPE_SPECIAL;
Packit ae235b
#endif
Packit ae235b
#ifdef S_ISLNK
Packit ae235b
  else if (S_ISLNK (statbuf->st_mode))
Packit ae235b
    file_type = G_FILE_TYPE_SYMBOLIC_LINK;
Packit ae235b
#elif defined (G_OS_WIN32)
Packit ae235b
  if (statbuf->reparse_tag == IO_REPARSE_TAG_SYMLINK)
Packit ae235b
    file_type = G_FILE_TYPE_SYMBOLIC_LINK;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_file_info_set_file_type (info, file_type);
Packit ae235b
  g_file_info_set_size (info, statbuf->st_size);
Packit ae235b
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  /* Pointless setting these on Windows even if they exist in the struct */
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
Packit ae235b
#endif
Packit ae235b
  /* FIXME: st_mode is mostly pointless on Windows, too. Set the attribute or not? */
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
Packit ae235b
#endif
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
Packit ae235b
                                           statbuf->st_blocks * G_GUINT64_CONSTANT (512));
Packit ae235b
#elif defined (G_OS_WIN32)
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
Packit ae235b
                                           statbuf->allocated_size);
Packit ae235b
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_BIRTHTIME) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtimensec / 1000);
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM) && defined (HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC)
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim.tv_sec);
Packit ae235b
  _g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED_USEC, statbuf->st_birthtim.tv_nsec / 1000);
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIME)
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtime);
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_BIRTHTIM)
Packit ae235b
  _g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CREATED, statbuf->st_birthtim);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_ETAG_VALUE))
Packit ae235b
    {
Packit ae235b
      char *etag = _g_local_file_info_create_etag (statbuf);
Packit ae235b
      _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ETAG_VALUE, etag);
Packit ae235b
      g_free (etag);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_ID_FILE))
Packit ae235b
    {
Packit ae235b
      char *id = _g_local_file_info_create_file_id (statbuf);
Packit ae235b
      _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILE, id);
Packit ae235b
      g_free (id);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM))
Packit ae235b
    {
Packit ae235b
      char *id = _g_local_file_info_create_fs_id (statbuf);
Packit ae235b
      _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_ID_FILESYSTEM, id);
Packit ae235b
      g_free (id);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
Packit ae235b
static char *
Packit ae235b
make_valid_utf8 (const char *name)
Packit ae235b
{
Packit ae235b
  GString *string;
Packit ae235b
  const gchar *remainder, *invalid;
Packit ae235b
  gint remaining_bytes, valid_bytes;
Packit ae235b
  
Packit ae235b
  string = NULL;
Packit ae235b
  remainder = name;
Packit ae235b
  remaining_bytes = strlen (name);
Packit ae235b
  
Packit ae235b
  while (remaining_bytes != 0) 
Packit ae235b
    {
Packit ae235b
      if (g_utf8_validate (remainder, remaining_bytes, &invalid)) 
Packit ae235b
	break;
Packit ae235b
      valid_bytes = invalid - remainder;
Packit ae235b
    
Packit ae235b
      if (string == NULL) 
Packit ae235b
	string = g_string_sized_new (remaining_bytes);
Packit ae235b
Packit ae235b
      g_string_append_len (string, remainder, valid_bytes);
Packit ae235b
      /* append U+FFFD REPLACEMENT CHARACTER */
Packit ae235b
      g_string_append (string, "\357\277\275");
Packit ae235b
      
Packit ae235b
      remaining_bytes -= valid_bytes + 1;
Packit ae235b
      remainder = invalid + 1;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (string == NULL)
Packit ae235b
    return g_strdup (name);
Packit ae235b
  
Packit ae235b
  g_string_append (string, remainder);
Packit ae235b
Packit ae235b
  g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
Packit ae235b
  
Packit ae235b
  return g_string_free (string, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
convert_pwd_string_to_utf8 (char *pwd_str)
Packit ae235b
{
Packit ae235b
  char *utf8_string;
Packit ae235b
  
Packit ae235b
  if (!g_utf8_validate (pwd_str, -1, NULL))
Packit ae235b
    {
Packit ae235b
      utf8_string = g_locale_to_utf8 (pwd_str, -1, NULL, NULL, NULL);
Packit ae235b
      if (utf8_string == NULL)
Packit ae235b
	utf8_string = make_valid_utf8 (pwd_str);
Packit ae235b
    }
Packit ae235b
  else 
Packit ae235b
    utf8_string = g_strdup (pwd_str);
Packit ae235b
  
Packit ae235b
  return utf8_string;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
uid_data_free (UidData *data)
Packit ae235b
{
Packit ae235b
  g_free (data->user_name);
Packit ae235b
  g_free (data->real_name);
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* called with lock held */
Packit ae235b
static UidData *
Packit ae235b
lookup_uid_data (uid_t uid)
Packit ae235b
{
Packit ae235b
  UidData *data;
Packit ae235b
  char buffer[4096];
Packit ae235b
  struct passwd pwbuf;
Packit ae235b
  struct passwd *pwbufp;
Packit ae235b
  char *gecos, *comma;
Packit ae235b
  
Packit ae235b
  if (uid_cache == NULL)
Packit ae235b
    uid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)uid_data_free);
Packit ae235b
Packit ae235b
  data = g_hash_table_lookup (uid_cache, GINT_TO_POINTER (uid));
Packit ae235b
Packit ae235b
  if (data)
Packit ae235b
    return data;
Packit ae235b
Packit ae235b
  data = g_new0 (UidData, 1);
Packit ae235b
Packit ae235b
#if defined(HAVE_GETPWUID_R)
Packit ae235b
  getpwuid_r (uid, &pwbuf, buffer, sizeof(buffer), &pwbufp);
Packit ae235b
#else
Packit ae235b
  pwbufp = getpwuid (uid);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (pwbufp != NULL)
Packit ae235b
    {
Packit ae235b
      if (pwbufp->pw_name != NULL && pwbufp->pw_name[0] != 0)
Packit ae235b
	data->user_name = convert_pwd_string_to_utf8 (pwbufp->pw_name);
Packit ae235b
Packit ae235b
#ifndef __BIONIC__
Packit ae235b
      gecos = pwbufp->pw_gecos;
Packit ae235b
Packit ae235b
      if (gecos)
Packit ae235b
	{
Packit ae235b
	  comma = strchr (gecos, ',');
Packit ae235b
	  if (comma)
Packit ae235b
	    *comma = 0;
Packit ae235b
	  data->real_name = convert_pwd_string_to_utf8 (gecos);
Packit ae235b
	}
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Default fallbacks */
Packit ae235b
  if (data->real_name == NULL)
Packit ae235b
    {
Packit ae235b
      if (data->user_name != NULL)
Packit ae235b
	data->real_name = g_strdup (data->user_name);
Packit ae235b
      else
Packit ae235b
	data->real_name = g_strdup_printf ("user #%d", (int)uid);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (data->user_name == NULL)
Packit ae235b
    data->user_name = g_strdup_printf ("%d", (int)uid);
Packit ae235b
  
Packit ae235b
  g_hash_table_replace (uid_cache, GINT_TO_POINTER (uid), data);
Packit ae235b
  
Packit ae235b
  return data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
get_username_from_uid (uid_t uid)
Packit ae235b
{
Packit ae235b
  char *res;
Packit ae235b
  UidData *data;
Packit ae235b
  
Packit ae235b
  G_LOCK (uid_cache);
Packit ae235b
  data = lookup_uid_data (uid);
Packit ae235b
  res = g_strdup (data->user_name);  
Packit ae235b
  G_UNLOCK (uid_cache);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
get_realname_from_uid (uid_t uid)
Packit ae235b
{
Packit ae235b
  char *res;
Packit ae235b
  UidData *data;
Packit ae235b
  
Packit ae235b
  G_LOCK (uid_cache);
Packit ae235b
  data = lookup_uid_data (uid);
Packit ae235b
  res = g_strdup (data->real_name);  
Packit ae235b
  G_UNLOCK (uid_cache);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* called with lock held */
Packit ae235b
static char *
Packit ae235b
lookup_gid_name (gid_t gid)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  char buffer[4096];
Packit ae235b
  struct group gbuf;
Packit ae235b
  struct group *gbufp;
Packit ae235b
  
Packit ae235b
  if (gid_cache == NULL)
Packit ae235b
    gid_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_free);
Packit ae235b
Packit ae235b
  name = g_hash_table_lookup (gid_cache, GINT_TO_POINTER (gid));
Packit ae235b
Packit ae235b
  if (name)
Packit ae235b
    return name;
Packit ae235b
Packit ae235b
#if defined (HAVE_GETGRGID_R)
Packit ae235b
  getgrgid_r (gid, &gbuf, buffer, sizeof(buffer), &gbufp);
Packit ae235b
#else
Packit ae235b
  gbufp = getgrgid (gid);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (gbufp != NULL &&
Packit ae235b
      gbufp->gr_name != NULL &&
Packit ae235b
      gbufp->gr_name[0] != 0)
Packit ae235b
    name = convert_pwd_string_to_utf8 (gbufp->gr_name);
Packit ae235b
  else
Packit ae235b
    name = g_strdup_printf("%d", (int)gid);
Packit ae235b
  
Packit ae235b
  g_hash_table_replace (gid_cache, GINT_TO_POINTER (gid), name);
Packit ae235b
  
Packit ae235b
  return name;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
get_groupname_from_gid (gid_t gid)
Packit ae235b
{
Packit ae235b
  char *res;
Packit ae235b
  char *name;
Packit ae235b
  
Packit ae235b
  G_LOCK (gid_cache);
Packit ae235b
  name = lookup_gid_name (gid);
Packit ae235b
  res = g_strdup (name);  
Packit ae235b
  G_UNLOCK (gid_cache);
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
static char *
Packit ae235b
get_content_type (const char          *basename,
Packit ae235b
		  const char          *path,
Packit ae235b
		  GLocalFileStat      *statbuf,
Packit ae235b
		  gboolean             is_symlink,
Packit ae235b
		  gboolean             symlink_broken,
Packit ae235b
		  GFileQueryInfoFlags  flags,
Packit ae235b
		  gboolean             fast)
Packit ae235b
{
Packit ae235b
  if (is_symlink &&
Packit ae235b
      (symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
Packit ae235b
    return g_content_type_from_mime_type ("inode/symlink");
Packit ae235b
  else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
Packit ae235b
    return g_content_type_from_mime_type ("inode/directory");
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
Packit ae235b
    return g_content_type_from_mime_type ("inode/chardevice");
Packit ae235b
  else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
Packit ae235b
    return g_content_type_from_mime_type ("inode/blockdevice");
Packit ae235b
  else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
Packit ae235b
    return g_content_type_from_mime_type ("inode/fifo");
Packit ae235b
  else if (statbuf != NULL && S_ISREG(statbuf->st_mode) && statbuf->st_size == 0)
Packit ae235b
    {
Packit ae235b
      /* Don't sniff zero-length files in order to avoid reading files
Packit ae235b
       * that appear normal but are not (eg: files in /proc and /sys)
Packit ae235b
       *
Packit ae235b
       * Note that we need to return text/plain here so that
Packit ae235b
       * newly-created text files are opened by the text editor.
Packit ae235b
       * See https://bugzilla.gnome.org/show_bug.cgi?id=755795
Packit ae235b
       */
Packit ae235b
      return g_content_type_from_mime_type ("text/plain");
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
#ifdef S_ISSOCK
Packit ae235b
  else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
Packit ae235b
    return g_content_type_from_mime_type ("inode/socket");
Packit ae235b
#endif
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      char *content_type;
Packit ae235b
      gboolean result_uncertain;
Packit ae235b
Packit ae235b
      content_type = g_content_type_guess (basename, NULL, 0, &result_uncertain);
Packit ae235b
      
Packit ae235b
#if !defined(G_OS_WIN32) && !defined(HAVE_COCOA)
Packit ae235b
      if (!fast && result_uncertain && path != NULL)
Packit ae235b
	{
Packit ae235b
	  guchar sniff_buffer[4096];
Packit ae235b
	  gsize sniff_length;
Packit ae235b
	  int fd, errsv;
Packit ae235b
Packit ae235b
	  sniff_length = _g_unix_content_type_get_sniff_len ();
Packit ae235b
	  if (sniff_length > 4096)
Packit ae235b
	    sniff_length = 4096;
Packit ae235b
Packit ae235b
#ifdef O_NOATIME	  
Packit ae235b
          fd = g_open (path, O_RDONLY | O_NOATIME, 0);
Packit ae235b
          errsv = errno;
Packit ae235b
          if (fd < 0 && errsv == EPERM)
Packit ae235b
#endif
Packit ae235b
	    fd = g_open (path, O_RDONLY, 0);
Packit ae235b
Packit ae235b
	  if (fd != -1)
Packit ae235b
	    {
Packit ae235b
	      gssize res;
Packit ae235b
	      
Packit ae235b
	      res = read (fd, sniff_buffer, sniff_length);
Packit ae235b
	      (void) g_close (fd, NULL);
Packit ae235b
	      if (res >= 0)
Packit ae235b
		{
Packit ae235b
		  g_free (content_type);
Packit ae235b
		  content_type = g_content_type_guess (basename, sniff_buffer, res, NULL);
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
#endif
Packit ae235b
      
Packit ae235b
      return content_type;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
}
Packit ae235b
Packit ae235b
/* @stat_buf is the pre-calculated result of stat(path), or %NULL if that failed. */
Packit ae235b
static void
Packit ae235b
get_thumbnail_attributes (const char     *path,
Packit ae235b
                          GFileInfo      *info,
Packit ae235b
                          const GLocalFileStat *stat_buf)
Packit ae235b
{
Packit ae235b
  GChecksum *checksum;
Packit ae235b
  char *uri;
Packit ae235b
  char *filename;
Packit ae235b
  char *basename;
Packit ae235b
Packit ae235b
  uri = g_filename_to_uri (path, NULL, NULL);
Packit ae235b
Packit ae235b
  checksum = g_checksum_new (G_CHECKSUM_MD5);
Packit ae235b
  g_checksum_update (checksum, (const guchar *) uri, strlen (uri));
Packit ae235b
Packit ae235b
  basename = g_strconcat (g_checksum_get_string (checksum), ".png", NULL);
Packit ae235b
  g_checksum_free (checksum);
Packit ae235b
Packit ae235b
  filename = g_build_filename (g_get_user_cache_dir (),
Packit ae235b
                               "thumbnails", "large", basename,
Packit ae235b
                               NULL);
Packit ae235b
Packit ae235b
  if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
Packit ae235b
    {
Packit ae235b
      _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
Packit ae235b
      _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
Packit ae235b
                                                thumbnail_verify (filename, uri, stat_buf));
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_free (filename);
Packit ae235b
      filename = g_build_filename (g_get_user_cache_dir (),
Packit ae235b
                                   "thumbnails", "normal", basename,
Packit ae235b
                                   NULL);
Packit ae235b
Packit ae235b
      if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
Packit ae235b
        {
Packit ae235b
          _g_file_info_set_attribute_byte_string_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH, filename);
Packit ae235b
          _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
Packit ae235b
                                                    thumbnail_verify (filename, uri, stat_buf));
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_free (filename);
Packit ae235b
          filename = g_build_filename (g_get_user_cache_dir (),
Packit ae235b
                                       "thumbnails", "fail",
Packit ae235b
                                       "gnome-thumbnail-factory",
Packit ae235b
                                       basename,
Packit ae235b
                                       NULL);
Packit ae235b
Packit ae235b
          if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
Packit ae235b
            {
Packit ae235b
              _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED, TRUE);
Packit ae235b
              _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID,
Packit ae235b
                                                        thumbnail_verify (filename, uri, stat_buf));
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  g_free (basename);
Packit ae235b
  g_free (filename);
Packit ae235b
  g_free (uri);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
static void
Packit ae235b
win32_get_file_user_info (const gchar  *filename,
Packit ae235b
			  gchar       **group_name, 
Packit ae235b
			  gchar       **user_name, 
Packit ae235b
			  gchar       **real_name)
Packit ae235b
{
Packit ae235b
  PSECURITY_DESCRIPTOR psd = NULL;
Packit ae235b
  DWORD sd_size = 0; /* first call calculates the size required */
Packit ae235b
  
Packit ae235b
  wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
Packit ae235b
  if ((GetFileSecurityW (wfilename, 
Packit ae235b
                        GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
Packit ae235b
			NULL,
Packit ae235b
			sd_size,
Packit ae235b
			&sd_size) || (ERROR_INSUFFICIENT_BUFFER == GetLastError())) &&
Packit ae235b
     (psd = g_try_malloc (sd_size)) != NULL &&
Packit ae235b
     GetFileSecurityW (wfilename, 
Packit ae235b
                       GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION,
Packit ae235b
		       psd,
Packit ae235b
		       sd_size,
Packit ae235b
		       &sd_size))
Packit ae235b
    {
Packit ae235b
      PSID psid = 0;
Packit ae235b
      BOOL defaulted;
Packit ae235b
      SID_NAME_USE name_use = 0; /* don't care? */
Packit ae235b
      wchar_t *name = NULL;
Packit ae235b
      wchar_t *domain = NULL;
Packit ae235b
      DWORD name_len = 0;
Packit ae235b
      DWORD domain_len = 0;
Packit ae235b
      /* get the user name */
Packit ae235b
      do {
Packit ae235b
        if (!user_name)
Packit ae235b
	  break;
Packit ae235b
	if (!GetSecurityDescriptorOwner (psd, &psid, &defaulted))
Packit ae235b
	  break;
Packit ae235b
	if (!LookupAccountSidW (NULL, /* local machine */
Packit ae235b
                                psid, 
Packit ae235b
			        name, &name_len,
Packit ae235b
			        domain, &domain_len, /* no domain info yet */
Packit ae235b
			        &name_use)  && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
Packit ae235b
	  break;
Packit ae235b
	name = g_try_malloc (name_len * sizeof (wchar_t));
Packit ae235b
	domain = g_try_malloc (domain_len * sizeof (wchar_t));
Packit ae235b
	if (name && domain &&
Packit ae235b
            LookupAccountSidW (NULL, /* local machine */
Packit ae235b
                               psid, 
Packit ae235b
			       name, &name_len,
Packit ae235b
			       domain, &domain_len, /* no domain info yet */
Packit ae235b
			       &name_use))
Packit ae235b
	  {
Packit ae235b
	    *user_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
Packit ae235b
	  }
Packit ae235b
	g_free (name);
Packit ae235b
	g_free (domain);
Packit ae235b
      } while (FALSE);
Packit ae235b
Packit ae235b
      /* get the group name */
Packit ae235b
      do {
Packit ae235b
        if (!group_name)
Packit ae235b
	  break;
Packit ae235b
	if (!GetSecurityDescriptorGroup (psd, &psid, &defaulted))
Packit ae235b
	  break;
Packit ae235b
	if (!LookupAccountSidW (NULL, /* local machine */
Packit ae235b
                                psid, 
Packit ae235b
			        name, &name_len,
Packit ae235b
			        domain, &domain_len, /* no domain info yet */
Packit ae235b
			        &name_use)  && (ERROR_INSUFFICIENT_BUFFER != GetLastError()))
Packit ae235b
	  break;
Packit ae235b
	name = g_try_malloc (name_len * sizeof (wchar_t));
Packit ae235b
	domain = g_try_malloc (domain_len * sizeof (wchar_t));
Packit ae235b
	if (name && domain &&
Packit ae235b
            LookupAccountSidW (NULL, /* local machine */
Packit ae235b
                               psid, 
Packit ae235b
			       name, &name_len,
Packit ae235b
			       domain, &domain_len, /* no domain info yet */
Packit ae235b
			       &name_use))
Packit ae235b
	  {
Packit ae235b
	    *group_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
Packit ae235b
	  }
Packit ae235b
	g_free (name);
Packit ae235b
	g_free (domain);
Packit ae235b
      } while (FALSE);
Packit ae235b
Packit ae235b
      /* TODO: get real name */
Packit ae235b
Packit ae235b
      g_free (psd);
Packit ae235b
    }
Packit ae235b
  g_free (wfilename);
Packit ae235b
}
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
/* support for '.hidden' files */
Packit ae235b
G_LOCK_DEFINE_STATIC (hidden_cache);
Packit ae235b
static GHashTable *hidden_cache;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
remove_from_hidden_cache (gpointer user_data)
Packit ae235b
{
Packit ae235b
  G_LOCK (hidden_cache);
Packit ae235b
  g_hash_table_remove (hidden_cache, user_data);
Packit ae235b
  G_UNLOCK (hidden_cache);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GHashTable *
Packit ae235b
read_hidden_file (const gchar *dirname)
Packit ae235b
{
Packit ae235b
  gchar *contents = NULL;
Packit ae235b
  gchar *filename;
Packit ae235b
Packit ae235b
  filename = g_build_path ("/", dirname, ".hidden", NULL);
Packit ae235b
  (void) g_file_get_contents (filename, &contents, NULL, NULL);
Packit ae235b
  g_free (filename);
Packit ae235b
Packit ae235b
  if (contents != NULL)
Packit ae235b
    {
Packit ae235b
      GHashTable *table;
Packit ae235b
      gchar **lines;
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
Packit ae235b
Packit ae235b
      lines = g_strsplit (contents, "\n", 0);
Packit ae235b
      g_free (contents);
Packit ae235b
Packit ae235b
      for (i = 0; lines[i]; i++)
Packit ae235b
        /* hash table takes the individual strings... */
Packit ae235b
        g_hash_table_add (table, lines[i]);
Packit ae235b
Packit ae235b
      /* ... so we only free the container. */
Packit ae235b
      g_free (lines);
Packit ae235b
Packit ae235b
      return table;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
maybe_unref_hash_table (gpointer data)
Packit ae235b
{
Packit ae235b
  if (data != NULL)
Packit ae235b
    g_hash_table_unref (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
file_is_hidden (const gchar *path,
Packit ae235b
                const gchar *basename)
Packit ae235b
{
Packit ae235b
  gboolean result;
Packit ae235b
  gchar *dirname;
Packit ae235b
  gpointer table;
Packit ae235b
Packit ae235b
  dirname = g_path_get_dirname (path);
Packit ae235b
Packit ae235b
  G_LOCK (hidden_cache);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (hidden_cache == NULL)
Packit ae235b
    hidden_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
Packit ae235b
                                          g_free, maybe_unref_hash_table);
Packit ae235b
Packit ae235b
  if (!g_hash_table_lookup_extended (hidden_cache, dirname,
Packit ae235b
                                     NULL, &table))
Packit ae235b
    {
Packit ae235b
      gchar *mydirname;
Packit ae235b
      GSource *remove_from_cache_source;
Packit ae235b
Packit ae235b
      g_hash_table_insert (hidden_cache,
Packit ae235b
                           mydirname = g_strdup (dirname),
Packit ae235b
                           table = read_hidden_file (dirname));
Packit ae235b
Packit ae235b
      remove_from_cache_source = g_timeout_source_new_seconds (5);
Packit ae235b
      g_source_set_priority (remove_from_cache_source, G_PRIORITY_DEFAULT);
Packit ae235b
      g_source_set_callback (remove_from_cache_source, 
Packit ae235b
                             remove_from_hidden_cache, 
Packit ae235b
                             mydirname, 
Packit ae235b
                             NULL);
Packit ae235b
      g_source_attach (remove_from_cache_source, 
Packit ae235b
                       GLIB_PRIVATE_CALL (g_get_worker_context) ());
Packit ae235b
      g_source_unref (remove_from_cache_source);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = table != NULL && g_hash_table_contains (table, basename);
Packit ae235b
Packit ae235b
  G_UNLOCK (hidden_cache);
Packit ae235b
Packit ae235b
  g_free (dirname);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_local_file_info_get_nostat (GFileInfo              *info,
Packit ae235b
                               const char             *basename,
Packit ae235b
			       const char             *path,
Packit ae235b
                               GFileAttributeMatcher  *attribute_matcher)
Packit ae235b
{
Packit ae235b
  g_file_info_set_name (info, basename);
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME))
Packit ae235b
    {
Packit ae235b
      char *display_name = g_filename_display_basename (path);
Packit ae235b
     
Packit ae235b
      /* look for U+FFFD REPLACEMENT CHARACTER */ 
Packit ae235b
      if (strstr (display_name, "\357\277\275") != NULL)
Packit ae235b
	{
Packit ae235b
	  char *p = display_name;
Packit ae235b
	  display_name = g_strconcat (display_name, _(" (invalid encoding)"), NULL);
Packit ae235b
	  g_free (p);
Packit ae235b
	}
Packit ae235b
      g_file_info_set_display_name (info, display_name);
Packit ae235b
      g_free (display_name);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_EDIT_NAME))
Packit ae235b
    {
Packit ae235b
      char *edit_name = g_filename_display_basename (path);
Packit ae235b
      g_file_info_set_edit_name (info, edit_name);
Packit ae235b
      g_free (edit_name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME))
Packit ae235b
    {
Packit ae235b
      char *copy_name = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
Packit ae235b
      if (copy_name)
Packit ae235b
	_g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_COPY_NAME, copy_name);
Packit ae235b
      g_free (copy_name);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static const char *
Packit ae235b
get_icon_name (const char *path,
Packit ae235b
               const char *content_type,
Packit ae235b
               gboolean    use_symbolic,
Packit ae235b
               gboolean   *with_fallbacks_out)
Packit ae235b
{
Packit ae235b
  const char *name = NULL;
Packit ae235b
  gboolean with_fallbacks = TRUE;
Packit ae235b
Packit ae235b
  if (g_strcmp0 (path, g_get_home_dir ()) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "user-home-symbolic" : "user-home";
Packit ae235b
      with_fallbacks = FALSE;
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "user-desktop-symbolic" : "user-desktop";
Packit ae235b
      with_fallbacks = FALSE;
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-documents-symbolic" : "folder-documents";
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-download-symbolic" : "folder-download";
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_MUSIC)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-music-symbolic" : "folder-music";
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PICTURES)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-pictures-symbolic" : "folder-pictures";
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-publicshare-symbolic" : "folder-publicshare";
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-templates-symbolic" : "folder-templates";
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (path, g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS)) == 0)
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-videos-symbolic" : "folder-videos";
Packit ae235b
    }
Packit ae235b
  else if (g_content_type_is_mime_type (content_type,"inode/directory"))
Packit ae235b
    {
Packit ae235b
      name = use_symbolic ? "folder-symbolic" : "folder";
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      name = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (with_fallbacks_out != NULL)
Packit ae235b
    *with_fallbacks_out = with_fallbacks;
Packit ae235b
Packit ae235b
  return name;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
get_icon (const char *path,
Packit ae235b
          const char *content_type,
Packit ae235b
          gboolean    use_symbolic)
Packit ae235b
{
Packit ae235b
  GIcon *icon = NULL;
Packit ae235b
  const char *icon_name;
Packit ae235b
  gboolean with_fallbacks;
Packit ae235b
Packit ae235b
  icon_name = get_icon_name (path, content_type, use_symbolic, &with_fallbacks);
Packit ae235b
  if (icon_name != NULL)
Packit ae235b
    {
Packit ae235b
      if (with_fallbacks)
Packit ae235b
        icon = g_themed_icon_new_with_default_fallbacks (icon_name);
Packit ae235b
      else
Packit ae235b
        icon = g_themed_icon_new (icon_name);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (use_symbolic)
Packit ae235b
        icon = g_content_type_get_symbolic_icon (content_type);
Packit ae235b
      else
Packit ae235b
        icon = g_content_type_get_icon (content_type);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
GFileInfo *
Packit ae235b
_g_local_file_info_get (const char             *basename,
Packit ae235b
			const char             *path,
Packit ae235b
			GFileAttributeMatcher  *attribute_matcher,
Packit ae235b
			GFileQueryInfoFlags     flags,
Packit ae235b
			GLocalParentFileInfo   *parent_info,
Packit ae235b
			GError                **error)
Packit ae235b
{
Packit ae235b
  GFileInfo *info;
Packit ae235b
  GLocalFileStat statbuf;
Packit ae235b
#ifdef S_ISLNK
Packit ae235b
  struct stat statbuf2;
Packit ae235b
#elif defined (G_OS_WIN32)
Packit ae235b
  GWin32PrivateStat statbuf2;
Packit ae235b
#endif
Packit ae235b
  int res;
Packit ae235b
  gboolean stat_ok;
Packit ae235b
  gboolean is_symlink, symlink_broken;
Packit ae235b
  char *symlink_target;
Packit ae235b
  GVfs *vfs;
Packit ae235b
  GVfsClass *class;
Packit ae235b
  guint64 device;
Packit ae235b
Packit ae235b
  info = g_file_info_new ();
Packit ae235b
Packit ae235b
  /* Make sure we don't set any unwanted attributes */
Packit ae235b
  g_file_info_set_attribute_mask (info, attribute_matcher);
Packit ae235b
  
Packit ae235b
  _g_local_file_info_get_nostat (info, basename, path, attribute_matcher);
Packit ae235b
Packit ae235b
  if (attribute_matcher == NULL)
Packit ae235b
    {
Packit ae235b
      g_file_info_unset_attribute_mask (info);
Packit ae235b
      return info;
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  res = g_lstat (path, &statbuf);
Packit ae235b
#else
Packit ae235b
  res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, &statbuf);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (res == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      /* Don't bail out if we get Permission denied (SELinux?) */
Packit ae235b
      if (errsv != EACCES)
Packit ae235b
        {
Packit ae235b
          char *display_name = g_filename_display_name (path);
Packit ae235b
          g_object_unref (info);
Packit ae235b
          g_set_error (error, G_IO_ERROR,
Packit ae235b
		       g_io_error_from_errno (errsv),
Packit ae235b
		       _("Error when getting information for file “%s”: %s"),
Packit ae235b
		       display_name, g_strerror (errsv));
Packit ae235b
          g_free (display_name);
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Even if stat() fails, try to get as much as other attributes possible */
Packit ae235b
  stat_ok = res != -1;
Packit ae235b
Packit ae235b
  if (stat_ok)
Packit ae235b
    device = statbuf.st_dev;
Packit ae235b
  else
Packit ae235b
    device = 0;
Packit ae235b
Packit ae235b
#ifdef S_ISLNK
Packit ae235b
  is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
Packit ae235b
#elif defined (G_OS_WIN32)
Packit ae235b
  /* glib already checked the FILE_ATTRIBUTE_REPARSE_POINT for us */
Packit ae235b
  is_symlink = stat_ok && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK; 
Packit ae235b
#else
Packit ae235b
  is_symlink = FALSE;
Packit ae235b
#endif
Packit ae235b
  symlink_broken = FALSE;
Packit ae235b
Packit ae235b
  if (is_symlink)
Packit ae235b
    {
Packit ae235b
      g_file_info_set_is_symlink (info, TRUE);
Packit ae235b
Packit ae235b
      /* Unless NOFOLLOW was set we default to following symlinks */
Packit ae235b
      if (!(flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS))
Packit ae235b
	{
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
	  res = stat (path, &statbuf2);
Packit ae235b
#else
Packit ae235b
	  res = GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, &statbuf2);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
	  /* Report broken links as symlinks */
Packit ae235b
	  if (res != -1)
Packit ae235b
	    {
Packit ae235b
	      statbuf = statbuf2;
Packit ae235b
	      stat_ok = TRUE;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    symlink_broken = TRUE;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (stat_ok)
Packit ae235b
    set_info_from_stat (info, &statbuf, attribute_matcher);
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
Packit ae235b
    g_file_info_set_is_hidden (info, TRUE);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_IS_HIDDEN))
Packit ae235b
    {
Packit ae235b
      if (basename != NULL &&
Packit ae235b
          (basename[0] == '.' ||
Packit ae235b
           file_is_hidden (path, basename)))
Packit ae235b
        g_file_info_set_is_hidden (info, TRUE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (basename != NULL && basename[strlen (basename) -1] == '~' &&
Packit ae235b
      (stat_ok && S_ISREG (statbuf.st_mode)))
Packit ae235b
    _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
Packit ae235b
#else
Packit ae235b
  if (statbuf.attributes & FILE_ATTRIBUTE_HIDDEN)
Packit ae235b
    g_file_info_set_is_hidden (info, TRUE);
Packit ae235b
Packit ae235b
  if (statbuf.attributes & FILE_ATTRIBUTE_ARCHIVE)
Packit ae235b
    _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_ARCHIVE, TRUE);
Packit ae235b
Packit ae235b
  if (statbuf.attributes & FILE_ATTRIBUTE_SYSTEM)
Packit ae235b
    _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_DOS_IS_SYSTEM, TRUE);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  symlink_target = NULL;
Packit ae235b
  if (is_symlink)
Packit ae235b
    {
Packit ae235b
#if defined (S_ISLNK) || defined (G_OS_WIN32)
Packit ae235b
      symlink_target = read_link (path);
Packit ae235b
#endif
Packit ae235b
      if (symlink_target &&
Packit ae235b
          _g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
                                                G_FILE_ATTRIBUTE_ID_STANDARD_SYMLINK_TARGET))
Packit ae235b
        g_file_info_set_symlink_target (info, symlink_target);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_CONTENT_TYPE) ||
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_ICON) ||
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
Packit ae235b
    {
Packit ae235b
      char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, FALSE);
Packit ae235b
Packit ae235b
      if (content_type)
Packit ae235b
	{
Packit ae235b
	  g_file_info_set_content_type (info, content_type);
Packit ae235b
Packit ae235b
	  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
                                                     G_FILE_ATTRIBUTE_ID_STANDARD_ICON)
Packit ae235b
               || _g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
                                                        G_FILE_ATTRIBUTE_ID_STANDARD_SYMBOLIC_ICON))
Packit ae235b
	    {
Packit ae235b
	      GIcon *icon;
Packit ae235b
Packit ae235b
              /* non symbolic icon */
Packit ae235b
              icon = get_icon (path, content_type, FALSE);
Packit ae235b
              if (icon != NULL)
Packit ae235b
                {
Packit ae235b
                  g_file_info_set_icon (info, icon);
Packit ae235b
                  g_object_unref (icon);
Packit ae235b
                }
Packit ae235b
Packit ae235b
              /* symbolic icon */
Packit ae235b
              icon = get_icon (path, content_type, TRUE);
Packit ae235b
              if (icon != NULL)
Packit ae235b
                {
Packit ae235b
                  g_file_info_set_symbolic_icon (info, icon);
Packit ae235b
                  g_object_unref (icon);
Packit ae235b
                }
Packit ae235b
Packit ae235b
	    }
Packit ae235b
	  
Packit ae235b
	  g_free (content_type);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE))
Packit ae235b
    {
Packit ae235b
      char *content_type = get_content_type (basename, path, stat_ok ? &statbuf : NULL, is_symlink, symlink_broken, flags, TRUE);
Packit ae235b
      
Packit ae235b
      if (content_type)
Packit ae235b
	{
Packit ae235b
	  _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_FAST_CONTENT_TYPE, content_type);
Packit ae235b
	  g_free (content_type);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_OWNER_USER))
Packit ae235b
    {
Packit ae235b
      char *name = NULL;
Packit ae235b
      
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      win32_get_file_user_info (path, NULL, &name, NULL);
Packit ae235b
#else
Packit ae235b
      if (stat_ok)
Packit ae235b
        name = get_username_from_uid (statbuf.st_uid);
Packit ae235b
#endif
Packit ae235b
      if (name)
Packit ae235b
	_g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
Packit ae235b
      g_free (name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL))
Packit ae235b
    {
Packit ae235b
      char *name = NULL;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      win32_get_file_user_info (path, NULL, NULL, &name);
Packit ae235b
#else
Packit ae235b
      if (stat_ok)
Packit ae235b
        name = get_realname_from_uid (statbuf.st_uid);
Packit ae235b
#endif
Packit ae235b
      if (name)
Packit ae235b
	_g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
Packit ae235b
      g_free (name);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
					    G_FILE_ATTRIBUTE_ID_OWNER_GROUP))
Packit ae235b
    {
Packit ae235b
      char *name = NULL;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      win32_get_file_user_info (path, &name, NULL, NULL);
Packit ae235b
#else
Packit ae235b
      if (stat_ok)
Packit ae235b
        name = get_groupname_from_gid (statbuf.st_gid);
Packit ae235b
#endif
Packit ae235b
      if (name)
Packit ae235b
	_g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
Packit ae235b
      g_free (name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (stat_ok && parent_info && parent_info->device != 0 &&
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
Packit ae235b
      statbuf.st_dev != parent_info->device) 
Packit ae235b
    _g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
Packit ae235b
  
Packit ae235b
  if (stat_ok)
Packit ae235b
    get_access_rights (attribute_matcher, info, path, &statbuf, parent_info);
Packit ae235b
  
Packit ae235b
#ifdef HAVE_SELINUX
Packit ae235b
  get_selinux_context (path, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
Packit ae235b
#endif
Packit ae235b
  get_xattrs (path, TRUE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
Packit ae235b
  get_xattrs (path, FALSE, info, attribute_matcher, (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) == 0);
Packit ae235b
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
                                            G_FILE_ATTRIBUTE_ID_THUMBNAIL_PATH) ||
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
                                            G_FILE_ATTRIBUTE_ID_THUMBNAIL_IS_VALID) ||
Packit ae235b
      _g_file_attribute_matcher_matches_id (attribute_matcher,
Packit ae235b
                                            G_FILE_ATTRIBUTE_ID_THUMBNAILING_FAILED))
Packit ae235b
    {
Packit ae235b
      if (stat_ok)
Packit ae235b
          get_thumbnail_attributes (path, info, &statbuf);
Packit ae235b
      else
Packit ae235b
          get_thumbnail_attributes (path, info, 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_add_info)
Packit ae235b
    {
Packit ae235b
      class->local_file_add_info (vfs,
Packit ae235b
                                  path,
Packit ae235b
                                  device,
Packit ae235b
                                  attribute_matcher,
Packit ae235b
                                  info,
Packit ae235b
                                  NULL,
Packit ae235b
                                  &parent_info->extra_data,
Packit ae235b
                                  &parent_info->free_extra_data);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_file_info_unset_attribute_mask (info);
Packit ae235b
Packit ae235b
  g_free (symlink_target);
Packit ae235b
Packit ae235b
  return info;
Packit ae235b
}
Packit ae235b
Packit ae235b
GFileInfo *
Packit ae235b
_g_local_file_info_get_from_fd (int         fd,
Packit ae235b
				const char *attributes,
Packit ae235b
				GError    **error)
Packit ae235b
{
Packit ae235b
  GLocalFileStat stat_buf;
Packit ae235b
  GFileAttributeMatcher *matcher;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#define FSTAT GLIB_PRIVATE_CALL (g_win32_fstat)
Packit ae235b
#else
Packit ae235b
#define FSTAT fstat
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (FSTAT (fd, &stat_buf) == -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 when getting information for file descriptor: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  info = g_file_info_new ();
Packit ae235b
Packit ae235b
  matcher = g_file_attribute_matcher_new (attributes);
Packit ae235b
Packit ae235b
  /* Make sure we don't set any unwanted attributes */
Packit ae235b
  g_file_info_set_attribute_mask (info, matcher);
Packit ae235b
  
Packit ae235b
  set_info_from_stat (info, &stat_buf, matcher);
Packit ae235b
  
Packit ae235b
#ifdef HAVE_SELINUX
Packit ae235b
  if (_g_file_attribute_matcher_matches_id (matcher, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT) &&
Packit ae235b
      is_selinux_enabled ())
Packit ae235b
    {
Packit ae235b
      char *context;
Packit ae235b
      if (fgetfilecon_raw (fd, &context) >= 0)
Packit ae235b
	{
Packit ae235b
	  _g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_SELINUX_CONTEXT, context);
Packit ae235b
	  freecon (context);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  get_xattrs_from_fd (fd, TRUE, info, matcher);
Packit ae235b
  get_xattrs_from_fd (fd, FALSE, info, matcher);
Packit ae235b
  
Packit ae235b
  g_file_attribute_matcher_unref (matcher);
Packit ae235b
Packit ae235b
  g_file_info_unset_attribute_mask (info);
Packit ae235b
  
Packit ae235b
  return info;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_uint32 (const GFileAttributeValue  *value,
Packit ae235b
	    guint32                    *val_out,
Packit ae235b
	    GError                    **error)
Packit ae235b
{
Packit ae235b
  if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT32)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Invalid attribute type (uint32 expected)"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *val_out = value->u.uint32;
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef HAVE_UTIMES
Packit ae235b
static gboolean
Packit ae235b
get_uint64 (const GFileAttributeValue  *value,
Packit ae235b
	    guint64                    *val_out,
Packit ae235b
	    GError                    **error)
Packit ae235b
{
Packit ae235b
  if (value->type != G_FILE_ATTRIBUTE_TYPE_UINT64)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Invalid attribute type (uint64 expected)"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *val_out = value->u.uint64;
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#if defined(HAVE_SYMLINK)
Packit ae235b
static gboolean
Packit ae235b
get_byte_string (const GFileAttributeValue  *value,
Packit ae235b
		 const char                **val_out,
Packit ae235b
		 GError                    **error)
Packit ae235b
{
Packit ae235b
  if (value->type != G_FILE_ATTRIBUTE_TYPE_BYTE_STRING)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Invalid attribute type (byte string expected)"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *val_out = value->u.string;
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_SELINUX
Packit ae235b
static gboolean
Packit ae235b
get_string (const GFileAttributeValue  *value,
Packit ae235b
	    const char                **val_out,
Packit ae235b
	    GError                    **error)
Packit ae235b
{
Packit ae235b
  if (value->type != G_FILE_ATTRIBUTE_TYPE_STRING)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Invalid attribute type (byte string expected)"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *val_out = value->u.string;
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
set_unix_mode (char                       *filename,
Packit ae235b
               GFileQueryInfoFlags         flags,
Packit ae235b
	       const GFileAttributeValue  *value,
Packit ae235b
	       GError                    **error)
Packit ae235b
{
Packit ae235b
  guint32 val = 0;
Packit ae235b
  int res = 0;
Packit ae235b
  
Packit ae235b
  if (!get_uint32 (value, &val, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
#if defined (HAVE_SYMLINK) || defined (G_OS_WIN32)
Packit ae235b
  if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS) {
Packit ae235b
#ifdef HAVE_LCHMOD
Packit ae235b
    res = lchmod (filename, val);
Packit ae235b
#else
Packit ae235b
    gboolean is_symlink;
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
    struct stat statbuf;
Packit ae235b
    /* Calling chmod on a symlink changes permissions on the symlink.
Packit ae235b
     * We don't want to do this, so we need to check for a symlink */
Packit ae235b
    res = g_lstat (filename, &statbuf);
Packit ae235b
    is_symlink = (res == 0 && S_ISLNK (statbuf.st_mode));
Packit ae235b
#else
Packit ae235b
    /* FIXME: implement lchmod for W32, should be doable */
Packit ae235b
    GWin32PrivateStat statbuf;
Packit ae235b
Packit ae235b
    res = GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (filename, &statbuf);
Packit ae235b
    is_symlink = (res == 0 && statbuf.reparse_tag == IO_REPARSE_TAG_SYMLINK);
Packit ae235b
#endif
Packit ae235b
    if (is_symlink)
Packit ae235b
      {
Packit ae235b
        g_set_error_literal (error, G_IO_ERROR,
Packit ae235b
                             G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
                             _("Cannot set permissions on symlinks"));
Packit ae235b
        return FALSE;
Packit ae235b
      }
Packit ae235b
    else if (res == 0)
Packit ae235b
      res = g_chmod (filename, val);
Packit ae235b
#endif
Packit ae235b
  } else
Packit ae235b
#endif
Packit ae235b
    res = g_chmod (filename, val);
Packit ae235b
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 setting permissions: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static gboolean
Packit ae235b
set_unix_uid_gid (char                       *filename,
Packit ae235b
		  const GFileAttributeValue  *uid_value,
Packit ae235b
		  const GFileAttributeValue  *gid_value,
Packit ae235b
		  GFileQueryInfoFlags         flags,
Packit ae235b
		  GError                    **error)
Packit ae235b
{
Packit ae235b
  int res;
Packit ae235b
  guint32 val = 0;
Packit ae235b
  uid_t uid;
Packit ae235b
  gid_t gid;
Packit ae235b
  
Packit ae235b
  if (uid_value)
Packit ae235b
    {
Packit ae235b
      if (!get_uint32 (uid_value, &val, error))
Packit ae235b
	return FALSE;
Packit ae235b
      uid = val;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    uid = -1;
Packit ae235b
  
Packit ae235b
  if (gid_value)
Packit ae235b
    {
Packit ae235b
      if (!get_uint32 (gid_value, &val, error))
Packit ae235b
	return FALSE;
Packit ae235b
      gid = val;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    gid = -1;
Packit ae235b
  
Packit ae235b
#ifdef HAVE_LCHOWN
Packit ae235b
  if (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)
Packit ae235b
    res = lchown (filename, uid, gid);
Packit ae235b
  else
Packit ae235b
#endif
Packit ae235b
    res = chown (filename, uid, gid);
Packit ae235b
  
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 setting owner: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
	  return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_SYMLINK
Packit ae235b
static gboolean
Packit ae235b
set_symlink (char                       *filename,
Packit ae235b
	     const GFileAttributeValue  *value,
Packit ae235b
	     GError                    **error)
Packit ae235b
{
Packit ae235b
  const char *val;
Packit ae235b
  struct stat statbuf;
Packit ae235b
  
Packit ae235b
  if (!get_byte_string (value, &val, error))
Packit ae235b
    return FALSE;
Packit ae235b
  
Packit ae235b
  if (val == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("symlink must be non-NULL"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (g_lstat (filename, &statbuf))
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 setting symlink: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (!S_ISLNK (statbuf.st_mode))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_NOT_SYMBOLIC_LINK,
Packit ae235b
                           _("Error setting symlink: file is not a symlink"));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (g_unlink (filename))
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 setting symlink: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (symlink (filename, val) != 0)
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 setting symlink: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_UTIMES
Packit ae235b
static int
Packit ae235b
lazy_stat (char        *filename, 
Packit ae235b
           struct stat *statbuf, 
Packit ae235b
           gboolean    *called_stat)
Packit ae235b
{
Packit ae235b
  int res;
Packit ae235b
Packit ae235b
  if (*called_stat)
Packit ae235b
    return 0;
Packit ae235b
  
Packit ae235b
  res = g_stat (filename, statbuf);
Packit ae235b
  
Packit ae235b
  if (res == 0)
Packit ae235b
    *called_stat = TRUE;
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
set_mtime_atime (char                       *filename,
Packit ae235b
		 const GFileAttributeValue  *mtime_value,
Packit ae235b
		 const GFileAttributeValue  *mtime_usec_value,
Packit ae235b
		 const GFileAttributeValue  *atime_value,
Packit ae235b
		 const GFileAttributeValue  *atime_usec_value,
Packit ae235b
		 GError                    **error)
Packit ae235b
{
Packit ae235b
  int res;
Packit ae235b
  guint64 val = 0;
Packit ae235b
  guint32 val_usec = 0;
Packit ae235b
  struct stat statbuf;
Packit ae235b
  gboolean got_stat = FALSE;
Packit ae235b
  struct timeval times[2] = { {0, 0}, {0, 0} };
Packit ae235b
Packit ae235b
  /* ATIME */
Packit ae235b
  if (atime_value)
Packit ae235b
    {
Packit ae235b
      if (!get_uint64 (atime_value, &val, error))
Packit ae235b
	return FALSE;
Packit ae235b
      times[0].tv_sec = val;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (lazy_stat (filename, &statbuf, &got_stat) == 0)
Packit ae235b
	{
Packit ae235b
	  times[0].tv_sec = statbuf.st_mtime;
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
Packit ae235b
	  times[0].tv_usec = statbuf.st_atimensec / 1000;
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
Packit ae235b
	  times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
Packit ae235b
#endif
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (atime_usec_value)
Packit ae235b
    {
Packit ae235b
      if (!get_uint32 (atime_usec_value, &val_usec, error))
Packit ae235b
	return FALSE;
Packit ae235b
      times[0].tv_usec = val_usec;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* MTIME */
Packit ae235b
  if (mtime_value)
Packit ae235b
    {
Packit ae235b
      if (!get_uint64 (mtime_value, &val, error))
Packit ae235b
	return FALSE;
Packit ae235b
      times[1].tv_sec = val;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (lazy_stat (filename, &statbuf, &got_stat) == 0)
Packit ae235b
	{
Packit ae235b
	  times[1].tv_sec = statbuf.st_mtime;
Packit ae235b
#if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
Packit ae235b
	  times[1].tv_usec = statbuf.st_mtimensec / 1000;
Packit ae235b
#elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
Packit ae235b
	  times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
Packit ae235b
#endif
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (mtime_usec_value)
Packit ae235b
    {
Packit ae235b
      if (!get_uint32 (mtime_usec_value, &val_usec, error))
Packit ae235b
	return FALSE;
Packit ae235b
      times[1].tv_usec = val_usec;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  res = utimes (filename, times);
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 setting modification or access time: %s"),
Packit ae235b
		   g_strerror (errsv));
Packit ae235b
	  return FALSE;
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
#ifdef HAVE_SELINUX
Packit ae235b
static gboolean
Packit ae235b
set_selinux_context (char                       *filename,
Packit ae235b
		 const GFileAttributeValue  *value,
Packit ae235b
		 GError                    **error)
Packit ae235b
{
Packit ae235b
  const char *val;
Packit ae235b
Packit ae235b
  if (!get_string (value, &val, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (val == NULL)
Packit ae235b
  {
Packit ae235b
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                         _("SELinux context must be non-NULL"));
Packit ae235b
    return FALSE;
Packit ae235b
  }
Packit ae235b
Packit ae235b
  if (is_selinux_enabled ()) {
Packit ae235b
	security_context_t val_s;
Packit ae235b
	
Packit ae235b
	val_s = g_strdup (val);
Packit ae235b
	
Packit ae235b
	if (setfilecon_raw (filename, val_s) < 0)
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 setting SELinux context: %s"),
Packit ae235b
                         g_strerror (errsv));
Packit ae235b
            return FALSE;
Packit ae235b
        }
Packit ae235b
        g_free (val_s);
Packit ae235b
  } else {
Packit ae235b
    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                         _("SELinux is not enabled on this system"));
Packit ae235b
    return FALSE;
Packit ae235b
  }
Packit ae235b
                                                     
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
#endif 
Packit ae235b
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
_g_local_file_info_set_attribute (char                 *filename,
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
  GFileAttributeValue value = { 0 };
Packit ae235b
  GVfsClass *class;
Packit ae235b
  GVfs *vfs;
Packit ae235b
Packit ae235b
  _g_file_attribute_value_set_from_pointer (&value, type, value_p, FALSE);
Packit ae235b
  
Packit ae235b
  if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_MODE) == 0)
Packit ae235b
    return set_unix_mode (filename, flags, &value, error);
Packit ae235b
  
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_UID) == 0)
Packit ae235b
    return set_unix_uid_gid (filename, &value, NULL, flags, error);
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_UNIX_GID) == 0)
Packit ae235b
    return set_unix_uid_gid (filename, NULL, &value, flags, error);
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
#ifdef HAVE_SYMLINK
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET) == 0)
Packit ae235b
    return set_symlink (filename, &value, error);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_UTIMES
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED) == 0)
Packit ae235b
    return set_mtime_atime (filename, &value, NULL, NULL, NULL, error);
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC) == 0)
Packit ae235b
    return set_mtime_atime (filename, NULL, &value, NULL, NULL, error);
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS) == 0)
Packit ae235b
    return set_mtime_atime (filename, NULL, NULL, &value, NULL, error);
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC) == 0)
Packit ae235b
    return set_mtime_atime (filename, NULL, NULL, NULL, &value, error);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_XATTR
Packit ae235b
  else if (g_str_has_prefix (attribute, "xattr::"))
Packit ae235b
    return set_xattr (filename, attribute, &value, error);
Packit ae235b
  else if (g_str_has_prefix (attribute, "xattr-sys::"))
Packit ae235b
    return set_xattr (filename, attribute, &value, error);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_SELINUX 
Packit ae235b
  else if (strcmp (attribute, G_FILE_ATTRIBUTE_SELINUX_CONTEXT) == 0)
Packit ae235b
    return set_selinux_context (filename, &value, error);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  vfs = g_vfs_get_default ();
Packit ae235b
  class = G_VFS_GET_CLASS (vfs);
Packit ae235b
  if (class->local_file_set_attributes)
Packit ae235b
    {
Packit ae235b
      GFileInfo *info;
Packit ae235b
Packit ae235b
      info = g_file_info_new ();
Packit ae235b
      g_file_info_set_attribute (info,
Packit ae235b
                                 attribute,
Packit ae235b
                                 type,
Packit ae235b
                                 value_p);
Packit ae235b
      if (!class->local_file_set_attributes (vfs, filename,
Packit ae235b
                                             info,
Packit ae235b
                                             flags, cancellable,
Packit ae235b
                                             error))
Packit ae235b
        {
Packit ae235b
          g_object_unref (info);
Packit ae235b
	  return FALSE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (g_file_info_get_attribute_status (info, attribute) == G_FILE_ATTRIBUTE_STATUS_SET)
Packit ae235b
        {
Packit ae235b
          g_object_unref (info);
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_object_unref (info);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
Packit ae235b
	       _("Setting attribute %s not supported"), attribute);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
_g_local_file_info_set_attributes  (char                 *filename,
Packit ae235b
				    GFileInfo            *info,
Packit ae235b
				    GFileQueryInfoFlags   flags,
Packit ae235b
				    GCancellable         *cancellable,
Packit ae235b
				    GError              **error)
Packit ae235b
{
Packit ae235b
  GFileAttributeValue *value;
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  GFileAttributeValue *uid, *gid;
Packit ae235b
#ifdef HAVE_UTIMES
Packit ae235b
  GFileAttributeValue *mtime, *mtime_usec, *atime, *atime_usec;
Packit ae235b
#endif
Packit ae235b
  GFileAttributeStatus status;
Packit ae235b
#endif
Packit ae235b
  gboolean res;
Packit ae235b
  GVfsClass *class;
Packit ae235b
  GVfs *vfs;
Packit ae235b
  
Packit ae235b
  /* Handles setting multiple specified data in a single set, and takes care
Packit ae235b
     of ordering restrictions when setting attributes */
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
Packit ae235b
  /* Set symlink first, since this recreates the file */
Packit ae235b
#ifdef HAVE_SYMLINK
Packit ae235b
  value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
Packit ae235b
  if (value)
Packit ae235b
    {
Packit ae235b
      if (!set_symlink (filename, value, error))
Packit ae235b
	{
Packit ae235b
	  value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
Packit ae235b
	  res = FALSE;
Packit ae235b
	  /* Don't set error multiple times */
Packit ae235b
	  error = NULL;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	value->status = G_FILE_ATTRIBUTE_STATUS_SET;
Packit ae235b
	
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  /* Group uid and gid setting into one call
Packit ae235b
   * Change ownership before permissions, since ownership changes can
Packit ae235b
     change permissions (e.g. setuid)
Packit ae235b
   */
Packit ae235b
  uid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_UID);
Packit ae235b
  gid = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_GID);
Packit ae235b
  
Packit ae235b
  if (uid || gid)
Packit ae235b
    {
Packit ae235b
      if (!set_unix_uid_gid (filename, uid, gid, flags, error))
Packit ae235b
	{
Packit ae235b
	  status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
Packit ae235b
	  res = FALSE;
Packit ae235b
	  /* Don't set error multiple times */
Packit ae235b
	  error = NULL;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	status = G_FILE_ATTRIBUTE_STATUS_SET;
Packit ae235b
      if (uid)
Packit ae235b
	uid->status = status;
Packit ae235b
      if (gid)
Packit ae235b
	gid->status = status;
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
  value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_UNIX_MODE);
Packit ae235b
  if (value)
Packit ae235b
    {
Packit ae235b
      if (!set_unix_mode (filename, flags, value, error))
Packit ae235b
	{
Packit ae235b
	  value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
Packit ae235b
	  res = FALSE;
Packit ae235b
	  /* Don't set error multiple times */
Packit ae235b
	  error = NULL;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	value->status = G_FILE_ATTRIBUTE_STATUS_SET;
Packit ae235b
	
Packit ae235b
    }
Packit ae235b
Packit ae235b
#ifdef HAVE_UTIMES
Packit ae235b
  /* Group all time settings into one call
Packit ae235b
   * Change times as the last thing to avoid it changing due to metadata changes
Packit ae235b
   */
Packit ae235b
  
Packit ae235b
  mtime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
Packit ae235b
  mtime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
Packit ae235b
  atime = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS);
Packit ae235b
  atime_usec = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_TIME_ACCESS_USEC);
Packit ae235b
Packit ae235b
  if (mtime || mtime_usec || atime || atime_usec)
Packit ae235b
    {
Packit ae235b
      if (!set_mtime_atime (filename, mtime, mtime_usec, atime, atime_usec, error))
Packit ae235b
	{
Packit ae235b
	  status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
Packit ae235b
	  res = FALSE;
Packit ae235b
	  /* Don't set error multiple times */
Packit ae235b
	  error = NULL;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	status = G_FILE_ATTRIBUTE_STATUS_SET;
Packit ae235b
      
Packit ae235b
      if (mtime)
Packit ae235b
	mtime->status = status;
Packit ae235b
      if (mtime_usec)
Packit ae235b
	mtime_usec->status = status;
Packit ae235b
      if (atime)
Packit ae235b
	atime->status = status;
Packit ae235b
      if (atime_usec)
Packit ae235b
	atime_usec->status = status;
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  /* xattrs are handled by default callback */
Packit ae235b
Packit ae235b
Packit ae235b
  /*  SELinux context */
Packit ae235b
#ifdef HAVE_SELINUX 
Packit ae235b
  if (is_selinux_enabled ()) {
Packit ae235b
    value = _g_file_info_get_attribute_value (info, G_FILE_ATTRIBUTE_SELINUX_CONTEXT);
Packit ae235b
    if (value)
Packit ae235b
    {
Packit ae235b
      if (!set_selinux_context (filename, value, error))
Packit ae235b
        {
Packit ae235b
          value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
Packit ae235b
          res = FALSE;
Packit ae235b
          /* Don't set error multiple times */
Packit ae235b
          error = NULL;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        value->status = G_FILE_ATTRIBUTE_STATUS_SET;
Packit ae235b
    }
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  vfs = g_vfs_get_default ();
Packit ae235b
  class = G_VFS_GET_CLASS (vfs);
Packit ae235b
  if (class->local_file_set_attributes)
Packit ae235b
    {
Packit ae235b
      if (!class->local_file_set_attributes (vfs, filename,
Packit ae235b
                                             info,
Packit ae235b
                                             flags, cancellable,
Packit ae235b
                                             error))
Packit ae235b
        {
Packit ae235b
	  res = FALSE;
Packit ae235b
	  /* Don't set error multiple times */
Packit ae235b
	  error = NULL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}