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