uowners.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. void ExtractUnixOwner(Archive &Arc,char *FileName)
  2. {
  3. if (Arc.HeaderCRC!=Arc.UOHead.HeadCRC)
  4. {
  5. Log(Arc.FileName,St(MOwnersBroken),FileName);
  6. ErrHandler.SetErrorCode(CRC_ERROR);
  7. return;
  8. }
  9. struct passwd *pw;
  10. errno=0; // Required by getpwnam specification if we need to check errno.
  11. if ((pw=getpwnam(Arc.UOHead.OwnerName))==NULL)
  12. {
  13. Log(Arc.FileName,St(MErrGetOwnerID),Arc.UOHead.OwnerName);
  14. ErrHandler.SysErrMsg();
  15. ErrHandler.SetErrorCode(WARNING);
  16. return;
  17. }
  18. uid_t OwnerID=pw->pw_uid;
  19. struct group *gr;
  20. errno=0; // Required by getgrnam specification if we need to check errno.
  21. if ((gr=getgrnam(Arc.UOHead.GroupName))==NULL)
  22. {
  23. Log(Arc.FileName,St(MErrGetGroupID),Arc.UOHead.GroupName);
  24. ErrHandler.SysErrMsg();
  25. ErrHandler.SetErrorCode(CRC_ERROR);
  26. return;
  27. }
  28. uint Attr=GetFileAttr(FileName,NULL);
  29. gid_t GroupID=gr->gr_gid;
  30. #if defined(SAVE_LINKS) && !defined(_APPLE)
  31. if (lchown(FileName,OwnerID,GroupID)!=0)
  32. #else
  33. if (chown(FileName,OwnerID,GroupID)!=0)
  34. #endif
  35. {
  36. Log(Arc.FileName,St(MSetOwnersError),FileName);
  37. ErrHandler.SetErrorCode(CREATE_ERROR);
  38. }
  39. SetFileAttr(FileName,NULL,Attr);
  40. }
  41. void ExtractUnixOwnerNew(Archive &Arc,char *FileName)
  42. {
  43. char *OwnerName=(char *)&Arc.SubHead.SubData[0];
  44. int OwnerSize=strlen(OwnerName)+1;
  45. int GroupSize=Arc.SubHead.SubData.Size()-OwnerSize;
  46. char GroupName[NM];
  47. strncpy(GroupName,(char *)&Arc.SubHead.SubData[OwnerSize],GroupSize);
  48. GroupName[GroupSize]=0;
  49. struct passwd *pw;
  50. if ((pw=getpwnam(OwnerName))==NULL)
  51. {
  52. Log(Arc.FileName,St(MErrGetOwnerID),OwnerName);
  53. ErrHandler.SetErrorCode(WARNING);
  54. return;
  55. }
  56. uid_t OwnerID=pw->pw_uid;
  57. struct group *gr;
  58. if ((gr=getgrnam(GroupName))==NULL)
  59. {
  60. Log(Arc.FileName,St(MErrGetGroupID),GroupName);
  61. ErrHandler.SetErrorCode(CRC_ERROR);
  62. return;
  63. }
  64. uint Attr=GetFileAttr(FileName,NULL);
  65. gid_t GroupID=gr->gr_gid;
  66. #if defined(SAVE_LINKS) && !defined(_APPLE)
  67. if (lchown(FileName,OwnerID,GroupID)!=0)
  68. #else
  69. if (chown(FileName,OwnerID,GroupID)!=0)
  70. #endif
  71. {
  72. Log(Arc.FileName,St(MSetOwnersError),FileName);
  73. ErrHandler.SetErrorCode(CREATE_ERROR);
  74. }
  75. SetFileAttr(FileName,NULL,Attr);
  76. }