📄 warfiledriverunix.cpp
字号:
#include "StdAfx.h"#include "WarFileDriverUnix.h" // class implemented#if !defined(WAR_STDLIB_H_INCLUDED)# define WAR_STDLIB_H_INCLUDED# include <stdlib.h>#endif#if !defined(WAR_STDIO_H_INCLUDED)# define WAR_STDIO_H_INCLUDED# include <stdio.h>#endif#if !defined(WAR_ERRNO_H_INCLUDED)# define WAR_ERRNO_H_INCLUDED# include <errno.h>#endif#if !defined(WAR_DIRECT_H_INCLUDED) && (defined(WIN32) || defined(HAVE_DIRECT_H))# define WAR_DIRECT_H_INCLUDED# include <direct.h>#endif#if !defined(WAR_SYS_STAT_H_INCLUDED) && defined(HAVE_SYS_STAT_H)# define WAR_SYS_STAT_H_INCLUDED# include <sys/stat.h>#endif #if !defined(WAR_SYS_TYPES_H_INCLUDED) && defined(HAVE_SYS_TYPES_H)# define WAR_SYS_TYPES_H_INCLUDED# include <sys/types.h>#endif #if !defined(WAR_UNISTD_H_INCLUDED) && defined(HAVE_UNISTD_H)# define WAR_UNISTD_H_INCLUDED# include <unistd.h>#endif #if !defined(WAR_FCNTL_H_INCLUDED) && (defined(WIN32) || defined(HAVE_FCNTL_H))# define WAR_FCNTL_H_INCLUDED# include <fcntl.h>#endif#if !defined(WAR_IO_H_INCLUDED) && defined(WIN32)# define WAR_IO_H_INCLUDED# include <io.h>#endif#if defined(HAVE_DIRENT_H) && !defined(WAR_DIRENT_H_INCLUDED)# define WAR_DIRENT_H_INCLUDED# include <dirent.h>#endif#if defined(HAVE_SYS_VFS_H) && !defined(WAR_SYS_VFS_H_INCLUDED)# define WAR_SYS_VFS_H_INCLUDED# include <sys/vfs.h>#endif#ifndef WAR_DIRLIST_H# include "WarDirList.h"#endif#ifndef WAR_FILE_DRIVER_FILE_UNIX_H# include "WarFileDriverFileUnix.h"#endif#ifndef WAR_MKDIR# ifdef WIN32# define WAR_MKDIR(name, perms) _tmkdir(name)# else# define WAR_MKDIR(name, perms) mkdir(name, perms)# endif#endif#ifndef WAR_RMDIR# ifdef WIN32# define WAR_RMDIR(name) _trmdir(name)# else# define WAR_RMDIR(name) rmdir(name)# endif#endif#ifndef WAR_UNLINK# ifdef WIN32# define WAR_UNLINK(name) _tunlink(name)# else# define WAR_UNLINK(name) unlink(name)# endif#endif#ifndef WAR_RENAME# ifdef WIN32# define WAR_RENAME(src, dst) _trename(src, dst)# else# define WAR_RENAME(src, dst) rename(src, dst)# endif#endif#ifndef WAR_STAT# ifdef WIN32# define WAR_STAT(name, st) _tstati64(name, st)# else# define WAR_STAT(name, st) stat(name, st)# endif#endif#ifndef WAR_OPEN# ifdef WIN32# define WAR_OPEN _topen# else# define WAR_OPEN open# endif#endif/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================WarFileDriverUnix::WarFileDriverUnix() :WarFileDriver("file"),mMkDirPerms(0775), mMkFilePerms(0664){}// WarFileDriverUnixWarFileDriverUnix::~WarFileDriverUnix(){}// ~WarFileDriverUnix//============================= OPERATORS ====================================//============================= OPERATIONS ===================================#if defined(HAVE_DIRENT_H)class UnixDirClass{public: UnixDirClass(DIR *pdir = NULL) : mpDir(pdir) { } ~UnixDirClass() { if (NULL != mpDir) { closedir(mpDir); mpDir = NULL; } } void operator = (DIR *pdir) { mpDir = pdir; } operator DIR *() { return mpDir; }private: DIR *mpDir;};void WarFileDriverUnix::WarListDirectory(const WarUrl& Path, WarDirList& listDestination) throw(WarException){ UnixDirClass pdir = opendir(Path.GetFilePath().GetPath()); if (NULL == pdir) WarThrow(WarSystemError(), "opendir()"); union { struct dirent dir; char extra_buffer[offsetof(struct dirent, d_name) + NAME_MAX + 1]; } entry_buffer; struct dirent *pent = NULL; war_stat_t st; memset(&st, 0, sizeof(st)); #define FILE_NAME pent->d_name while(readdir_r(pdir, &entry_buffer.dir, &pent) == 0) { war_svrpath_t file_name; file_name << FILE_NAME; if (listDestination.IsFileExcluded(file_name.GetPath())) continue; if (listDestination.DoesNeedStat()) { war_syspath_t full_path; full_path << Path.GetFilePath().GetPath() << WAR_SYSSLASH << FILE_NAME; if (WAR_STAT(full_path.GetPath(), &st) != 0) { WarThrow(WarSystemError(), "stat()"); } } listDestination.AddFile(file_name.GetPath(), &st); }}#endif // HAVE_DIRENT_H///void WarFileDriverUnix::WarDeleteFile(const WarUrl& Path) const throw(WarException){ int result = WAR_UNLINK(Path.GetFilePath().GetPath()); if (0 != result) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, errno), "unlink()");}///void WarFileDriverUnix::WarDeleteDirectory(const WarUrl& Path) constthrow(WarException){ int result = WAR_RMDIR(Path.GetFilePath().GetPath()); if (0 != result) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, errno), "rmdir()");}///void WarFileDriverUnix::WarCreateDirectory(const WarUrl& Path) constthrow(WarException){ int result = WAR_MKDIR(Path.GetFilePath().GetPath(), mMkDirPerms); if (0 != result) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, errno), "mkdir()");}///void WarFileDriverUnix::WarCopyFile(const WarUrl& From, const WarUrl& To) constthrow(WarException){ char buf[1024 * 16]; int from_fd = -1, to_fd = -1; int open_perms = O_RDONLY; #if defined(O_LARGEFILE) open_perms |= O_LARGEFILE;#endif#if defined(O_BINARY) open_perms |= O_BINARY; // Required for WIN32 !!#endif#if defined(O_SEQUENTIAL) open_perms |= O_SEQUENTIAL; // Cache hint WIN32#endif from_fd = WAR_OPEN(From.GetFilePath().GetPath(), open_perms); if (-1 == from_fd) goto failed; open_perms = O_WRONLY | O_TRUNC | O_CREAT;#if defined(O_LARGEFILE) open_perms |= O_LARGEFILE;#endif#if defined(O_BINARY) open_perms |= O_BINARY; // Required for WIN32 !!#endif#if defined(O_SEQUENTIAL) open_perms |= O_SEQUENTIAL; // Cache hint WIN32#endif to_fd = WAR_OPEN(To.GetFilePath().GetPath(), open_perms, mMkFilePerms); if (-1 == to_fd) goto failed; // Input and output file is opened. Copy the file. while(true) { int bytes = read(from_fd, buf, sizeof(buf)); if (0 < bytes) { int bytes_written = write(to_fd, buf, bytes); if (bytes_written != bytes) goto failed; // write error } else if (0 == bytes) break; // Done else goto failed; // Error on read } close(to_fd); close(from_fd); return;failed: WarError fail_err(WAR_ERR_SYSTEM_ERROR, errno); if (-1 != from_fd) close(from_fd); if (-1 != to_fd) close(to_fd); WAR_UNLINK(To.GetFilePath().GetPath()); WarThrow(fail_err, NULL);}///void WarFileDriverUnix::WarRenameDirectory(const WarUrl& From, const WarUrl& To) constthrow(WarException){ int result = WAR_RENAME(From.GetFilePath().GetPath(), To.GetFilePath().GetPath()); if (0 != result) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, errno), "rename()");}///void WarFileDriverUnix::WarMoveFile(const WarUrl& From, const WarUrl& To) constthrow(WarException){ int result = WAR_RENAME(From.GetFilePath().GetPath(), To.GetFilePath().GetPath()); if (0 != result) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, errno), "rename()");}///war_flen_t WarFileDriverUnix::WarGetFreeSpaceOnPath(const WarUrl& Path) constthrow(WarException){ war_flen_t free_bytes = 0;#if HAVE_STATFS struct statfs stf; if (statfs(Path.GetFilePath().GetPath(), &stf) != 0) { WarThrow(WarSystemError(), "statfs()"); } free_bytes = stf.f_bavail * stf.f_bsize;#else WarThrow(WarError(WAR_ERR_NOT_IMPLEMENTED), "WarGetFreeSpaceOnPath()");#endif return free_bytes;}///war_flen_t WarFileDriverUnix::WarGetSize(const WarUrl& Path) constthrow(WarException){ war_stat_t st; WarStat(Path, st); return st.st_size;}///void WarFileDriverUnix::WarStat(const WarUrl& Path, war_stat_t& st) constthrow(WarException){ memset(&st, 0, sizeof(st)); int result = WAR_STAT(Path.GetFilePath().GetPath(), &st); if (0 != result) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, errno), "stat()");}///WarFileDriverUnix::PathCmpTypeE WarFileDriverUnix::WarGetCmpMode(const WarUrl& Path) constthrow(WarException){ return PCMP_CASE_SENSITIVE;}/// WarFile helperWarFileDriverFile *WarFileDriverUnix::WarCreateNewFileObject(const WarUrl& fileUrl) constthrow(WarException){ return new WarFileDriverFileUnix((WarFileDriver *)this);}//============================= CALLBACK ===================================//============================= ACCESS ===================================//============================= INQUIRY ===================================int WarFileDriverUnix::GetDefaultNewFilePerms() const{ return mMkFilePerms;}int WarFileDriverUnix::GetDefaultNewDirectoryPerms() const{ return mMkDirPerms;}/////////////////////////////// PROTECTED ////////////////////////////////////////////////////////////////// PRIVATE ///////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -