PIFile: applyFileInfo

PIPeer: fixed
pisd work progress ...

git-svn-id: svn://db.shs.com.ru/pip@20 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2015-03-12 13:28:27 +00:00
parent 3ed292a602
commit fdb48aba64
14 changed files with 346 additions and 148 deletions

View File

@@ -34,7 +34,9 @@
# define S_IFSOCK 0x20
#else
# include <sys/stat.h>
# include <sys/time.h>
# include <fcntl.h>
# include <utime.h>
#endif
#define S_IFHDN 0x40
#ifdef QNX
@@ -289,6 +291,20 @@ PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
ret.size = filesize.QuadPart;
ret.time_access = PIDateTime(fi.ftLastAccessTime);
ret.time_modification = PIDateTime(fi.ftLastWriteTime);
/*PIByteArray sec;
DWORD sec_n(0);
//SECURITY_DESCRIPTOR sec;
GetFileSecurity(path.data(), DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION, (SECURITY_DESCRIPTOR*)sec.data(), 0, &sec_n);
sec.resize(sec_n);
GetFileSecurity(path.data(), DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION, (SECURITY_DESCRIPTOR*)sec.data(), sec.size(), &sec_n);
errorClear();
SID sid; BOOL def;
GetSecurityDescriptorGroup((PSECURITY_DESCRIPTOR)sec.data(), &sid, &def);
char * s(0);
ConvertSidToStringSid((PSID)&sid, s);
piCout << s;
LocalFree(s);
//ret.id_user = ;*/
}
CloseHandle(hFile);
#else
@@ -325,3 +341,65 @@ PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
if (n == "..") ret.flags = FileInfo::Dir | FileInfo::DotDot;
return ret;
}
bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info) {
if (path.isEmpty()) return false;
PIString fp(path);
if (fp.endsWith(PIDir::separator)) fp.pop_back();
#ifdef WINDOWS
DWORD attr = GetFileAttributes((LPCTSTR)(path.data()));
if (attr == 0xFFFFFFFF) return false;
attr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY);
if (info.isHidden()) attr |= FILE_ATTRIBUTE_HIDDEN;
if (!info.perm_user.write) attr |= FILE_ATTRIBUTE_READONLY;
if (SetFileAttributes((LPCTSTR)(fp.data()), attr) == 0) {
piCout << "[PIFile] applyFileInfo: \"SetFileAttributes\" error:" << errorString();
return false;
}
HANDLE hFile = 0;
if ((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
hFile = CreateFile(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
} else {
hFile = CreateFile(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
}
if (!hFile) return false;
FILETIME atime = info.time_access.toFILETIME(), mtime = info.time_modification.toFILETIME();
if (SetFileTime(hFile, 0, &atime, &mtime) == 0) {
piCout << "[PIFile] applyFileInfo: \"SetFileTime\" error:" << errorString();
return false;
}
CloseHandle(hFile);
#else
int mode(0);
if (info.perm_user.read) mode |= S_IRUSR;
if (info.perm_user.write) mode |= S_IWUSR;
if (info.perm_user.exec) mode |= S_IXUSR;
if (info.perm_group.read) mode |= S_IRGRP;
if (info.perm_group.write) mode |= S_IWGRP;
if (info.perm_group.exec) mode |= S_IXGRP;
if (info.perm_other.read) mode |= S_IROTH;
if (info.perm_other.write) mode |= S_IWOTH;
if (info.perm_other.exec) mode |= S_IXOTH;
if (chmod(fp.data(), mode) != 0) {
piCout << "[PIFile] applyFileInfo: \"chmod\" error:" << errorString();
return false;
}
if (chown(fp.data(), info.id_user, info.id_group) != 0) {
piCout << "[PIFile] applyFileInfo: \"chown\" error:" << errorString();
return false;
}
struct timeval tm[2];
PISystemTime st = info.time_access.toSystemTime();
tm[0].tv_sec = st.seconds;
tm[0].tv_usec = st.nanoseconds / 1000;
st = info.time_modification.toSystemTime();
tm[1].tv_sec = st.seconds;
tm[1].tv_usec = st.nanoseconds / 1000;
if (utimes(fp.data(), tm) != 0) {
piCout << "[PIFile] applyFileInfo: \"utimes\" error:" << errorString();
return false;
}
#endif
return true;
}