"Fossies" - the Fresh Open Source Software Archive

Member "unrar/uowners.cpp" (4 May 2022, 3695 Bytes) of package /linux/misc/unrarsrc-6.1.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "uowners.cpp" see the Fossies "Dox" file reference documentation.

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