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;
49 /* owner should be uid_t but it breaks compliance with MS-Windows */
54 GDestroyNotify free_extra_data;
55 } GLocalParentFileInfo;
58 #define GLocalFileStat struct statx
62 G_LOCAL_FILE_STAT_FIELD_TYPE = STATX_TYPE,
63 G_LOCAL_FILE_STAT_FIELD_MODE = STATX_MODE,
64 G_LOCAL_FILE_STAT_FIELD_NLINK = STATX_NLINK,
65 G_LOCAL_FILE_STAT_FIELD_UID = STATX_UID,
66 G_LOCAL_FILE_STAT_FIELD_GID = STATX_GID,
67 G_LOCAL_FILE_STAT_FIELD_ATIME = STATX_ATIME,
68 G_LOCAL_FILE_STAT_FIELD_MTIME = STATX_MTIME,
69 G_LOCAL_FILE_STAT_FIELD_CTIME = STATX_CTIME,
70 G_LOCAL_FILE_STAT_FIELD_INO = STATX_INO,
71 G_LOCAL_FILE_STAT_FIELD_SIZE = STATX_SIZE,
72 G_LOCAL_FILE_STAT_FIELD_BLOCKS = STATX_BLOCKS,
73 G_LOCAL_FILE_STAT_FIELD_BTIME = STATX_BTIME,
74 } GLocalFileStatField;
76 #define G_LOCAL_FILE_STAT_FIELD_BASIC_STATS STATX_BASIC_STATS
77 #define G_LOCAL_FILE_STAT_FIELD_ALL STATX_ALL
80 g_local_file_statx (int dirfd,
83 GLocalFileStatField mask,
84 GLocalFileStatField mask_required,
85 GLocalFileStat *stat_buf)
89 /* Allow the caller to set mask_required==G_LOCAL_FILE_STAT_FIELD_ALL as a
90 * shortcut for saying it’s equal to @mask. */
91 mask_required &= mask;
93 retval = statx (dirfd, pathname, flags, mask, stat_buf);
94 if (retval == 0 && (stat_buf->stx_mask & mask_required) != mask_required)
96 /* Not all required fields could be returned. */
105 g_local_file_fstat (int fd,
106 GLocalFileStatField mask,
107 GLocalFileStatField mask_required,
108 GLocalFileStat *stat_buf)
110 return g_local_file_statx (fd, "", AT_EMPTY_PATH, mask, mask_required, stat_buf);
114 g_local_file_fstatat (int fd,
117 GLocalFileStatField mask,
118 GLocalFileStatField mask_required,
119 GLocalFileStat *stat_buf)
121 return g_local_file_statx (fd, path, flags, mask, mask_required, stat_buf);
125 g_local_file_lstat (const char *path,
126 GLocalFileStatField mask,
127 GLocalFileStatField mask_required,
128 GLocalFileStat *stat_buf)
130 return g_local_file_statx (AT_FDCWD, path,
131 AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW | AT_STATX_SYNC_AS_STAT,
132 mask, mask_required, stat_buf);
136 g_local_file_stat (const char *path,
137 GLocalFileStatField mask,
138 GLocalFileStatField mask_required,
139 GLocalFileStat *stat_buf)
141 return g_local_file_statx (AT_FDCWD, path,
142 AT_NO_AUTOMOUNT | AT_STATX_SYNC_AS_STAT,
143 mask, mask_required, stat_buf);
146 inline static gboolean _g_stat_has_field (const GLocalFileStat *buf, GLocalFileStatField field) { return buf->stx_mask & field; }
148 inline static guint16 _g_stat_mode (const GLocalFileStat *buf) { return buf->stx_mode; }
149 inline static guint32 _g_stat_nlink (const GLocalFileStat *buf) { return buf->stx_nlink; }
150 inline static dev_t _g_stat_dev (const GLocalFileStat *buf) { return makedev (buf->stx_dev_major, buf->stx_dev_minor); }
151 inline static guint64 _g_stat_ino (const GLocalFileStat *buf) { return buf->stx_ino; }
152 inline static guint64 _g_stat_size (const GLocalFileStat *buf) { return buf->stx_size; }
154 inline static guint32 _g_stat_uid (const GLocalFileStat *buf) { return buf->stx_uid; }
155 inline static guint32 _g_stat_gid (const GLocalFileStat *buf) { return buf->stx_gid; }
156 inline static dev_t _g_stat_rdev (const GLocalFileStat *buf) { return makedev (buf->stx_rdev_major, buf->stx_rdev_minor); }
157 inline static guint32 _g_stat_blksize (const GLocalFileStat *buf) { return buf->stx_blksize; }
159 inline static guint64 _g_stat_blocks (const GLocalFileStat *buf) { return buf->stx_blocks; }
161 inline static gint64 _g_stat_atime (const GLocalFileStat *buf) { return buf->stx_atime.tv_sec; }
162 inline static gint64 _g_stat_ctime (const GLocalFileStat *buf) { return buf->stx_ctime.tv_sec; }
163 inline static gint64 _g_stat_mtime (const GLocalFileStat *buf) { return buf->stx_mtime.tv_sec; }
164 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return buf->stx_atime.tv_nsec; }
165 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return buf->stx_ctime.tv_nsec; }
166 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return buf->stx_mtime.tv_nsec; }
168 #else /* if !HAVE_STATX */
171 /* We want 64-bit file size, file ID and symlink support */
172 #define GLocalFileStat GWin32PrivateStat
174 #define GLocalFileStat struct stat
177 /* If the system doesn’t have statx() support, emulate it using traditional
178 * stat(). It supports fields %G_LOCAL_FILE_STAT_FIELD_BASIC_STATS only, and
179 * always returns all of them. */
182 G_LOCAL_FILE_STAT_FIELD_TYPE = (1 << 0),
183 G_LOCAL_FILE_STAT_FIELD_MODE = (1 << 1),
184 G_LOCAL_FILE_STAT_FIELD_NLINK = (1 << 2),
185 G_LOCAL_FILE_STAT_FIELD_UID = (1 << 3),
186 G_LOCAL_FILE_STAT_FIELD_GID = (1 << 4),
187 G_LOCAL_FILE_STAT_FIELD_ATIME = (1 << 5),
188 G_LOCAL_FILE_STAT_FIELD_MTIME = (1 << 6),
189 G_LOCAL_FILE_STAT_FIELD_CTIME = (1 << 7),
190 G_LOCAL_FILE_STAT_FIELD_INO = (1 << 8),
191 G_LOCAL_FILE_STAT_FIELD_SIZE = (1 << 9),
192 G_LOCAL_FILE_STAT_FIELD_BLOCKS = (1 << 10),
193 G_LOCAL_FILE_STAT_FIELD_BTIME = (1 << 11),
194 } GLocalFileStatField;
196 #define G_LOCAL_FILE_STAT_FIELD_BASIC_STATS \
197 (G_LOCAL_FILE_STAT_FIELD_TYPE | G_LOCAL_FILE_STAT_FIELD_MODE | \
198 G_LOCAL_FILE_STAT_FIELD_NLINK | G_LOCAL_FILE_STAT_FIELD_UID | \
199 G_LOCAL_FILE_STAT_FIELD_GID | G_LOCAL_FILE_STAT_FIELD_ATIME | \
200 G_LOCAL_FILE_STAT_FIELD_MTIME | G_LOCAL_FILE_STAT_FIELD_CTIME | \
201 G_LOCAL_FILE_STAT_FIELD_INO | G_LOCAL_FILE_STAT_FIELD_SIZE | \
202 G_LOCAL_FILE_STAT_FIELD_BLOCKS)
203 #define G_LOCAL_FILE_STAT_FIELD_ALL (G_LOCAL_FILE_STAT_FIELD_BASIC_STATS | G_LOCAL_FILE_STAT_FIELD_BTIME)
206 g_local_file_fstat (int fd,
207 GLocalFileStatField mask,
208 GLocalFileStatField mask_required,
209 GLocalFileStat *stat_buf)
211 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
213 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
219 return GLIB_PRIVATE_CALL (g_win32_fstat) (fd, stat_buf);
221 return fstat (fd, stat_buf);
226 g_local_file_fstatat (int fd,
229 GLocalFileStatField mask,
230 GLocalFileStatField mask_required,
231 GLocalFileStat *stat_buf)
233 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
235 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
240 #if !defined(G_OS_WIN32) && defined(AT_FDCWD)
241 return fstatat (fd, path, stat_buf, flags);
243 /* Currently not supported on Windows or macOS < 10.10 */
250 g_local_file_lstat (const char *path,
251 GLocalFileStatField mask,
252 GLocalFileStatField mask_required,
253 GLocalFileStat *stat_buf)
255 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
257 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
263 return GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, stat_buf);
265 return g_lstat (path, stat_buf);
270 g_local_file_stat (const char *path,
271 GLocalFileStatField mask,
272 GLocalFileStatField mask_required,
273 GLocalFileStat *stat_buf)
275 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
277 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
283 return GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, stat_buf);
285 return stat (path, stat_buf);
289 inline static gboolean _g_stat_has_field (const GLocalFileStat *buf, GLocalFileStatField field) { return (G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & field) == field; }
292 inline static mode_t _g_stat_mode (const GLocalFileStat *buf) { return buf->st_mode; }
293 inline static nlink_t _g_stat_nlink (const GLocalFileStat *buf) { return buf->st_nlink; }
295 inline static guint16 _g_stat_mode (const GLocalFileStat *buf) { return buf->st_mode; }
296 inline static guint32 _g_stat_nlink (const GLocalFileStat *buf) { return buf->st_nlink; }
298 inline static dev_t _g_stat_dev (const GLocalFileStat *buf) { return buf->st_dev; }
299 inline static ino_t _g_stat_ino (const GLocalFileStat *buf) { return buf->st_ino; }
300 inline static off_t _g_stat_size (const GLocalFileStat *buf) { return buf->st_size; }
303 inline static uid_t _g_stat_uid (const GLocalFileStat *buf) { return buf->st_uid; }
304 inline static gid_t _g_stat_gid (const GLocalFileStat *buf) { return buf->st_gid; }
305 inline static dev_t _g_stat_rdev (const GLocalFileStat *buf) { return buf->st_rdev; }
306 inline static blksize_t _g_stat_blksize (const GLocalFileStat *buf) { return buf->st_blksize; }
308 inline static guint16 _g_stat_uid (const GLocalFileStat *buf) { return buf->st_uid; }
309 inline static guint16 _g_stat_gid (const GLocalFileStat *buf) { return buf->st_gid; }
312 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
313 inline static blkcnt_t _g_stat_blocks (const GLocalFileStat *buf) { return buf->st_blocks; }
317 inline static time_t _g_stat_atime (const GLocalFileStat *buf) { return buf->st_atime; }
318 inline static time_t _g_stat_ctime (const GLocalFileStat *buf) { return buf->st_ctime; }
319 inline static time_t _g_stat_mtime (const GLocalFileStat *buf) { return buf->st_mtime; }
321 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
322 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return buf->st_atim.tv_nsec; }
323 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return buf->st_ctim.tv_nsec; }
324 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return buf->st_mtim.tv_nsec; }
327 #endif /* !HAVE_STATX */
329 #define G_LOCAL_FILE_INFO_NOSTAT_ATTRIBUTES \
330 G_FILE_ATTRIBUTE_STANDARD_NAME "," \
331 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," \
332 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME "," \
333 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME
335 gboolean _g_local_file_has_trash_dir (const char *dirname,
338 gboolean _g_local_file_is_lost_found_dir (const char *path,
341 void _g_local_file_info_get_parent_info (const char *dir,
342 GFileAttributeMatcher *attribute_matcher,
343 GLocalParentFileInfo *parent_info);
344 void _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info);
345 void _g_local_file_info_get_nostat (GFileInfo *info,
346 const char *basename,
348 GFileAttributeMatcher *attribute_matcher);
349 GFileInfo *_g_local_file_info_get (const char *basename,
351 GFileAttributeMatcher *attribute_matcher,
352 GFileQueryInfoFlags flags,
353 GLocalParentFileInfo *parent_info,
355 GFileInfo *_g_local_file_info_get_from_fd (int fd,
356 const char *attributes,
358 char * _g_local_file_info_create_etag (GLocalFileStat *statbuf);
359 gboolean _g_local_file_info_set_attribute (char *filename,
360 const char *attribute,
361 GFileAttributeType type,
363 GFileQueryInfoFlags flags,
364 GCancellable *cancellable,
366 gboolean _g_local_file_info_set_attributes (char *filename,
368 GFileQueryInfoFlags flags,
369 GCancellable *cancellable,
374 #endif /* __G_FILE_LOCAL_FILE_INFO_H__ */