From 390243fb7c10276e8f882ef6fc2add39f2b1c8ae Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Mon, 17 Sep 2018 14:54:22 +0100 Subject: [PATCH] utils: Add flatpak_file_is_non_empty() Utility to check for existing, regular (or symlink to regular) files that are not empty.wq --- common/flatpak-utils-private.h | 2 ++ common/flatpak-utils.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/common/flatpak-utils-private.h b/common/flatpak-utils-private.h index 5620c5a3b..a6d1a9a3d 100644 --- a/common/flatpak-utils-private.h +++ b/common/flatpak-utils-private.h @@ -493,6 +493,8 @@ char * flatpak_resolve_link (const char *path, GError **error); char * flatpak_canonicalize_filename (const char *path); +gboolean flatpak_file_is_non_empty (const char *path); + gboolean flatpak_file_rename (GFile *from, GFile *to, GCancellable *cancellable, diff --git a/common/flatpak-utils.c b/common/flatpak-utils.c index 875887cf8..81cd0ef01 100644 --- a/common/flatpak-utils.c +++ b/common/flatpak-utils.c @@ -2164,6 +2164,21 @@ flatpak_canonicalize_filename (const char *path) return g_file_get_path (file); } +/* Check for non-empty regular files */ +gboolean +flatpak_file_is_non_empty (const char *path) +{ + struct stat stbuf; + + if (TEMP_FAILURE_RETRY (stat (path, &stbuf)) != 0) + return FALSE; + + if (!S_ISREG (stbuf.st_mode) || stbuf.st_size == 0) + return FALSE; + + return TRUE; +} + gboolean flatpak_file_rename (GFile *from, GFile *to,