1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #ifndef __G_LOCAL_FILE_INFO_H__
22 #define __G_LOCAL_FILE_INFO_H__
24 /* Needed for statx() */
30 #include <gio/gfileinfo.h>
31 #include <gio/gfile.h>
32 #include <glib/glib-private.h>
33 #include <glib/gstdio.h>
34 #include <glib/gstdioprivate.h>
36 #include <sys/types.h>
39 #include <sys/sysmacros.h>
48 gboolean has_trash_dir;
53 GDestroyNotify free_extra_data;
54 } GLocalParentFileInfo;
57 #define GLocalFileStat struct statx
61 G_LOCAL_FILE_STAT_FIELD_TYPE = STATX_TYPE,
62 G_LOCAL_FILE_STAT_FIELD_MODE = STATX_MODE,
63 G_LOCAL_FILE_STAT_FIELD_NLINK = STATX_NLINK,
64 G_LOCAL_FILE_STAT_FIELD_UID = STATX_UID,
65 G_LOCAL_FILE_STAT_FIELD_GID = STATX_GID,
66 G_LOCAL_FILE_STAT_FIELD_ATIME = STATX_ATIME,
67 G_LOCAL_FILE_STAT_FIELD_MTIME = STATX_MTIME,
68 G_LOCAL_FILE_STAT_FIELD_CTIME = STATX_CTIME,
69 G_LOCAL_FILE_STAT_FIELD_INO = STATX_INO,
70 G_LOCAL_FILE_STAT_FIELD_SIZE = STATX_SIZE,
71 G_LOCAL_FILE_STAT_FIELD_BLOCKS = STATX_BLOCKS,
72 G_LOCAL_FILE_STAT_FIELD_BTIME = STATX_BTIME,
73 } GLocalFileStatField;
75 #define G_LOCAL_FILE_STAT_FIELD_BASIC_STATS STATX_BASIC_STATS
76 #define G_LOCAL_FILE_STAT_FIELD_ALL STATX_ALL
79 g_local_file_statx (int dirfd,
82 GLocalFileStatField mask,
83 GLocalFileStatField mask_required,
84 GLocalFileStat *stat_buf)
88 /* Allow the caller to set mask_required==G_LOCAL_FILE_STAT_FIELD_ALL as a
89 * shortcut for saying it’s equal to @mask. */
90 mask_required &= mask;
92 retval = statx (dirfd, pathname, flags, mask, stat_buf);
93 if (retval == 0 && (stat_buf->stx_mask & mask_required) != mask_required)
95 /* Not all required fields could be returned. */
104 g_local_file_fstat (int fd,
105 GLocalFileStatField mask,
106 GLocalFileStatField mask_required,
107 GLocalFileStat *stat_buf)
109 return g_local_file_statx (fd, "", AT_EMPTY_PATH, mask, mask_required, stat_buf);
113 g_local_file_fstatat (int fd,
116 GLocalFileStatField mask,
117 GLocalFileStatField mask_required,
118 GLocalFileStat *stat_buf)
120 return g_local_file_statx (fd, path, flags, mask, mask_required, stat_buf);
124 g_local_file_lstat (const char *path,
125 GLocalFileStatField mask,
126 GLocalFileStatField mask_required,
127 GLocalFileStat *stat_buf)
129 return g_local_file_statx (AT_FDCWD, path,
130 AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW | AT_STATX_SYNC_AS_STAT,
131 mask, mask_required, stat_buf);
135 g_local_file_stat (const char *path,
136 GLocalFileStatField mask,
137 GLocalFileStatField mask_required,
138 GLocalFileStat *stat_buf)
140 return g_local_file_statx (AT_FDCWD, path,
141 AT_NO_AUTOMOUNT | AT_STATX_SYNC_AS_STAT,
142 mask, mask_required, stat_buf);
145 inline static gboolean _g_stat_has_field (const GLocalFileStat *buf, GLocalFileStatField field) { return buf->stx_mask & field; }
147 inline static guint16 _g_stat_mode (const GLocalFileStat *buf) { return buf->stx_mode; }
148 inline static guint32 _g_stat_nlink (const GLocalFileStat *buf) { return buf->stx_nlink; }
149 inline static dev_t _g_stat_dev (const GLocalFileStat *buf) { return makedev (buf->stx_dev_major, buf->stx_dev_minor); }
150 inline static guint64 _g_stat_ino (const GLocalFileStat *buf) { return buf->stx_ino; }
151 inline static guint64 _g_stat_size (const GLocalFileStat *buf) { return buf->stx_size; }
153 inline static guint32 _g_stat_uid (const GLocalFileStat *buf) { return buf->stx_uid; }
154 inline static guint32 _g_stat_gid (const GLocalFileStat *buf) { return buf->stx_gid; }
155 inline static dev_t _g_stat_rdev (const GLocalFileStat *buf) { return makedev (buf->stx_rdev_major, buf->stx_rdev_minor); }
156 inline static guint32 _g_stat_blksize (const GLocalFileStat *buf) { return buf->stx_blksize; }
158 inline static guint64 _g_stat_blocks (const GLocalFileStat *buf) { return buf->stx_blocks; }
160 inline static gint64 _g_stat_atime (const GLocalFileStat *buf) { return buf->stx_atime.tv_sec; }
161 inline static gint64 _g_stat_ctime (const GLocalFileStat *buf) { return buf->stx_ctime.tv_sec; }
162 inline static gint64 _g_stat_mtime (const GLocalFileStat *buf) { return buf->stx_mtime.tv_sec; }
163 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return buf->stx_atime.tv_nsec; }
164 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return buf->stx_ctime.tv_nsec; }
165 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return buf->stx_mtime.tv_nsec; }
167 #else /* if !HAVE_STATX */
170 /* We want 64-bit file size, file ID and symlink support */
171 #define GLocalFileStat GWin32PrivateStat
173 #define GLocalFileStat struct stat
176 /* If the system doesn’t have statx() support, emulate it using traditional
177 * stat(). It supports fields %G_LOCAL_FILE_STAT_FIELD_BASIC_STATS only, and
178 * always returns all of them. */
181 G_LOCAL_FILE_STAT_FIELD_TYPE = (1 << 0),
182 G_LOCAL_FILE_STAT_FIELD_MODE = (1 << 1),
183 G_LOCAL_FILE_STAT_FIELD_NLINK = (1 << 2),
184 G_LOCAL_FILE_STAT_FIELD_UID = (1 << 3),
185 G_LOCAL_FILE_STAT_FIELD_GID = (1 << 4),
186 G_LOCAL_FILE_STAT_FIELD_ATIME = (1 << 5),
187 G_LOCAL_FILE_STAT_FIELD_MTIME = (1 << 6),
188 G_LOCAL_FILE_STAT_FIELD_CTIME = (1 << 7),
189 G_LOCAL_FILE_STAT_FIELD_INO = (1 << 8),
190 G_LOCAL_FILE_STAT_FIELD_SIZE = (1 << 9),
191 G_LOCAL_FILE_STAT_FIELD_BLOCKS = (1 << 10),
192 G_LOCAL_FILE_STAT_FIELD_BTIME = (1 << 11),
193 } GLocalFileStatField;
195 #define G_LOCAL_FILE_STAT_FIELD_BASIC_STATS \
196 (G_LOCAL_FILE_STAT_FIELD_TYPE | G_LOCAL_FILE_STAT_FIELD_MODE | \
197 G_LOCAL_FILE_STAT_FIELD_NLINK | G_LOCAL_FILE_STAT_FIELD_UID | \
198 G_LOCAL_FILE_STAT_FIELD_GID | G_LOCAL_FILE_STAT_FIELD_ATIME | \
199 G_LOCAL_FILE_STAT_FIELD_MTIME | G_LOCAL_FILE_STAT_FIELD_CTIME | \
200 G_LOCAL_FILE_STAT_FIELD_INO | G_LOCAL_FILE_STAT_FIELD_SIZE | \
201 G_LOCAL_FILE_STAT_FIELD_BLOCKS)
202 #define G_LOCAL_FILE_STAT_FIELD_ALL (G_LOCAL_FILE_STAT_FIELD_BASIC_STATS | G_LOCAL_FILE_STAT_FIELD_BTIME)
205 g_local_file_fstat (int fd,
206 GLocalFileStatField mask,
207 GLocalFileStatField mask_required,
208 GLocalFileStat *stat_buf)
210 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
212 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
218 return GLIB_PRIVATE_CALL (g_win32_fstat) (fd, stat_buf);
220 return fstat (fd, stat_buf);
225 g_local_file_fstatat (int fd,
228 GLocalFileStatField mask,
229 GLocalFileStatField mask_required,
230 GLocalFileStat *stat_buf)
232 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
234 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
239 #if !defined(G_OS_WIN32) && defined(AT_FDCWD)
240 return fstatat (fd, path, stat_buf, flags);
242 /* Currently not supported on Windows or macOS < 10.10 */
249 g_local_file_lstat (const char *path,
250 GLocalFileStatField mask,
251 GLocalFileStatField mask_required,
252 GLocalFileStat *stat_buf)
254 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
256 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
262 return GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, stat_buf);
264 return g_lstat (path, stat_buf);
269 g_local_file_stat (const char *path,
270 GLocalFileStatField mask,
271 GLocalFileStatField mask_required,
272 GLocalFileStat *stat_buf)
274 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
276 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
282 return GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, stat_buf);
284 return stat (path, stat_buf);
288 inline static gboolean _g_stat_has_field (const GLocalFileStat *buf, GLocalFileStatField field) { return (G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & field) == field; }
291 inline static mode_t _g_stat_mode (const GLocalFileStat *buf) { return buf->st_mode; }
292 inline static nlink_t _g_stat_nlink (const GLocalFileStat *buf) { return buf->st_nlink; }
294 inline static guint16 _g_stat_mode (const GLocalFileStat *buf) { return buf->st_mode; }
295 inline static guint32 _g_stat_nlink (const GLocalFileStat *buf) { return buf->st_nlink; }
297 inline static dev_t _g_stat_dev (const GLocalFileStat *buf) { return buf->st_dev; }
298 inline static ino_t _g_stat_ino (const GLocalFileStat *buf) { return buf->st_ino; }
299 inline static off_t _g_stat_size (const GLocalFileStat *buf) { return buf->st_size; }
302 inline static uid_t _g_stat_uid (const GLocalFileStat *buf) { return buf->st_uid; }
303 inline static gid_t _g_stat_gid (const GLocalFileStat *buf) { return buf->st_gid; }
304 inline static dev_t _g_stat_rdev (const GLocalFileStat *buf) { return buf->st_rdev; }
305 inline static blksize_t _g_stat_blksize (const GLocalFileStat *buf) { return buf->st_blksize; }
307 inline static guint16 _g_stat_uid (const GLocalFileStat *buf) { return buf->st_uid; }
308 inline static guint16 _g_stat_gid (const GLocalFileStat *buf) { return buf->st_gid; }
311 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
312 inline static blkcnt_t _g_stat_blocks (const GLocalFileStat *buf) { return buf->st_blocks; }
316 inline static time_t _g_stat_atime (const GLocalFileStat *buf) { return buf->st_atime; }
317 inline static time_t _g_stat_ctime (const GLocalFileStat *buf) { return buf->st_ctime; }
318 inline static time_t _g_stat_mtime (const GLocalFileStat *buf) { return buf->st_mtime; }
320 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
321 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return buf->st_atim.tv_nsec; }
322 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return buf->st_ctim.tv_nsec; }
323 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return buf->st_mtim.tv_nsec; }
326 #endif /* !HAVE_STATX */
328 #define G_LOCAL_FILE_INFO_NOSTAT_ATTRIBUTES \
329 G_FILE_ATTRIBUTE_STANDARD_NAME "," \
330 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," \
331 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME "," \
332 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME
334 gboolean _g_local_file_has_trash_dir (const char *dirname,
337 gboolean _g_local_file_is_lost_found_dir (const char *path,
340 void _g_local_file_info_get_parent_info (const char *dir,
341 GFileAttributeMatcher *attribute_matcher,
342 GLocalParentFileInfo *parent_info);
343 void _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info);
344 void _g_local_file_info_get_nostat (GFileInfo *info,
345 const char *basename,
347 GFileAttributeMatcher *attribute_matcher);
348 GFileInfo *_g_local_file_info_get (const char *basename,
350 GFileAttributeMatcher *attribute_matcher,
351 GFileQueryInfoFlags flags,
352 GLocalParentFileInfo *parent_info,
354 GFileInfo *_g_local_file_info_get_from_fd (int fd,
355 const char *attributes,
357 char * _g_local_file_info_create_etag (GLocalFileStat *statbuf);
358 gboolean _g_local_file_info_set_attribute (char *filename,
359 const char *attribute,
360 GFileAttributeType type,
362 GFileQueryInfoFlags flags,
363 GCancellable *cancellable,
365 gboolean _g_local_file_info_set_attributes (char *filename,
367 GFileQueryInfoFlags flags,
368 GCancellable *cancellable,
373 #endif /* __G_FILE_LOCAL_FILE_INFO_H__ */