⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 warfiledriverwin32.cpp

📁 ftpserver very good sample
💻 CPP
字号:
#include "StdAfx.h"#include "WarFileDriverWin32.h"   // class implemented#include "WarFileDriverFileWin32.h"#ifndef WAR_LOG_H#   include "WarLog.h"#endif/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================WarFileDriverWin32::WarFileDriverWin32()        : WarFileDriver("file"){}//============================= OPERATORS ====================================//============================= OPERATIONS ===================================void WarFileDriverWin32::WarListDirectory(const WarUrl& Path,                                  WarDirList& listDestination)                                  throw(WarException){    HANDLE h;    WIN32_FIND_DATA d;    bool do_patterns_manually = false;    war_syspath_t file_pattern;    file_pattern << Path.GetFilePath().GetPath()         << WAR_SYSSLASH;    if (listDestination.mPatternList.size() == 1)        file_pattern << listDestination.mPatternList.front().GetPath();    else    {        if (!listDestination.mPatternList.empty())            do_patterns_manually = true;        file_pattern << "*";    }        if ((h = ::FindFirstFile(file_pattern.GetPath(), &d))         == INVALID_HANDLE_VALUE)    {        WarThrow(WarSystemError(WAR_FERR_NO_SUCH_PATH), NULL);    }    do    {        if ((listDestination.mFlags & DIRF_HIDE_HIDDEN)            && (d.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))            continue;        #ifdef UNICODE#   define FILE_NAME d.cFileName#else#   define FILE_NAME filename_buf.GetValue().c_str()        WarCollector<wchar_t> filename_buf(d.cFileName);#endif        if (listDestination.IsFileExcluded(FILE_NAME,             !do_patterns_manually))            continue;                    listDestination.AddFile(FILE_NAME, &d);                } while(::FindNextFile(h, &d));        ::FindClose(h);    }void WarFileDriverWin32::WarDeleteFile(const WarUrl& Path) constthrow(WarException){    BOOL result = ::DeleteFile(Path.GetFilePath().GetPath());    if (!result)    {        WarError system_error;        system_error.Capture();                 switch(system_error.SystemError())        {        case ERROR_FILE_NOT_FOUND:        case ERROR_PATH_NOT_FOUND:            system_error = WAR_FERR_NO_SUCH_PATH;            break;        case ERROR_ACCESS_DENIED:            system_error = WAR_ERR_ACCESS_DENIED;            break;        default:            ;        }        WarThrow(system_error, NULL);    }}void WarFileDriverWin32::WarDeleteDirectory(const WarUrl& Path) constthrow(WarException){    BOOL result = ::RemoveDirectory(Path.GetFilePath().GetPath());    if (!result)    {        WarError system_error;        system_error.Capture();         switch(system_error.SystemError())        {        case ERROR_FILE_NOT_FOUND:        case ERROR_PATH_NOT_FOUND:            system_error = WAR_FERR_NO_SUCH_PATH;            break;        case ERROR_ACCESS_DENIED:            system_error = WAR_ERR_ACCESS_DENIED;            break;        default:           ;        }        WarThrow(system_error, NULL);    }}void WarFileDriverWin32::WarCreateDirectory(const WarUrl& Path) constthrow(WarException){    BOOL result = ::CreateDirectory(Path.GetFilePath().GetPath(), NULL);    if (!result)    {        WarError system_error;        system_error.Capture();         switch(system_error.SystemError())        {        case ERROR_ACCESS_DENIED:            system_error = WAR_ERR_ACCESS_DENIED;            break;        case ERROR_ALREADY_EXISTS:            system_error = WAR_ERR_OBJECT_EXIST;            break;        default:            ;        }        WarThrow(system_error, NULL);    }}void WarFileDriverWin32::WarRenameDirectory(const WarUrl& From,                                          const WarUrl& To) const                                         throw(WarException){    WarMoveFile(From, To);}void WarFileDriverWin32::WarCopyFile(const WarUrl& From,                                   const WarUrl& To) const                                  throw(WarException) {    BOOL result = ::CopyFile(From.GetFilePath().GetPath(),         To.GetFilePath().GetPath(), FALSE);        if (!result)    {        WarError system_error;        system_error.Capture();                 switch(system_error.SystemError())        {        case ERROR_ACCESS_DENIED:            system_error = WAR_ERR_ACCESS_DENIED;            break;        default:            ;        }                WarThrow(system_error, NULL);    }}void WarFileDriverWin32::WarMoveFile(const WarUrl& From,                                   const WarUrl& To) const                                  throw(WarException){    BOOL result = ::MoveFileEx(From.GetFilePath().GetPath(),         To.GetFilePath().GetPath(),        MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING);    if (!result)    {        WarError system_error;        system_error.Capture();                 switch(system_error.SystemError())        {        case ERROR_ACCESS_DENIED:            system_error = WAR_ERR_ACCESS_DENIED;            break;        default:           ;        }        WarThrow(system_error, NULL);    }}war_flen_t WarFileDriverWin32::WarGetSize(const WarUrl& Path) constthrow(WarException){    war_flen_t return_val = 0;    HANDLE file_handle = ::CreateFile(Path.GetFilePath().GetPath(),         0, 0, NULL, OPEN_EXISTING, 0, NULL);    if (file_handle == INVALID_HANDLE_VALUE)    {        WarError system_error;        system_error.Capture();                 switch(system_error.SystemError())        {        case ERROR_FILE_NOT_FOUND:        case ERROR_PATH_NOT_FOUND:            system_error = WAR_FERR_NO_SUCH_PATH;            break;        case ERROR_ACCESS_DENIED:            system_error = WAR_ERR_ACCESS_DENIED;            break;        default:            ;        }        WarThrow(system_error, NULL);    }    LARGE_INTEGER FileSize;#ifdef HAVE_GETFILESIZEEX    if (::GetFileSizeEx(file_handle, &FileSize))        return_val = FileSize.QuadPart;    else    {        WarError system_error;        system_error.Capture();        WarThrow(system_error, NULL);    }#else    {        DWORD dwHigh = 0;        DWORD dwLow = ::GetFileSize(file_handle, &dwHigh);        ::CloseHandle(file_handle);                if (dwLow == 0xFFFFFFFF)         {            WarError system_error;            system_error.Capture();                        if (system_error.SystemError() != NO_ERROR)                WarThrow(system_error, NULL);        }                FileSize.LowPart = dwLow;        FileSize.HighPart = dwHigh;        return_val = FileSize.QuadPart;    }#endif    return return_val;}war_flen_t WarFileDriverWin32::WarGetFreeSpaceOnPath(const WarUrl& Path) constthrow(WarException){    ULARGE_INTEGER FreeBytesAvailableToCaller,         TotalNumberOfBytes,         TotalNumberOfFreeBytes;        if (GetDiskFreeSpaceEx(Path.GetFilePath().GetPath(),        &FreeBytesAvailableToCaller,        &TotalNumberOfBytes,        &TotalNumberOfFreeBytes))    {        return FreeBytesAvailableToCaller.QuadPart;    }        WarError system_error;    system_error.Capture();    WarThrow(system_error, NULL);        return 0; // Compiler food}WarFileDriverFile *WarFileDriverWin32::WarCreateNewFileObject(const WarUrl& fileUrl) const throw(WarException){    return new WarFileDriverFileWin32((WarFileDriver *)this);}void WarFileDriverWin32::WarStat(const WarUrl& Path, war_stat_t& st) const        throw(WarException){    int retries = 0;retry_open:#ifdef UNICODE    if (_wstati64(Path.GetFilePath().GetPath(), &st))#else    if (_stati64(Path.GetFilePath().GetPath(), &st))#endif    {        WarSystemError system_error;        switch(system_error.SystemError())        {        case ERROR_PATH_NOT_FOUND:            system_error = WAR_FERR_NO_SUCH_PATH;            break;        case ERROR_ACCESS_DENIED:            system_error = WAR_ERR_ACCESS_DENIED;            break;        case ERROR_BAD_NET_NAME:            {                // Microsoft Windows can't handle its own network shares                // trough the standard API calls, so we have to do some                // magic here if this is a UNC path...                WIN32_FILE_ATTRIBUTE_DATA fd;                memset(&fd, 0, sizeof(fd));                if (GetFileAttributesEx(                    Path.GetFilePath().GetPath(),                    GetFileExInfoStandard, &fd)                    && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))                {                    // Just fake an entry...                    memset(&st, 0, sizeof(st));                    st.st_dev = -1;                    st.st_ino = 0;                    st.st_mode = _S_IFDIR                         | ((fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _S_IREAD : (_S_IREAD|_S_IWRITE));                    st.st_mode |= (st.st_mode & 0700) >> 3;                    st.st_mode |= (st.st_mode & 0700) >> 6;                    st.st_nlink = 1;                    st.st_rdev = -1;                    st.st_size = fd.nFileSizeLow | ((fd.nFileSizeHigh << 32) & 0xffffffff00000000L);                    st.st_atime = WarTime(fd.ftLastAccessTime).GetTime();                    st.st_mtime = WarTime(fd.ftLastWriteTime).GetTime();                    st.st_ctime = WarTime(fd.ftCreationTime).GetTime();                    return; // OK                }                else                {                    WarSystemError sys_err;                    if (sys_err.SystemError() == ERROR_LOGON_FAILURE)                        goto logon_err;                }            }            break;        case ERROR_LOGON_FAILURE:logon_err:            if (!retries++ && LogonToUrl(Path))                goto retry_open;         }        WarThrow(system_error, NULL);    }}bool WarFileDriverWin32::LogonToUrl(const WarUrl& logonUrl) const{    WarLog debug_log(WARLOG_DEBUG, "WarFileDriverWin32::LogonToUrl()");    try    {        if (!logonUrl.GetPassword().empty())        {            war_syspath_t net_path = logonUrl.GetFilePath().GetMountPath();            NETRESOURCE n;                        memset(&n, 0, sizeof(n));            n.dwType = RESOURCETYPE_DISK;            n.lpRemoteName = (LPTSTR)net_path.GetPath();                        WarCollector<TCHAR> user_name;            WarCollector<TCHAR> user_passwd(WarCollector<TCHAR>::SM_ERASE);                        user_name << logonUrl.GetUserName();            user_passwd << logonUrl.GetPassword();                        if (debug_log)            {                debug_log << "Attempting a network logon to the path "                    << net_path.GetPath()                    << war_endl;            }                        // Try and log on to the network path            int err = ::WNetAddConnection2(&n,                 user_passwd.GetValue().c_str(),                user_name.GetValue().c_str(),                0);                        if (NO_ERROR == err)                return true;                        WarError my_err(WAR_ERR_ACCESS_DENIED, err);                        WarLog warn_log(WARLOG_WARNINGS, "WarFileDriverWin32::LogonToUrl");            warn_log << "Failed to log on to network path "                 << logonUrl.GetUrl()                << my_err                << war_endl;        }    }    catch(WarException)    {    }        return false;}//============================= ACCESS     ===================================//============================= INQUIRY    ===================================WarFileEnums::PathCmpTypeE WarFileDriverWin32::WarGetCmpMode(const WarUrl& Path) const{    war_syspath_t root_path;    war_syspath_t my_path = Path.GetFilePath().GetPath();    DWORD SerialNo = 0, ComponentLength = 0, Flags = 0;    root_path = my_path.GetMountPath();    if (!GetVolumeInformation(root_path.GetPath(), NULL, 0, &SerialNo, &ComponentLength, &Flags, NULL, 0))        WarThrow(WarError(WAR_FERR_NOT_A_DRIVE), NULL);    return ((Flags & FS_CASE_SENSITIVE) == FS_CASE_SENSITIVE) ?           PCMP_CASE_SENSITIVE : PCMP_CASE_INSENSITIVE;}void WarFileDriverWin32::WarGetFileModTime(const WarUrl& fsysUrl,                      WarTime& outTime,                      size_t& outPrecision) const                      throw(WarException){    WIN32_FILE_ATTRIBUTE_DATA fi;    if (::GetFileAttributesEx(fsysUrl.GetFilePath().GetPath(),        GetFileExInfoStandard,         &fi))    {        outTime = fi.ftLastWriteTime;        outPrecision = 1000;        return;    }    WarFileDriver::WarGetFileModTime(fsysUrl, outTime, outPrecision);}/////////////////////////////// PROTECTED  ////////////////////////////////////////////////////////////////// PRIVATE    ///////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -