Imported Upstream version 2.66.6
[platform/upstream/glib.git] / gio / glocalfileinfo.h
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #ifndef __G_LOCAL_FILE_INFO_H__
22 #define __G_LOCAL_FILE_INFO_H__
23
24 /* Needed for statx() */
25 #ifndef _GNU_SOURCE
26 #define _GNU_SOURCE
27 #endif
28
29 #include <fcntl.h>
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>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37
38 #ifdef HAVE_STATX
39 #include <sys/sysmacros.h>
40 #endif
41
42 G_BEGIN_DECLS
43
44 typedef struct
45 {
46   gboolean writable;
47   gboolean is_sticky;
48   gboolean has_trash_dir;
49   int      owner;
50   dev_t    device;
51   ino_t    inode;
52   gpointer extra_data;
53   GDestroyNotify free_extra_data;
54 } GLocalParentFileInfo;
55
56 #ifdef HAVE_STATX
57 #define GLocalFileStat struct statx
58
59 typedef enum
60 {
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;
74
75 #define G_LOCAL_FILE_STAT_FIELD_BASIC_STATS STATX_BASIC_STATS
76 #define G_LOCAL_FILE_STAT_FIELD_ALL STATX_ALL
77
78 static inline int
79 g_local_file_statx (int                  dirfd,
80                     const char          *pathname,
81                     int                  flags,
82                     GLocalFileStatField  mask,
83                     GLocalFileStatField  mask_required,
84                     GLocalFileStat      *stat_buf)
85 {
86   int retval;
87
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;
91
92   retval = statx (dirfd, pathname, flags, mask, stat_buf);
93   if (retval == 0 && (stat_buf->stx_mask & mask_required) != mask_required)
94     {
95       /* Not all required fields could be returned. */
96       errno = ERANGE;
97       return -1;
98     }
99
100   return retval;
101 }
102
103 static inline int
104 g_local_file_fstat (int                  fd,
105                     GLocalFileStatField  mask,
106                     GLocalFileStatField  mask_required,
107                     GLocalFileStat      *stat_buf)
108 {
109   return g_local_file_statx (fd, "", AT_EMPTY_PATH, mask, mask_required, stat_buf);
110 }
111
112 static inline int
113 g_local_file_fstatat (int                  fd,
114                       const char          *path,
115                       int                  flags,
116                       GLocalFileStatField  mask,
117                       GLocalFileStatField  mask_required,
118                       GLocalFileStat      *stat_buf)
119 {
120   return g_local_file_statx (fd, path, flags, mask, mask_required, stat_buf);
121 }
122
123 static inline int
124 g_local_file_lstat (const char          *path,
125                     GLocalFileStatField  mask,
126                     GLocalFileStatField  mask_required,
127                     GLocalFileStat      *stat_buf)
128 {
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);
132 }
133
134 static inline int
135 g_local_file_stat (const char          *path,
136                    GLocalFileStatField  mask,
137                    GLocalFileStatField  mask_required,
138                    GLocalFileStat      *stat_buf)
139 {
140   return g_local_file_statx (AT_FDCWD, path,
141                              AT_NO_AUTOMOUNT | AT_STATX_SYNC_AS_STAT,
142                              mask, mask_required, stat_buf);
143 }
144
145 inline static gboolean _g_stat_has_field  (const GLocalFileStat *buf, GLocalFileStatField field) { return buf->stx_mask & field; }
146
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; }
152
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; }
157
158 inline static guint64 _g_stat_blocks      (const GLocalFileStat *buf) { return buf->stx_blocks; }
159
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; }
166
167 #else  /* if !HAVE_STATX */
168
169 #ifdef G_OS_WIN32
170 /* We want 64-bit file size, file ID and symlink support */
171 #define GLocalFileStat GWin32PrivateStat
172 #else
173 #define GLocalFileStat struct stat
174 #endif
175
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. */
179 typedef enum
180 {
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;
194
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)
203
204 static inline int
205 g_local_file_fstat (int                  fd,
206                     GLocalFileStatField  mask,
207                     GLocalFileStatField  mask_required,
208                     GLocalFileStat      *stat_buf)
209 {
210   if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
211     {
212       /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
213       errno = ERANGE;
214       return -1;
215     }
216
217 #ifdef G_OS_WIN32
218   return GLIB_PRIVATE_CALL (g_win32_fstat) (fd, stat_buf);
219 #else
220   return fstat (fd, stat_buf);
221 #endif
222 }
223
224 static inline int
225 g_local_file_fstatat (int                  fd,
226                       const char          *path,
227                       int                  flags,
228                       GLocalFileStatField  mask,
229                       GLocalFileStatField  mask_required,
230                       GLocalFileStat      *stat_buf)
231 {
232   if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
233     {
234       /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
235       errno = ERANGE;
236       return -1;
237     }
238
239 #if !defined(G_OS_WIN32) && defined(AT_FDCWD)
240   return fstatat (fd, path, stat_buf, flags);
241 #else
242   /* Currently not supported on Windows or macOS < 10.10 */
243   errno = ENOSYS;
244   return -1;
245 #endif
246 }
247
248 static inline int
249 g_local_file_lstat (const char          *path,
250                     GLocalFileStatField  mask,
251                     GLocalFileStatField  mask_required,
252                     GLocalFileStat      *stat_buf)
253 {
254   if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
255     {
256       /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
257       errno = ERANGE;
258       return -1;
259     }
260
261 #ifdef G_OS_WIN32
262   return GLIB_PRIVATE_CALL (g_win32_lstat_utf8) (path, stat_buf);
263 #else
264   return g_lstat (path, stat_buf);
265 #endif
266 }
267
268 static inline int
269 g_local_file_stat (const char          *path,
270                    GLocalFileStatField  mask,
271                    GLocalFileStatField  mask_required,
272                    GLocalFileStat      *stat_buf)
273 {
274   if ((G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & (mask_required & mask)) != (mask_required & mask))
275     {
276       /* Only G_LOCAL_FILE_STAT_FIELD_BASIC_STATS are supported. */
277       errno = ERANGE;
278       return -1;
279     }
280
281 #ifdef G_OS_WIN32
282   return GLIB_PRIVATE_CALL (g_win32_stat_utf8) (path, stat_buf);
283 #else
284   return stat (path, stat_buf);
285 #endif
286 }
287
288 inline static gboolean  _g_stat_has_field  (const GLocalFileStat *buf, GLocalFileStatField field) { return (G_LOCAL_FILE_STAT_FIELD_BASIC_STATS & field) == field; }
289
290 #ifndef G_OS_WIN32
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; }
293 #else
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; }
296 #endif
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; }
300
301 #ifndef G_OS_WIN32
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; }
306 #else
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; }
309 #endif
310
311 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
312 inline static blkcnt_t  _g_stat_blocks    (const GLocalFileStat *buf) { return buf->st_blocks; }
313 #endif
314
315 #ifndef G_OS_WIN32
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; }
319 #endif
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; }
324 #endif
325
326 #endif  /* !HAVE_STATX */
327
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
333
334 gboolean   _g_local_file_has_trash_dir        (const char             *dirname,
335                                                dev_t                   dir_dev);
336 #ifdef G_OS_UNIX
337 gboolean   _g_local_file_is_lost_found_dir    (const char             *path,
338                                                dev_t                   path_dev);
339 #endif
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,
346                                                const char             *path,
347                                                GFileAttributeMatcher  *attribute_matcher);
348 GFileInfo *_g_local_file_info_get             (const char             *basename,
349                                                const char             *path,
350                                                GFileAttributeMatcher  *attribute_matcher,
351                                                GFileQueryInfoFlags     flags,
352                                                GLocalParentFileInfo   *parent_info,
353                                                GError                **error);
354 GFileInfo *_g_local_file_info_get_from_fd     (int                     fd,
355                                                const char             *attributes,
356                                                GError                **error);
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,
361                                                gpointer                value_p,
362                                                GFileQueryInfoFlags     flags,
363                                                GCancellable           *cancellable,
364                                                GError                **error);
365 gboolean   _g_local_file_info_set_attributes  (char                   *filename,
366                                                GFileInfo              *info,
367                                                GFileQueryInfoFlags     flags,
368                                                GCancellable           *cancellable,
369                                                GError                **error);
370
371 G_END_DECLS
372
373 #endif /* __G_FILE_LOCAL_FILE_INFO_H__ */