Improve code using more winpr functions

This commit is contained in:
David PHAM-VAN 2016-02-26 10:19:21 -08:00
parent 2e1e6a4ff0
commit 2f28b6c38b
2 changed files with 66 additions and 56 deletions

View File

@ -61,6 +61,8 @@
#ifdef _WIN32
#pragma comment(lib, "Shlwapi.lib")
#include <Shlwapi.h>
#else
#include <winpr/path.h>
#endif
#include "drive_file.h"
@ -512,19 +514,19 @@ BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UIN
INT64 size;
int status;
char* fullpath;
struct STAT st;
#if defined(__linux__) && !defined(ANDROID) || defined(sun)
struct timespec tv[2];
#else
struct timeval tv[2];
#endif
UINT64 CreationTime;
UINT64 LastAccessTime;
UINT64 LastWriteTime;
UINT64 ChangeTime;
ULARGE_INTEGER liCreationTime;
ULARGE_INTEGER liLastAccessTime;
ULARGE_INTEGER liLastWriteTime;
ULARGE_INTEGER liChangeTime;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
FILETIME* pftCreationTime = NULL;
FILETIME* pftLastAccessTime = NULL;
FILETIME* pftLastWriteTime = NULL;
UINT32 FileAttributes;
UINT32 FileNameLength;
int fd;
HANDLE hFd;
LARGE_INTEGER liSize;
m = 0;
@ -533,45 +535,51 @@ BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UIN
{
case FileBasicInformation:
/* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
Stream_Read_UINT64(input, CreationTime);
Stream_Read_UINT64(input, LastAccessTime);
Stream_Read_UINT64(input, LastWriteTime);
Stream_Read_UINT64(input, ChangeTime);
Stream_Read_UINT64(input, liCreationTime.QuadPart);
Stream_Read_UINT64(input, liLastAccessTime.QuadPart);
Stream_Read_UINT64(input, liLastWriteTime.QuadPart);
Stream_Read_UINT64(input, liChangeTime.QuadPart);
Stream_Read_UINT32(input, FileAttributes);
if (FSTAT(file->fd, &st) != 0)
if (!PathFileExistsA(file->fullpath))
return FALSE;
tv[0].tv_sec = st.st_atime;
tv[1].tv_sec = (LastWriteTime > 0 ? FILE_TIME_RDP_TO_SYSTEM(LastWriteTime) : st.st_mtime);
#ifndef WIN32
#ifdef ANDROID
tv[0].tv_usec = 0;
tv[1].tv_usec = 0;
utimes(file->fullpath, tv);
#elif defined (__linux__) || defined (sun)
tv[0].tv_nsec = 0;
tv[1].tv_nsec = 0;
futimens(file->fd, tv);
#else
tv[0].tv_usec = 0;
tv[1].tv_usec = 0;
futimes(file->fd, tv);
#endif
if (FileAttributes > 0)
hFd = CreateFileA(file->fullpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFd == INVALID_HANDLE_VALUE)
{
m = st.st_mode;
if ((FileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
m |= S_IWUSR;
else
m &= ~S_IWUSR;
if (m != st.st_mode)
fchmod(file->fd, st.st_mode);
WLog_ERR(TAG, "Unable to set file time %s to %d", file->fullpath);
return FALSE;
}
#else /* WIN32 */
SetFileTime(file->fd, &CreationTime, &LastAccessTime, &LastWriteTime);
#endif
if (liCreationTime.QuadPart != 0)
{
ftCreationTime.dwHighDateTime = liCreationTime.HighPart;
ftCreationTime.dwLowDateTime = liCreationTime.LowPart;
pftCreationTime = &ftCreationTime;
}
if (liLastAccessTime.QuadPart != 0)
{
ftLastAccessTime.dwHighDateTime = liLastAccessTime.HighPart;
ftLastAccessTime.dwLowDateTime = liLastAccessTime.LowPart;
pftLastAccessTime = &ftLastAccessTime;
}
if (liLastWriteTime.QuadPart != 0)
{
ftLastWriteTime.dwHighDateTime = liLastWriteTime.HighPart;
ftLastWriteTime.dwLowDateTime = liLastWriteTime.LowPart;
pftLastWriteTime = &ftLastWriteTime;
}
if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
{
ftLastWriteTime.dwHighDateTime = liChangeTime.HighPart;
ftLastWriteTime.dwLowDateTime = liChangeTime.LowPart;
pftLastWriteTime = &ftLastWriteTime;
}
if (!SetFileTime(hFd, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
{
WLog_ERR(TAG, "Unable to set file time %s to %d", file->fullpath);
CloseHandle(hFd);
return FALSE;
}
CloseHandle(hFd);
break;
case FileEndOfFileInformation:
@ -579,19 +587,21 @@ BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UIN
case FileAllocationInformation:
/* http://msdn.microsoft.com/en-us/library/cc232076.aspx */
Stream_Read_INT64(input, size);
#ifndef _WIN32
fd = OPEN(file->fullpath, O_RDWR);
if (ftruncate(fd, size) != 0)
hFd = CreateFileA(file->fullpath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFd == INVALID_HANDLE_VALUE)
{
WLog_ERR(TAG, "Unable to truncate %s to %d: %s (%d)", file->fullpath, size, strerror(errno), errno);
WLog_ERR(TAG, "Unable to truncate %s to %d", file->fullpath, size);
return FALSE;
}
close(fd);
#else
liSize.QuadPart = size;
SetFilePointerEx(file->fd, liSize, NULL, FILE_BEGIN);
SetEndOfFile(file->fd);
#endif
if (SetFilePointerEx(hFd, liSize, NULL, FILE_BEGIN) == 0)
{
WLog_ERR(TAG, "Unable to truncate %s to %d", file->fullpath, size);
CloseHandle(hFd);
return FALSE;
}
CloseHandle(hFd);
break;
case FileDispositionInformation:

View File

@ -7,6 +7,8 @@
* Copyright 2012 Gerald Richter
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
* Copyright 2016 Inuvika Inc.
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -84,8 +86,6 @@ typedef UINT32 mode_t;
#define FILE_TIME_SYSTEM_TO_RDP(_t) \
(((UINT64)(_t) + EPOCH_DIFF) * 10000000LL)
#define FILE_TIME_RDP_TO_SYSTEM(_t) \
(((_t) == 0LL || (_t) == (UINT64)(-1LL)) ? 0 : (time_t)((_t) / 10000000LL - EPOCH_DIFF))
#define FILE_ATTR_SYSTEM_TO_RDP(_f, _st) ( \
(S_ISDIR(_st.st_mode) ? FILE_ATTRIBUTE_DIRECTORY : 0) | \