PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/streams/file.h

http://github.com/mozy/mordor
C Header | 91 lines | 67 code | 16 blank | 8 comment | 3 complexity | d052072e1393d44847f89cbda136cbfa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_FILE_STREAM_H__
  2. #define __MORDOR_FILE_STREAM_H__
  3. // Copyright (c) 2009 - Mozy, Inc.
  4. #include "mordor/version.h"
  5. #ifdef WINDOWS
  6. #include "handle.h"
  7. #else
  8. #include <fcntl.h>
  9. #include "fd.h"
  10. #endif
  11. namespace Mordor {
  12. class FileStream : public NativeStream
  13. {
  14. public:
  15. typedef boost::shared_ptr<FileStream> ptr;
  16. #ifdef WINDOWS
  17. enum AccessFlags {
  18. READ = 0x01,
  19. WRITE = 0x02,
  20. READWRITE = 0x03,
  21. APPEND = 0x06,
  22. };
  23. enum CreateFlags {
  24. OPEN = OPEN_EXISTING,
  25. CREATE = CREATE_NEW,
  26. OPEN_OR_CREATE = OPEN_ALWAYS,
  27. OVERWRITE = TRUNCATE_EXISTING,
  28. OVERWRITE_OR_CREATE = CREATE_ALWAYS,
  29. DELETE_ON_CLOSE = 0x80000000
  30. };
  31. #else
  32. enum AccessFlags {
  33. READ = O_RDONLY,
  34. WRITE = O_WRONLY,
  35. READWRITE = O_RDWR,
  36. APPEND = O_APPEND | O_WRONLY
  37. };
  38. enum CreateFlags {
  39. /// Open a file. Fail if it does not exist.
  40. OPEN = 1,
  41. /// Create a file. Fail if it exists.
  42. CREATE,
  43. /// Open a file. Create it if it does not exist.
  44. OPEN_OR_CREATE,
  45. /// Open a file, and recreate it from scratch. Fail if it does not exist.
  46. OVERWRITE,
  47. /// Create a file. If it exists, recreate it from scratch.
  48. OVERWRITE_OR_CREATE,
  49. /// Delete the file when it is closed. Can be combined with any of the
  50. /// other options
  51. DELETE_ON_CLOSE = 0x80000000
  52. };
  53. #endif
  54. protected:
  55. FileStream();
  56. void init(const std::string &path,
  57. AccessFlags accessFlags = READWRITE, CreateFlags createFlags = OPEN,
  58. IOManager *ioManager = NULL, Scheduler *scheduler = NULL);
  59. void setSupportFlags(AccessFlags flags);
  60. void setPath(const std::string & path) { m_path = path; }
  61. public:
  62. FileStream(const std::string &path,
  63. AccessFlags accessFlags = READWRITE, CreateFlags createFlags = OPEN,
  64. IOManager *ioManager = NULL, Scheduler *scheduler = NULL)
  65. { init(path, accessFlags, createFlags, ioManager, scheduler); }
  66. bool supportsRead() { return m_supportsRead && NativeStream::supportsRead(); }
  67. bool supportsWrite() { return m_supportsWrite && NativeStream::supportsWrite(); }
  68. bool supportsSeek() { return m_supportsSeek && NativeStream::supportsSeek(); }
  69. std::string path() const { return m_path; }
  70. private:
  71. bool m_supportsRead, m_supportsWrite, m_supportsSeek;
  72. std::string m_path;
  73. };
  74. }
  75. #endif