File.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // "$Id: File.h 125715 2013-07-09 03:54:31Z sun_xiaohui $"
  3. //
  4. // Copyright (c)1992-2007, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef __INFRA3_FILE_H__
  11. #define __INFRA3_FILE_H__
  12. #include <string>
  13. #ifdef _WIN32
  14. #include <io.h>
  15. #endif
  16. #include "Defs.h"
  17. #include "String.h"
  18. namespace Dahua {
  19. namespace Infra {
  20. /// \struct FileInfo
  21. /// \brief 文件信息结构
  22. struct FileInfo
  23. {
  24. enum {maxPathSize = 260};
  25. char name[maxPathSize]; ///< 文件名
  26. uint16_t attrib; ///< 文件属性
  27. uint64_t timeCreate; ///< 文件创建时间
  28. uint64_t timeAccess; ///< 文件访问时间
  29. uint64_t timeWrite; ///< 文件修改时间
  30. uint64_t size; ///< 文件大小
  31. };
  32. /// \struct io_vec
  33. /// \brief io向量结构
  34. struct io_vec
  35. {
  36. void *iov_base; ///< 基本地址指针,指向缓冲区
  37. size_t iov_len; ///< 指定缓冲区长度
  38. };
  39. struct FileInternal;
  40. /// \class CFile
  41. /// \brief 文件服务封装
  42. ////////////////////////////////////////////////////////////////////////////////
  43. class INFRA_API CFile
  44. {
  45. CFile(CFile const&);
  46. CFile& operator=(CFile const&);
  47. public:
  48. /// 文件打开标志
  49. enum OpenFlags {
  50. modeRead = (int) 0x00000, ///< 为读打开一个文件,如果文件不存在或无法打开,Open调用失败。
  51. modeWrite = (int) 0x00001, ///< 为写创建一个空文件,如果文件存在,其内容将被销毁。
  52. modeReadWrite = (int) 0x00002, ///< 为读写打开一个文件,如果文件不存在或无法打开,Open调用失败。
  53. shareCompat = (int) 0x00000,
  54. shareExclusive = (int) 0x00010,
  55. shareDenyWrite = (int) 0x00020,
  56. shareDenyRead = (int) 0x00030,
  57. shareDenyNone = (int) 0x00040,
  58. modeNoInherit = (int) 0x00080,
  59. modeCreate = (int) 0x01000, ///< 如果文件不存在,自动创建文件,和modeReadWrite配合使用。
  60. modeNoTruncate = (int) 0x02000, ///< 和modeCreate一起使用,如果要创建的文件已经存在,原来的内容保留。
  61. typeText = (int) 0x04000, // typeText and typeBinary are
  62. typeBinary = (int) 0x08000, // used in derived classes only
  63. osNoBuffer = (int) 0x10000, ///< 上层处理缓冲,此时读写的偏移和长度都需要按页面大小对齐。
  64. osWriteThrough = (int) 0x20000,
  65. osRandomAccess = (int) 0x40000,
  66. osSequentialScan = (int) 0x80000,
  67. };
  68. /// 文件属性
  69. enum Attribute {
  70. // WIN32 兼容的文件属性掩码(废弃,使用 posix 兼容掩码代替)
  71. normal = 0x00,
  72. readOnly = 0x01,
  73. hidden = 0x02,
  74. system = 0x04,
  75. volume = 0x08,
  76. directory = 0x10,
  77. archive = 0x20,
  78. // posix 兼容的文件属性掩码
  79. posixFIFO = 0x1000,
  80. posixCharacter = 0x2000, ///< Character special (indicates a device if set)
  81. posixDirectory = 0x4000,
  82. posixRegular = 0x8000,
  83. };
  84. /// 文件定位参考位置
  85. enum SeekPosition
  86. {
  87. begin = 0x0,
  88. current = 0x1,
  89. end = 0x2
  90. };
  91. /// 文件访问方式
  92. enum AccessMode
  93. {
  94. accessExist = 0,
  95. accessWrite = 2,
  96. accessRead = 4,
  97. accessReadWrite = 6,
  98. };
  99. /// 构造函数。
  100. CFile();
  101. /// 析构函数。如果数据缓冲还有效,会被释放。
  102. virtual ~CFile();
  103. /// 打开文件。打开后文件指针偏移在0处,而以modeAppend标志打开的文件文件指针在文件末尾。
  104. /// \param [in] pFileName 文件名。
  105. /// \param [in] dwFlags 打开标志,默认为modeReadWrite。
  106. /// \retval true 打开成功
  107. /// \retval false 打开失败,文件不存在或无法打开。
  108. virtual bool open(const char* pFileName, uint32_t dwFlags = modeReadWrite);
  109. /// 关闭文件。
  110. virtual void close();
  111. /// 装载数据,申请和文件长度一样大小的缓冲,将文件内容读到该缓冲,返回缓冲指针。
  112. /// 和unload函数一起提供方便
  113. /// \param [in] pFileName 文件名。
  114. /// \retval NULL 装载失败
  115. /// \retval !NULL 数据缓冲指针。
  116. virtual uint8_t* load(const char* pFileName);
  117. /// 释放数据缓冲
  118. virtual void unload();
  119. /// 读文件数据。读操作后文件指针自动累加。实际仍然按照size_t来填充完成超过size_t数值的实现
  120. /// \param [out] pBuffer 数据缓冲的指针。
  121. /// \param [in] dwCount 要读出的字节数
  122. /// \retval >=0 读出的字节数
  123. /// \retval <0 读失败
  124. virtual int64_t read(void* pBuffer, int64_t dwCount);
  125. /// 写文件数据。读操作后文件指针自动累加。实际仍然按照size_t来填充完成超过size_t数值的实现
  126. /// \param [in] pBuffer 数据缓冲的指针。
  127. /// \param [in] dwCount 要写入的字节数
  128. /// \retval >=0 写入的字节数
  129. /// \retval <0 写失败
  130. virtual int64_t write(void *pBuffer, int64_t dwCount);
  131. /// 同步文件底层缓冲,在写操作后调用,确保写入的数据已经传给操作系统。
  132. virtual void flush();
  133. /// 文件定位。
  134. /// \param [in] lOff 偏移量,字节为单位。
  135. /// \param [in] nFrom 偏移相对位置,最后得到的偏移为lOff+nFrom。
  136. /// \return 偏移后文件的指针位置。
  137. virtual int64_t seek(int64_t lOff, SeekPosition nFrom);
  138. /// 返回当前的文件指针位置
  139. virtual int64_t getPosition();
  140. /// 返回文件长度
  141. virtual int64_t getLength();
  142. /// 从文本文件当前偏移处读取一行字符串。读操作后文件指针自动累加。
  143. /// \param [out] s 数据缓冲。
  144. /// \param [in] size 需要读取的字符串长度
  145. /// \retval NULL 读取失败
  146. /// \retval !NULL 字符串指针。
  147. virtual char* gets(char* s, int64_t size);
  148. /// 从文本文件当前偏移处写入一行字符串。写操作后文件指针自动累加。
  149. /// \param [in] s 数据缓冲。
  150. /// \return 实际写入字符串长度。
  151. virtual int64_t puts(const char* s);
  152. /// 判断文件是否打开
  153. virtual bool isOpen();
  154. /// 聚合写文件数据
  155. /// \param iov 聚合向量地址
  156. /// \param iovcnt 聚合向量元素个数
  157. /// \retval >=0 写入的字节数
  158. /// \retval <0 写失败
  159. virtual int64_t writev(const struct io_vec *iov, int iovcnt);
  160. /// 重命名文件
  161. /// \param oldName 旧的文件名
  162. /// \param oldName 新的文件名
  163. static bool rename(const char* oldName, const char* newName);
  164. /// 删除文件
  165. /// \param fileName 文件名
  166. static bool remove(const char* fileName);
  167. /// 创建目录
  168. /// \param 目录名
  169. static bool makeDirectory(const char* dirName);
  170. /// 删除目录
  171. /// \param 目录名
  172. static bool removeDirectory(const char* dirName);
  173. /// 文件系统统计
  174. /// \param path 任意路径,不一定是分区根目录。
  175. /// \param userFreeBytes 目录所在文件系统分配给该用户的剩余空间字节数
  176. /// \param totalBytes 目录所在文件系统总的字节数
  177. /// \param totalFreeBytes 目录所在文件系统总的剩余空间字节数,如果使用了硬盘
  178. /// 空间配额,userFreeBytes可能会比totalFreeBytes小
  179. static bool statFS(const char* path,
  180. uint64_t& userFreeBytes,
  181. uint64_t& totalBytes,
  182. uint64_t& totalFreeBytes);
  183. /// 判断文件或目录访问权限
  184. /// \param path 文件或目录的路径。
  185. /// \param mode 访问权限,\see AccessMode
  186. /// \return 是否有mode指定的权限
  187. static bool access(const char* path, int mode);
  188. /// 根据路径获取文件信息
  189. /// \param path 文件或目录的路径。
  190. /// \param info 文件信息,\see FileInfo
  191. /// \return 是否成功
  192. static bool stat(const char* path, FileInfo& info);
  193. protected:
  194. FileInternal* m_internal;
  195. };
  196. /////////////////////////////////////////////////////////////////////////////
  197. struct FileFindInternal;
  198. /// \brief 文件查找类,支持'*','?'通配符查找
  199. class INFRA_API CFileFind
  200. {
  201. CFileFind(CFileFind const&);
  202. CFileFind& operator=(CFileFind const&);
  203. public:
  204. /// 构造函数
  205. CFileFind();
  206. /// 析构函数
  207. virtual ~CFileFind();
  208. /// 查找第一个文件
  209. /// \param fileName 包含通配符的路径名
  210. /// \return 是否找到了第一个文件
  211. virtual bool findFile(const char* fileName);
  212. /// 查找下一个文件,使用和上次findFile相同的条件,必须在findFile之后调用
  213. /// \return 是否找到了下一个文件
  214. virtual bool findNextFile();
  215. /// 关闭查找,关闭后可以再次调用findFile
  216. virtual void close();
  217. /// 得到查找到的文件的长度
  218. virtual int64_t getLength();
  219. /// 得到查找到的文件的文件名
  220. virtual CString getFileName();
  221. /// 得到查找到的文件的全路径
  222. virtual CString getFilePath();
  223. /// 是否为只读文件
  224. virtual bool isReadOnly();
  225. /// 是否为目录文件
  226. virtual bool isDirectory();
  227. /// 是否为隐藏文件
  228. virtual bool isHidden();
  229. /// 是否为普通文件
  230. virtual bool isNormal();
  231. protected:
  232. FileFindInternal* m_internal;
  233. };
  234. ////////////////////////////////////////////////////////////////////////////////
  235. /// 文件系统操作函数集
  236. class IFileSystem
  237. {
  238. public:
  239. typedef void* FileHandle;
  240. virtual ~IFileSystem() {}
  241. virtual FileHandle fopen(const char*, const char*) = 0;
  242. virtual int fclose(FileHandle) = 0;
  243. virtual int64_t fread(void*, int64_t, int64_t, FileHandle) = 0;
  244. virtual int64_t fwrite(const void*, int64_t, int64_t, FileHandle) = 0;
  245. virtual int fflush(FileHandle) = 0;
  246. virtual int fseek(FileHandle, int64_t, int) = 0;
  247. virtual int64_t ftell(FileHandle) = 0;
  248. virtual char* fgets(char*, int64_t, FileHandle) = 0;
  249. virtual int fputs(const char*, FileHandle) = 0;
  250. virtual int rename(const char* oldname, const char* newname) = 0;
  251. virtual int remove(const char* path) = 0;
  252. virtual int64_t findfirst(const char*, FileInfo&) = 0;
  253. virtual int findnext(int64_t, FileInfo&) = 0;
  254. virtual int findclose(int64_t) = 0;
  255. virtual int mkdir( const char* dirname) = 0;
  256. virtual int rmdir( const char* dirname) = 0;
  257. virtual int statfs( const char* path, uint64_t& userFreeBytes, uint64_t& totalBytes, uint64_t& totalFreeBytes) = 0;
  258. virtual int access( const char* path, int mode) = 0;
  259. virtual int stat( const char* path, FileInfo&) = 0;
  260. virtual size_t writev(int fileds, const struct io_vec *iov, int iovcnt) = 0;
  261. };
  262. /// 为了兼容其他非内核文件系统,支持设置钩子接口,通过封装和模拟实现这些接口。
  263. /// \param path 匹配的路径
  264. /// \param filesystem 文件系统文件操作对象,在取消钩子之前,这个对象不能被销毁;
  265. /// 为 NULL 表示取消钩子
  266. void INFRA_API hookFileSystem(const char* path, IFileSystem* filesystem);
  267. } // namespace Infra
  268. } // namespace Dahua
  269. #endif //__INFRA_FILE_H__