Coverage Report

Created: 2024-04-23 06:19

/src/unrar/uowners.cpp
Line
Count
Source (jump to first uncovered line)
1
2
3
void ExtractUnixOwner20(Archive &Arc,const wchar *FileName)
4
0
{
5
0
  char NameA[NM];
6
0
  WideToChar(FileName,NameA,ASIZE(NameA));
7
8
0
  if (Arc.BrokenHeader)
9
0
  {
10
0
    uiMsg(UIERROR_UOWNERBROKEN,Arc.FileName,FileName);
11
0
    ErrHandler.SetErrorCode(RARX_CRC);
12
0
    return;
13
0
  }
14
15
0
  struct passwd *pw;
16
0
  errno=0; // Required by getpwnam specification if we need to check errno.
17
0
  if ((pw=getpwnam(Arc.UOHead.OwnerName))==NULL)
18
0
  {
19
0
    uiMsg(UIERROR_UOWNERGETOWNERID,Arc.FileName,GetWide(Arc.UOHead.OwnerName));
20
0
    ErrHandler.SysErrMsg();
21
0
    ErrHandler.SetErrorCode(RARX_WARNING);
22
0
    return;
23
0
  }
24
0
  uid_t OwnerID=pw->pw_uid;
25
26
0
  struct group *gr;
27
0
  errno=0; // Required by getgrnam specification if we need to check errno.
28
0
  if ((gr=getgrnam(Arc.UOHead.GroupName))==NULL)
29
0
  {
30
0
    uiMsg(UIERROR_UOWNERGETGROUPID,Arc.FileName,GetWide(Arc.UOHead.GroupName));
31
0
    ErrHandler.SysErrMsg();
32
0
    ErrHandler.SetErrorCode(RARX_CRC);
33
0
    return;
34
0
  }
35
0
  uint Attr=GetFileAttr(FileName);
36
0
  gid_t GroupID=gr->gr_gid;
37
0
#if defined(SAVE_LINKS) && !defined(_APPLE)
38
0
  if (lchown(NameA,OwnerID,GroupID)!=0)
39
#else
40
  if (chown(NameA,OwnerID,GroupID)!=0)
41
#endif
42
0
  {
43
0
    uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName);
44
0
    ErrHandler.SetErrorCode(RARX_CREATE);
45
0
  }
46
0
  SetFileAttr(FileName,Attr);
47
0
}
48
49
50
void ExtractUnixOwner30(Archive &Arc,const wchar *FileName)
51
0
{
52
0
  char NameA[NM];
53
0
  WideToChar(FileName,NameA,ASIZE(NameA));
54
55
0
  char *OwnerName=(char *)&Arc.SubHead.SubData[0];
56
0
  int OwnerSize=strlen(OwnerName)+1;
57
0
  int GroupSize=Arc.SubHead.SubData.Size()-OwnerSize;
58
0
  char GroupName[NM];
59
0
  strncpy(GroupName,(char *)&Arc.SubHead.SubData[OwnerSize],GroupSize);
60
0
  GroupName[GroupSize]=0;
61
62
0
  struct passwd *pw;
63
0
  if ((pw=getpwnam(OwnerName))==NULL)
64
0
  {
65
0
    uiMsg(UIERROR_UOWNERGETOWNERID,Arc.FileName,GetWide(OwnerName));
66
0
    ErrHandler.SetErrorCode(RARX_WARNING);
67
0
    return;
68
0
  }
69
0
  uid_t OwnerID=pw->pw_uid;
70
71
0
  struct group *gr;
72
0
  if ((gr=getgrnam(GroupName))==NULL)
73
0
  {
74
0
    uiMsg(UIERROR_UOWNERGETGROUPID,Arc.FileName,GetWide(GroupName));
75
0
    ErrHandler.SetErrorCode(RARX_WARNING);
76
0
    return;
77
0
  }
78
0
  uint Attr=GetFileAttr(FileName);
79
0
  gid_t GroupID=gr->gr_gid;
80
0
#if defined(SAVE_LINKS) && !defined(_APPLE)
81
0
  if (lchown(NameA,OwnerID,GroupID)!=0)
82
#else
83
  if (chown(NameA,OwnerID,GroupID)!=0)
84
#endif
85
0
  {
86
0
    uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName);
87
0
    ErrHandler.SetErrorCode(RARX_CREATE);
88
0
  }
89
0
  SetFileAttr(FileName,Attr);
90
0
}
91
92
93
void SetUnixOwner(Archive &Arc,const wchar *FileName)
94
0
{
95
0
  char NameA[NM];
96
0
  WideToChar(FileName,NameA,ASIZE(NameA));
97
98
  // First, we try to resolve symbolic names. If they are missing or cannot
99
  // be resolved, we try to use numeric values if any. If numeric values
100
  // are missing too, function fails.
101
0
  FileHeader &hd=Arc.FileHead;
102
0
  if (*hd.UnixOwnerName!=0)
103
0
  {
104
0
    struct passwd *pw;
105
0
    if ((pw=getpwnam(hd.UnixOwnerName))==NULL)
106
0
    {
107
0
      if (!hd.UnixOwnerNumeric)
108
0
      {
109
0
        uiMsg(UIERROR_UOWNERGETOWNERID,Arc.FileName,GetWide(hd.UnixOwnerName));
110
0
        ErrHandler.SetErrorCode(RARX_WARNING);
111
0
        return;
112
0
      }
113
0
    }
114
0
    else
115
0
      hd.UnixOwnerID=pw->pw_uid;
116
0
  }
117
0
  if (*hd.UnixGroupName!=0)
118
0
  {
119
0
    struct group *gr;
120
0
    if ((gr=getgrnam(hd.UnixGroupName))==NULL)
121
0
    {
122
0
      if (!hd.UnixGroupNumeric)
123
0
      {
124
0
        uiMsg(UIERROR_UOWNERGETGROUPID,Arc.FileName,GetWide(hd.UnixGroupName));
125
0
        ErrHandler.SetErrorCode(RARX_WARNING);
126
0
        return;
127
0
      }
128
0
    }
129
0
    else
130
0
      hd.UnixGroupID=gr->gr_gid;
131
0
  }
132
0
#if defined(SAVE_LINKS) && !defined(_APPLE)
133
0
  if (lchown(NameA,hd.UnixOwnerID,hd.UnixGroupID)!=0)
134
#else
135
  if (chown(NameA,hd.UnixOwnerID,hd.UnixGroupID)!=0)
136
#endif
137
0
  {
138
0
    uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName);
139
0
    ErrHandler.SetErrorCode(RARX_CREATE);
140
0
  }
141
0
}