glocalfileinfo: Use accessors to access struct stat members

This will allow the actual struct in use to be changed in future without
code changes everywhere.

Helps: #1970
This commit is contained in:
Andre Miranda
2020-08-14 14:46:14 +01:00
committed by Philip Withnall
parent 83a59e99ff
commit 8edbfe8bb0
5 changed files with 103 additions and 66 deletions

View File

@@ -127,11 +127,11 @@ _g_local_file_info_create_etag (GLocalFileStat *statbuf)
sec = statbuf->st_mtim.tv_sec;
usec = statbuf->st_mtim.tv_nsec / 1000;
#else
sec = statbuf->st_mtime;
sec = _g_stat_mtime (statbuf);
#if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
usec = statbuf->st_mtimensec / 1000;
#elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
usec = statbuf->st_mtim.tv_nsec / 1000;
usec = _g_stat_mtim_nsec (statbuf) / 1000;
#else
usec = 0;
#endif
@@ -147,10 +147,10 @@ _g_local_file_info_create_file_id (GLocalFileStat *statbuf)
#ifdef G_OS_WIN32
ino = statbuf->file_index;
#else
ino = statbuf->st_ino;
ino = _g_stat_ino (statbuf);
#endif
return g_strdup_printf ("l%" G_GUINT64_FORMAT ":%" G_GUINT64_FORMAT,
(guint64) statbuf->st_dev,
(guint64) _g_stat_dev (statbuf),
ino);
}
@@ -158,7 +158,7 @@ static char *
_g_local_file_info_create_fs_id (GLocalFileStat *statbuf)
{
return g_strdup_printf ("l%" G_GUINT64_FORMAT,
(guint64) statbuf->st_dev);
(guint64) _g_stat_dev (statbuf));
}
#if defined (S_ISLNK) || defined (G_OS_WIN32)
@@ -926,7 +926,7 @@ get_access_rights (GFileAttributeMatcher *attribute_matcher,
{
uid_t uid = geteuid ();
if (uid == statbuf->st_uid ||
if (uid == _g_stat_uid (statbuf) ||
uid == parent_info->owner ||
uid == 0)
writable = TRUE;
@@ -959,22 +959,22 @@ set_info_from_stat (GFileInfo *info,
file_type = G_FILE_TYPE_UNKNOWN;
if (S_ISREG (statbuf->st_mode))
if (S_ISREG (_g_stat_mode (statbuf)))
file_type = G_FILE_TYPE_REGULAR;
else if (S_ISDIR (statbuf->st_mode))
else if (S_ISDIR (_g_stat_mode (statbuf)))
file_type = G_FILE_TYPE_DIRECTORY;
#ifndef G_OS_WIN32
else if (S_ISCHR (statbuf->st_mode) ||
S_ISBLK (statbuf->st_mode) ||
S_ISFIFO (statbuf->st_mode)
else if (S_ISCHR (_g_stat_mode (statbuf)) ||
S_ISBLK (_g_stat_mode (statbuf)) ||
S_ISFIFO (_g_stat_mode (statbuf))
#ifdef S_ISSOCK
|| S_ISSOCK (statbuf->st_mode)
|| S_ISSOCK (_g_stat_mode (statbuf))
#endif
)
file_type = G_FILE_TYPE_SPECIAL;
#endif
#ifdef S_ISLNK
else if (S_ISLNK (statbuf->st_mode))
else if (S_ISLNK (_g_stat_mode (statbuf)))
file_type = G_FILE_TYPE_SYMBOLIC_LINK;
#elif defined (G_OS_WIN32)
else if (statbuf->reparse_tag == IO_REPARSE_TAG_SYMLINK ||
@@ -983,28 +983,28 @@ set_info_from_stat (GFileInfo *info,
#endif
g_file_info_set_file_type (info, file_type);
g_file_info_set_size (info, statbuf->st_size);
g_file_info_set_size (info, _g_stat_size (statbuf));
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, statbuf->st_dev);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, statbuf->st_nlink);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_DEVICE, _g_stat_dev (statbuf));
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_NLINK, _g_stat_nlink (statbuf));
#ifndef G_OS_WIN32
/* Pointless setting these on Windows even if they exist in the struct */
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, statbuf->st_ino);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, statbuf->st_uid);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, statbuf->st_gid);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, statbuf->st_rdev);
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_INODE, _g_stat_ino (statbuf));
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_UID, _g_stat_uid (statbuf));
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_GID, _g_stat_gid (statbuf));
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_RDEV, _g_stat_rdev (statbuf));
#endif
/* Mostly pointless on Windows.
* Still, it allows for S_ISREG/S_ISDIR and IWRITE (read-only) checks.
*/
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, statbuf->st_mode);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_MODE, _g_stat_mode (statbuf));
#if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, statbuf->st_blksize);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCK_SIZE, _g_stat_blksize (statbuf));
#endif
#if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, statbuf->st_blocks);
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_BLOCKS, _g_stat_blocks (statbuf));
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
statbuf->st_blocks * G_GUINT64_CONSTANT (512));
_g_stat_blocks (statbuf) * G_GUINT64_CONSTANT (512));
#elif defined (G_OS_WIN32)
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_ALLOCATED_SIZE,
statbuf->allocated_size);
@@ -1017,18 +1017,18 @@ set_info_from_stat (GFileInfo *info,
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atim.tv_sec);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
#else
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, statbuf->st_mtime);
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED, _g_stat_mtime (statbuf));
#if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC)
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtimensec / 1000);
#elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, statbuf->st_mtim.tv_nsec / 1000);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_MODIFIED_USEC, _g_stat_mtim_nsec (statbuf) / 1000);
#endif
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, statbuf->st_atime);
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS, _g_stat_atime (statbuf));
#if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC)
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atimensec / 1000);
#elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, statbuf->st_atim.tv_nsec / 1000);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_ACCESS_USEC, _g_stat_atim_nsec (statbuf) / 1000);
#endif
#endif
@@ -1038,11 +1038,11 @@ set_info_from_stat (GFileInfo *info,
* https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions#generic-text-routine-mappings
* Thank you, Microsoft!
*/
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, statbuf->st_ctime);
_g_file_info_set_attribute_uint64_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED, _g_stat_ctime (statbuf));
#if defined (HAVE_STRUCT_STAT_ST_CTIMENSEC)
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctimensec / 1000);
#elif defined (HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC)
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, statbuf->st_ctim.tv_nsec / 1000);
_g_file_info_set_attribute_uint32_by_id (info, G_FILE_ATTRIBUTE_ID_TIME_CHANGED_USEC, _g_stat_ctim_nsec (statbuf) / 1000);
#endif
#endif
@@ -1306,16 +1306,16 @@ get_content_type (const char *basename,
if (is_symlink &&
(symlink_broken || (flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
return g_content_type_from_mime_type ("inode/symlink");
else if (statbuf != NULL && S_ISDIR(statbuf->st_mode))
else if (statbuf != NULL && S_ISDIR(_g_stat_mode (statbuf)))
return g_content_type_from_mime_type ("inode/directory");
#ifndef G_OS_WIN32
else if (statbuf != NULL && S_ISCHR(statbuf->st_mode))
else if (statbuf != NULL && S_ISCHR(_g_stat_mode (statbuf)))
return g_content_type_from_mime_type ("inode/chardevice");
else if (statbuf != NULL && S_ISBLK(statbuf->st_mode))
else if (statbuf != NULL && S_ISBLK(_g_stat_mode (statbuf)))
return g_content_type_from_mime_type ("inode/blockdevice");
else if (statbuf != NULL && S_ISFIFO(statbuf->st_mode))
else if (statbuf != NULL && S_ISFIFO(_g_stat_mode (statbuf)))
return g_content_type_from_mime_type ("inode/fifo");
else if (statbuf != NULL && S_ISREG(statbuf->st_mode) && statbuf->st_size == 0)
else if (statbuf != NULL && S_ISREG(_g_stat_mode (statbuf)) && _g_stat_size (statbuf) == 0)
{
/* Don't sniff zero-length files in order to avoid reading files
* that appear normal but are not (eg: files in /proc and /sys)
@@ -1328,7 +1328,7 @@ get_content_type (const char *basename,
}
#endif
#ifdef S_ISSOCK
else if (statbuf != NULL && S_ISSOCK(statbuf->st_mode))
else if (statbuf != NULL && S_ISSOCK(_g_stat_mode (statbuf)))
return g_content_type_from_mime_type ("inode/socket");
#endif
else
@@ -1827,12 +1827,12 @@ _g_local_file_info_get (const char *basename,
stat_ok = res != -1;
if (stat_ok)
device = statbuf.st_dev;
device = _g_stat_dev (&statbuf);
else
device = 0;
#ifdef S_ISLNK
is_symlink = stat_ok && S_ISLNK (statbuf.st_mode);
is_symlink = stat_ok && S_ISLNK (_g_stat_mode (&statbuf));
#elif defined (G_OS_WIN32)
/* glib already checked the FILE_ATTRIBUTE_REPARSE_POINT for us */
is_symlink = stat_ok &&
@@ -1871,7 +1871,7 @@ _g_local_file_info_get (const char *basename,
set_info_from_stat (info, &statbuf, attribute_matcher);
#ifdef G_OS_UNIX
if (stat_ok && _g_local_file_is_lost_found_dir (path, statbuf.st_dev))
if (stat_ok && _g_local_file_is_lost_found_dir (path, _g_stat_dev (&statbuf)))
g_file_info_set_is_hidden (info, TRUE);
#endif
@@ -1886,7 +1886,7 @@ _g_local_file_info_get (const char *basename,
}
if (basename != NULL && basename[strlen (basename) -1] == '~' &&
(stat_ok && S_ISREG (statbuf.st_mode)))
(stat_ok && S_ISREG (_g_stat_mode (&statbuf))))
_g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_STANDARD_IS_BACKUP, TRUE);
#else
if (statbuf.attributes & FILE_ATTRIBUTE_HIDDEN)
@@ -1980,7 +1980,7 @@ _g_local_file_info_get (const char *basename,
win32_get_file_user_info (path, NULL, &name, NULL);
#else
if (stat_ok)
name = get_username_from_uid (statbuf.st_uid);
name = get_username_from_uid (_g_stat_uid (&statbuf));
#endif
if (name)
_g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER, name);
@@ -1995,7 +1995,7 @@ _g_local_file_info_get (const char *basename,
win32_get_file_user_info (path, NULL, NULL, &name);
#else
if (stat_ok)
name = get_realname_from_uid (statbuf.st_uid);
name = get_realname_from_uid (_g_stat_uid (&statbuf));
#endif
if (name)
_g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_USER_REAL, name);
@@ -2010,7 +2010,7 @@ _g_local_file_info_get (const char *basename,
win32_get_file_user_info (path, &name, NULL, NULL);
#else
if (stat_ok)
name = get_groupname_from_gid (statbuf.st_gid);
name = get_groupname_from_gid (_g_stat_gid (&statbuf));
#endif
if (name)
_g_file_info_set_attribute_string_by_id (info, G_FILE_ATTRIBUTE_ID_OWNER_GROUP, name);
@@ -2019,7 +2019,7 @@ _g_local_file_info_get (const char *basename,
if (stat_ok && parent_info && parent_info->device != 0 &&
_g_file_attribute_matcher_matches_id (attribute_matcher, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT) &&
(statbuf.st_dev != parent_info->device || statbuf.st_ino == parent_info->inode))
(_g_stat_dev (&statbuf) != parent_info->device || _g_stat_ino (&statbuf) == parent_info->inode))
_g_file_info_set_attribute_boolean_by_id (info, G_FILE_ATTRIBUTE_ID_UNIX_IS_MOUNTPOINT, TRUE);
if (stat_ok)