]> git.tdb.fi Git - libs/core.git/commitdiff
Improve robustness of resolving user/group names in Stat
authorMikko Rasa <tdb@tdb.fi>
Sun, 31 Oct 2021 11:26:57 +0000 (13:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 31 Oct 2021 17:12:05 +0000 (19:12 +0200)
source/fs/android/stat_owner.cpp
source/fs/stat_private.h
source/fs/unix/stat_owner.cpp
source/fs/unix/stat_platform.h
source/fs/windows/stat.cpp
source/fs/windows/stat_platform.h

index 45fba304e76907325247e140476fd05b8127b9d7..48823e948372ae6f237eb906656683ff9464b1de 100644 (file)
@@ -9,17 +9,27 @@ namespace FS {
 
 void Stat::Private::fill_owner_info(Stat::OwnerInfo &result)
 {
-       struct passwd *owner;
-       if((owner = getpwuid(owner_id)))
-               result.owner = owner->pw_name;
+       if(owner_id!=INVALID_UID)
+       {
+               struct passwd *owner;
+               if((owner = getpwuid(owner_id)))
+                       result.owner = owner->pw_name;
+               else
+                       result.owner = format("%d", owner_id);
+       }
        else
-               result.owner = format("%d", owner_id);
+               result.owner = "None";
 
-       struct group *group;
-       if((group = getgrgid(group_id)))
-               result.group = group->gr_name;
+       if(group_id!=INVALID_GID)
+       {
+               struct group *group;
+               if((group = getgrgid(group_id)))
+                       result.group = group->gr_name;
+               else
+                       result.group = format("%d", group_id);
+       }
        else
-               result.group = format("%d", group_id);
+               result.group = "None";
 }
 
 } // namespace FS
index 7b20638d36ed5b5a1e75d2f14ce9fb102dfc44c0..60438a269717373f9cc418aaa3266ba9b57df4e8 100644 (file)
@@ -8,8 +8,8 @@ namespace FS {
 
 struct Stat::Private
 {
-       UserID owner_id;
-       GroupID group_id;
+       UserID owner_id = INVALID_UID;
+       GroupID group_id = INVALID_GID;
 
        Private();
        Private(const Private &);
index 1a33d74efc0d862df0cbf85f3e1bd8a4a9ff5b11..9c643d9cf92c3533d8f80ee4df3b4649e567e4d6 100644 (file)
@@ -11,19 +11,29 @@ void Stat::Private::fill_owner_info(Stat::OwnerInfo &result)
 {
        char buf[1024];
 
-       struct passwd pw;
-       struct passwd *owner;
-       if(!getpwuid_r(owner_id, &pw, buf, sizeof(buf), &owner) && owner)
-               result.owner = owner->pw_name;
+       if(owner_id!=INVALID_UID)
+       {
+               struct passwd pw;
+               struct passwd *owner;
+               if(!getpwuid_r(owner_id, &pw, buf, sizeof(buf), &owner) && owner)
+                       result.owner = owner->pw_name;
+               else
+                       result.owner = format("%d", owner_id);
+       }
        else
-               result.owner = format("%d", owner_id);
+               result.owner = "None";
 
-       struct group gr;
-       struct group *group;
-       if(!getgrgid_r(group_id, &gr, buf, sizeof(buf), &group) && group)
-               result.group = group->gr_name;
+       if(group_id!=INVALID_GID)
+       {
+               struct group gr;
+               struct group *group;
+               if(!getgrgid_r(group_id, &gr, buf, sizeof(buf), &group) && group)
+                       result.group = group->gr_name;
+               else
+                       result.group = format("%d", group_id);
+       }
        else
-               result.group = format("%d", group_id);
+               result.group = "None";
 }
 
 } // namespace FS
index cc3f0957b58100d5c0e52cb1f67f851d75769e25..d888fed3d38180a13b1c45a4d79d1281b7f56112 100644 (file)
@@ -9,6 +9,9 @@ namespace FS {
 typedef uid_t UserID;
 typedef gid_t GroupID;
 
+const UserID INVALID_UID = -1;
+const GroupID INVALID_GID = -1;
+
 } // namespace FS
 } // namespace Msp
 
index ac9e0c977ac537fcd97bb9230b794ba7f334cc8a..36f28a0de3413ba489fbfea524ed9cad9ee999b6 100644 (file)
@@ -50,20 +50,20 @@ Stat::Private::Private(const Private &other):
 
 Stat::Private::~Private()
 {
-       if(owner_id)
+       if(owner_id!=INVALID_UID)
                HeapFree(GetProcessHeap(), 0, owner_id);
-       if(group_id)
+       if(group_id!=INVALID_GID)
                HeapFree(GetProcessHeap(), 0, group_id);
 }
 
 void Stat::Private::fill_owner_info(Stat::OwnerInfo &result)
 {
-       if(owner_id)
+       if(owner_id!=INVALID_UID)
                result.owner = get_account_name(owner_id);
        else
                result.owner = "None";
 
-       if(group_id)
+       if(group_id!=INVALID_GID)
                result.group = get_account_name(group_id);
        else
                result.group = "None";
index 4e8cfbc3799d2f18d90f791dd6e0d92060fb7dd5..c193accd186b6452410e16fbc0fe0cf75f1d50ff 100644 (file)
@@ -9,6 +9,9 @@ namespace FS {
 typedef PSID UserID;
 typedef PSID GroupID;
 
+const UserID INVALID_UID = 0;
+const GroupID INVALID_GID = 0;
+
 } // namespace FS
 } // namespace Msp