1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #ifndef __G_LOCAL_FILE_INFO_H__
24 #define __G_LOCAL_FILE_INFO_H__
26 /* Needed for statx() */
32 #include <gio/gfileinfo.h>
33 #include <gio/gfile.h>
34 #include <glib/glib-private.h>
35 #include <glib/gstdio.h>
36 #include <glib/gstdioprivate.h>
38 #include <sys/types.h>
41 #include <sys/sysmacros.h>
50 gboolean has_trash_dir;
51 /* owner should be uid_t but it breaks compliance with MS-Windows */
56 GDestroyNotify free_extra_data;
57 } GLocalParentFileInfo;
60 #define GLocalFileStat struct statx
64 G_LOCAL_FILE_STAT_FIELD_TYPE = STATX_TYPE,
65 G_LOCAL_FILE_STAT_FIELD_MODE = STATX_MODE,
66 G_LOCAL_FILE_STAT_FIELD_NLINK = STATX_NLINK,
67 G_LOCAL_FILE_STAT_FIELD_UID = STATX_UID,
68 G_LOCAL_FILE_STAT_FIELD_GID = STATX_GID,
69 G_LOCAL_FILE_STAT_FIELD_ATIME = STATX_ATIME,
70 G_LOCAL_FILE_STAT_FIELD_MTIME = STATX_MTIME,
71 G_LOCAL_FILE_STAT_FIELD_CTIME = STATX_CTIME,
72 G_LOCAL_FILE_STAT_FIELD_INO = STATX_INO,
73 G_LOCAL_FILE_STAT_FIELD_SIZE = STATX_SIZE,
74 G_LOCAL_FILE_STAT_FIELD_BLOCKS = STATX_BLOCKS,
75 G_LOCAL_FILE_STAT_FIELD_BTIME = STATX_BTIME,
76 } GLocalFileStatField;
78 #define G_LOCAL_FILE_STAT_FIELD_BASIC_STATS STATX_BASIC_STATS
79 #define G_LOCAL_FILE_STAT_FIELD_ALL STATX_ALL
82 g_local_file_statx (int dirfd,
85 GLocalFileStatField mask,
86 GLocalFileStatField mask_required,
87 GLocalFileStat *stat_buf)
91 /* Allow the caller to set mask_required==G_LOCAL_FILE_STAT_FIELD_ALL as a
92 * shortcut for saying it’s equal to @mask. */
93 mask_required &= mask;
95 retval = statx (dirfd, pathname, flags, mask, stat_buf);
96 if (retval == 0 && (stat_buf->stx_mask & mask_required) != mask_required)
98 /* Not all required fields could be returned. */
107 g_local_file_fstat (int fd,
108 GLocalFileStatField mask,
109 GLocalFileStatField mask_required,
110 GLocalFileStat *stat_buf)
112 return g_local_file_statx (fd, "", AT_EMPTY_PATH, mask, mask_required, stat_buf);
116 g_local_file_fstatat (int fd,
119 GLocalFileStatField mask,
120 GLocalFileStatField mask_required,
121 GLocalFileStat *stat_buf)
123 return g_local_file_statx (fd, path, flags, mask, mask_required, stat_buf);
127 g_local_file_lstat (const char *path,
128 GLocalFileStatField mask,
129 GLocalFileStatField mask_required,
130 GLocalFileStat *stat_buf)
132 return g_local_file_statx (AT_FDCWD, path,
133 AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW | AT_STATX_SYNC_AS_STAT,
134 mask, mask_required, stat_buf);
138 g_local_file_stat (const char *path,
139 GLocalFileStatField mask,
140 GLocalFileStatField mask_required,
141 GLocalFileStat *stat_buf)
143 return g_local_file_statx (AT_FDCWD, path,
144 AT_NO_AUTOMOUNT | AT_STATX_SYNC_AS_STAT,
145 mask, mask_required, stat_buf);
148 inline static gboolean _g_stat_has_field (const GLocalFileStat *buf, GLocalFileStatField field) { return buf->stx_mask & field; }
150 inline static guint16 _g_stat_mode (const GLocalFileStat *buf) { return buf->stx_mode; }
151 inline static guint32 _g_stat_nlink (const GLocalFileStat *buf) { return buf->stx_nlink; }
152 inline static dev_t _g_stat_dev (const GLocalFileStat *buf) { return makedev (buf->stx_dev_major, buf->stx_dev_minor); }
153 inline static guint64 _g_stat_ino (const GLocalFileStat *buf) { return buf->stx_ino; }
154 inline static guint64 _g_stat_size (const GLocalFileStat *buf) { return buf->stx_size; }
156 inline static guint32 _g_stat_uid (const GLocalFileStat *buf) { return buf->stx_uid; }
157 inline static guint32 _g_stat_gid (const GLocalFileStat *buf) { return buf->stx_gid; }
158 inline static dev_t _g_stat_rdev (const GLocalFileStat *buf) { return makedev (buf->stx_rdev_major, buf->stx_rdev_minor); }
159 inline static guint32 _g_stat_blksize (const GLocalFileStat *buf) { return buf->stx_blksize; }
161 inline static guint64 _g_stat_blocks (const GLocalFileStat *buf) { return buf->stx_blocks; }
163 inline static gint64 _g_stat_atime (const GLocalFileStat *buf) { return buf->stx_atime.tv_sec; }
164 inline static gint64 _g_stat_ctime (const GLocalFileStat *buf) { return buf->stx_ctime.tv_sec; }
165 inline static gint64 _g_stat_mtime (const GLocalFileStat *buf) { return buf->stx_mtime.tv_sec; }
166 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return buf->stx_atime.tv_nsec; }
167 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return buf->stx_ctime.tv_nsec; }
168 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return buf->stx_mtime.tv_nsec; }
170 #else /* if !HAVE_STATX */
173 /* We want 64-bit file size, file ID and symlink support */
174 #define GLocalFileStat GWin32PrivateStat
176 #define GLocalFileStat struct stat
179 /* If the system doesn’t have statx() support, emulate it using traditional
180 * stat(). It supports fields %G_LOCAL_FILE_STAT_FIELD_BASIC_STATS only, and
181 * always returns all of them. */
184 G_LOCAL_FILE_STAT_FIELD_TYPE = (1 << 0),
185 G_LOCAL_FILE_STAT_FIELD_MODE = (1 << 1),
186 G_LOCAL_FILE_STAT_FIELD_NLINK = (1 << 2),
187 G_LOCAL_FILE_STAT_FIELD_UID = (1 << 3),
188 G_LOCAL_FILE_STAT_FIELD_GID = (1 << 4),
189 G_LOCAL_FILE_STAT_FIELD_ATIME = (1 << 5),
190 G_LOCAL_FILE_STAT_FIELD_MTIME = (1 << 6),
191 G_LOCAL_FILE_STAT_FIELD_CTIME = (1 << 7),
192 G_LOCAL_FILE_STAT_FIELD_INO = (1 << 8),
193 G_LOCAL_FILE_STAT_FIELD_SIZE = (1 << 9),
194 G_LOCAL_FILE_STAT_FIELD_BLOCKS = (1 << 10),
195 G_LOCAL_FILE_STAT_FIELD_BTIME = (1 << 11),
196 } GLocalFileStatField;
198 #define G_LOCAL_FILE_STAT_FIELD_BASIC_STATS \
199 (G_LOCAL_FILE_STAT_FIELD_TYPE | G_LOCAL_FILE_STAT_FIELD_MODE | \
200 G_LOCAL_FILE_STAT_FIELD_NLINK | G_LOCAL_FILE_STAT_FIELD_UID | \
201 G_LOCAL_FILE_STAT_FIELD_GID | G_LOCAL_FILE_STAT_FIELD_ATIME | \
202 G_LOCAL_FILE_STAT_FIELD_MTIME | G_LOCAL_FILE_STAT_FIELD_CTIME | \
203 G_LOCAL_FILE_STAT_FIELD_INO | G_LOCAL_FILE_STAT_FIELD_SIZE | \
204 G_LOCAL_FILE_STAT_FIELD_BLOCKS)
205 #define G_LOCAL_FILE_STAT_FIELD_ALL (G_LOCAL_FILE_STAT_FIELD_BASIC_STATS | G_LOCAL_FILE_STAT_FIELD_BTIME)
208 g_local_file_fstat (int fd,
209 GLocalFileStatField mask,
210 GLocalFileStatField mask_required,
211 GLocalFileStat *stat_buf)
213 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
215 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
221 return GLIB_PRIVATE_CALL (g_win32_fstat) (fd, stat_buf);
223 return fstat (fd, stat_buf);
228 g_local_file_fstatat (int fd,
231 GLocalFileStatField mask,
232 GLocalFileStatField mask_required,
233 GLocalFileStat *stat_buf)
235 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
237 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
242 #if !defined(G_OS_WIN32) && defined(AT_FDCWD)
243 return fstatat (fd, path, stat_buf, flags);
245 /* Currently not supported on Windows or macOS < 10.10 */
252 g_local_file_lstat (const char *path,
253 GLocalFileStatField mask,
254 GLocalFileStatField mask_required,
255 GLocalFileStat *stat_buf)
257 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
259 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
265 return GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, stat_buf);
267 return g_lstat (path, stat_buf);
272 g_local_file_stat (const char *path,
273 GLocalFileStatField mask,
274 GLocalFileStatField mask_required,
275 GLocalFileStat *stat_buf)
277 if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
279 /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
285 return GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, stat_buf);
287 return stat (path, stat_buf);
291 inline static gboolean _g_stat_has_field (const GLocalFileStat *buf, GLocalFileStatField field) { return (G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & field) == field; }
294 inline static mode_t _g_stat_mode (const GLocalFileStat *buf) { return buf->st_mode; }
295 inline static nlink_t _g_stat_nlink (const GLocalFileStat *buf) { return buf->st_nlink; }
297 inline static guint16 _g_stat_mode (const GLocalFileStat *buf) { return buf->st_mode; }
298 inline static guint32 _g_stat_nlink (const GLocalFileStat *buf) { return buf->st_nlink; }
300 inline static dev_t _g_stat_dev (const GLocalFileStat *buf) { return buf->st_dev; }
301 inline static ino_t _g_stat_ino (const GLocalFileStat *buf) { return buf->st_ino; }
302 inline static goffset _g_stat_size (const GLocalFileStat *buf) { return buf->st_size; }
305 inline static uid_t _g_stat_uid (const GLocalFileStat *buf) { return buf->st_uid; }
306 inline static gid_t _g_stat_gid (const GLocalFileStat *buf) { return buf->st_gid; }
307 inline static dev_t _g_stat_rdev (const GLocalFileStat *buf) { return buf->st_rdev; }
308 inline static blksize_t _g_stat_blksize (const GLocalFileStat *buf) { return buf->st_blksize; }
310 inline static guint16 _g_stat_uid (const GLocalFileStat *buf) { return buf->st_uid; }
311 inline static guint16 _g_stat_gid (const GLocalFileStat *buf) { return buf->st_gid; }
314 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
315 inline static blkcnt_t _g_stat_blocks (const GLocalFileStat *buf) { return buf->st_blocks; }
319 inline static guint64 _g_stat_atime (const GLocalFileStat *buf) { return buf->st_atime; }
320 inline static guint64 _g_stat_ctime (const GLocalFileStat *buf) { return buf->st_ctime; }
321 inline static guint64 _g_stat_mtime (const GLocalFileStat *buf) { return buf->st_mtime; }
323 inline static guint64 _g_stat_atime (const GLocalFileStat *buf) { return buf->st_atim.tv_sec; }
324 inline static guint64 _g_stat_ctime (const GLocalFileStat *buf) { return buf->st_ctim.tv_sec; }
325 inline static guint64 _g_stat_mtime (const GLocalFileStat *buf) { return buf->st_mtim.tv_sec; }
327 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) || defined(G_OS_WIN32)
328 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return buf->st_atim.tv_nsec; }
329 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return buf->st_ctim.tv_nsec; }
330 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return buf->st_mtim.tv_nsec; }
331 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
332 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return buf->st_atimensec; }
333 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return buf->st_ctimensec; }
334 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return buf->st_mtimensec; }
336 inline static guint32 _g_stat_atim_nsec (const GLocalFileStat *buf) { return 0; }
337 inline static guint32 _g_stat_ctim_nsec (const GLocalFileStat *buf) { return 0; }
338 inline static guint32 _g_stat_mtim_nsec (const GLocalFileStat *buf) { return 0; }
341 #endif /* !HAVE_STATX */
343 #define G_LOCAL_FILE_INFO_NOSTAT_ATTRIBUTES \
344 G_FILE_ATTRIBUTE_STANDARD_NAME "," \
345 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," \
346 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME "," \
347 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME
349 gboolean _g_local_file_has_trash_dir (const char *dirname,
352 gboolean _g_local_file_is_lost_found_dir (const char *path,
355 void _g_local_file_info_get_parent_info (const char *dir,
356 GFileAttributeMatcher *attribute_matcher,
357 GLocalParentFileInfo *parent_info);
358 void _g_local_file_info_free_parent_info (GLocalParentFileInfo *parent_info);
359 void _g_local_file_info_get_nostat (GFileInfo *info,
360 const char *basename,
362 GFileAttributeMatcher *attribute_matcher);
363 GFileInfo *_g_local_file_info_get (const char *basename,
365 GFileAttributeMatcher *attribute_matcher,
366 GFileQueryInfoFlags flags,
367 GLocalParentFileInfo *parent_info,
369 GFileInfo *_g_local_file_info_get_from_fd (int fd,
370 const char *attributes,
372 char * _g_local_file_info_create_etag (GLocalFileStat *statbuf);
373 gboolean _g_local_file_info_set_attribute (char *filename,
374 const char *attribute,
375 GFileAttributeType type,
377 GFileQueryInfoFlags flags,
378 GCancellable *cancellable,
380 gboolean _g_local_file_info_set_attributes (char *filename,
382 GFileQueryInfoFlags flags,
383 GCancellable *cancellable,
388 #endif /* __G_FILE_LOCAL_FILE_INFO_H__ */