📄 warfiledriverfileunix.cpp
字号:
#include "StdAfx.h"#include "WarFileDriverFileUnix.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_UNIX_H# include "WarFileDriverUnix.h"#endif#ifndef WAR_LOG_H# include "WarLog.h"#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_FSTAT# ifdef WIN32# define WAR_FSTAT(fd, st) _fstati64((int)fd, st)# else# define WAR_FSTAT(fd, st) fstat(fd, st)# endif#endif#ifndef WAR_OPEN# ifdef WIN32# define WAR_OPEN _topen# else# define WAR_OPEN open# endif#endif#if !defined(S_ISDIR) && defined(S_IFDIR)# define S_ISDIR(mode) ((mode & S_IFDIR) != 0)#endif#define Validate()\ if (!IsOpen()) WarThrow(WarError(WAR_ERR_NOT_OPEN), NULL)/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================WarFileDriverFileUnix::WarFileDriverFileUnix(WarFileDriver *pDriver) :WarFileDriverFile(pDriver),mFileHandle((war_filehandle_t)-1),mCurrentFileOffset(0){}WarFileDriverFileUnix::~WarFileDriverFileUnix(){ Close();}//============================= OPERATORS ====================================//============================= OPERATIONS ===================================void WarFileDriverFileUnix::Open(const WarUrl& openUrl, war_uint32_t openFlags)throw(WarException){ if (mFileHandle != (war_filehandle_t)-1) WarThrow(WarError(WAR_ERR_ALREADY_OPEN), NULL); war_syspath_t my_path = openUrl.GetFilePath().GetPath(); if (openFlags & F_CALLBACK) { WarThrow(WarError(WAR_ERR_NOT_IMPLEMENTED), "WarFileEnume::F_CALLBACK"); } war_stat_t st; bool does_exist = (WAR_STAT(my_path.GetPath(), &st) == 0); if (does_exist && (S_ISDIR(st.st_mode))) WarThrow(WarError(WAR_FERR_PATH_IS_A_DIR), NULL); if ((openFlags & F_NOTEXIST) && does_exist) WarThrow(WarError(WAR_ERR_OBJECT_EXIST), NULL); if ((openFlags & F_MUSTEXIST) && !does_exist) WarThrow(WarError(WAR_ERR_OBJECT_NOT_FOUND), NULL); if (!does_exist && !(openFlags & F_CREATE)) WarThrow(WarError(WAR_ERR_OBJECT_NOT_FOUND), NULL); int open_flags = 0;#if defined(O_BINARY) open_flags |= O_BINARY;#endif if (openFlags & (F_WRITE | F_APPEND | F_CREATE | F_TRUNCATE)) openFlags |= F_WRITE; if ((openFlags & F_READ) && !(openFlags & (F_WRITE))) open_flags |= O_RDONLY; else if ((openFlags & F_READ) && (openFlags & (F_WRITE))) open_flags |= O_RDWR; else if (!(openFlags & F_READ) && (openFlags & (F_WRITE))) open_flags |= O_WRONLY; else WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), "openFlags"); if (openFlags & F_TRUNCATE) open_flags |= O_TRUNC; if ((openFlags & F_CREATE) && !does_exist) open_flags |= O_CREAT; if (openFlags & F_APPEND) open_flags |= O_APPEND; if ((mFileHandle = (war_filehandle_t)WAR_OPEN(my_path.GetPath(), open_flags, ((WarFileDriverUnix &)GetDriver()).GetDefaultNewFilePerms())) < 0) { WarSystemError sys_err; WarLog debug_log(WARLOG_DEBUG, "WarFileDriverFileUnix::Open()"); if (debug_log) { debug_log << "Open '" << my_path.GetPath() << "' failed. Open flags: " << sys_err << GetDriver().WarExplainFags(openFlags) << war_endl; } WarThrow(sys_err, NULL); } if (openFlags & F_APPEND) Seek(0, WAR_SEEK_END); mFlags = openFlags;} void WarFileDriverFileUnix::Close() throw(WarException){ if (mFileHandle != (war_filehandle_t)-1) { close((int)mFileHandle); mFileHandle = (war_filehandle_t)-1; }}war_uint32_t WarFileDriverFileUnix::Read(war_cptr_t Buf, war_uint32_t Bytes) throw(WarException){ int bytes_to_read = (int)Bytes; if (bytes_to_read <= 0) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); int bytes_read = read((int)mFileHandle, (void *)Buf, bytes_to_read); if (bytes_read <= 0) { if ((Bytes > 0) && (bytes_read == 0)) return 0; WarThrow(WarSystemError(WAR_FERR_READ_FAILED), NULL); } mCurrentFileOffset += bytes_read; return (war_uint32_t)bytes_read;}void WarFileDriverFileUnix::Write(war_ccptr_t Buf, war_uint32_t Bytes) throw(WarException){ Validate(); int bytes_to_write = (int)Bytes; if (Bytes <= 0) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); int bytes_written = write((int)mFileHandle, (void *)Buf, bytes_to_write); if (bytes_written == -1) WarThrow(WarSystemError(WAR_FERR_WRITE_FAILED), NULL); if (bytes_to_write != bytes_written) { // Seek back to preserve file-offset integrity if (bytes_written) Seek(mCurrentFileOffset - bytes_written, WAR_SEEK_BEGIN); WarThrow(WarSystemError(WAR_FERR_WRITE_FAILED), NULL); } mCurrentFileOffset += bytes_written;}/*void WarFileDriverFileUnix::WriteWithCallback(war_transfer_buffer_ptr_t& outBuffer)throw(WarException){ }*//*void WarFileDriverFileUnix::ReadWithCallback(war_transfer_buffer_ptr_t& inBuffer) throw(WarException){}*/void WarFileDriverFileUnix::Flush() throw(WarException){ // Not supported, but don't throw... ;}war_flen_t WarFileDriverFileUnix::Seek (war_flen_t fileOffset, SeekModes seekMode) throw(WarException){ static int modes[] = {SEEK_SET, SEEK_CUR, SEEK_END}; Validate(); off_t where_to = (off_t)fileOffset; off_t where = lseek((int)mFileHandle, where_to, modes[seekMode]); if (where == (off_t)-1) WarThrow(WarSystemError(WAR_FERR_WRITE_FAILED), NULL); return mCurrentFileOffset = where;}war_flen_t WarFileDriverFileUnix::GetLength() throw(WarException){ war_stat_t st; if (WAR_FSTAT(mFileHandle, &st) != 0) WarThrow(WarSystemError(), "fstat()"); return (war_flen_t)st.st_size;}//============================= CALLBACK ===================================//============================= ACCESS ===================================//============================= INQUIRY ===================================bool WarFileDriverFileUnix::IsOpen() throw(WarException){ return (war_filehandle_t)-1 != mFileHandle;}/////////////////////////////// PROTECTED ////////////////////////////////////////////////////////////////// PRIVATE ///////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -