summaryrefslogtreecommitdiff
path: root/TAO/tao/Storable_FlatFileStream.h
blob: 9083f558ffc85b49961ab0fcddb8074c21ab7cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// -*- C++ -*-

//=============================================================================
/**
 * @file  Storable_FlatFileStream.h
 *
 * @author Marina Spivak <marina@cs.wustl.edu>
 * @author Byron Harris <harrisb@ociweb.com>
 */
//=============================================================================

#ifndef STORABLE_FLATFILESTREAM_H
#define STORABLE_FLATFILESTREAM_H

#include /**/ "ace/pre.h"
#include "ace/config-lite.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
#pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "tao/Storable_Base.h"
#include "tao/Storable_Factory.h"
#include "ace/OS_NS_stdio.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  /**
   * @brief A Storable_Base derived class that works with a file stream.
   */
  class TAO_Export Storable_FlatFileStream : public Storable_Base
  {
  public:

    Storable_FlatFileStream(const ACE_CString & file, const char * mode,
                            bool use_backup = Storable_Base::use_backup_default,
                            bool retry_on_ebadf = Storable_Base::retry_on_ebadf_default);

    virtual ~Storable_FlatFileStream();

    /// Check if a file exists on disk (file is not open)
    virtual int exists ();

    /// Open a file (the remaining methods below all require an open file)
    virtual int open ();

    /// When the locked file resides on an NFS mounted volume, occasionally
    /// the FD of the opened file becomes stale, when that happens the handle
    /// needs to be refreshed and the lock verified
    virtual int reopen ();

    /// Acquire a file lock
    virtual int close ();

    /// Acquire a file lock
    virtual int flock (int whence, int start, int len);

    /// Release a file lock
    virtual int funlock (int whence, int start, int len);

    /// Returns the last time an open file was changed
    virtual time_t last_changed (void);

    virtual void rewind (void);

    virtual bool flush (void);

    /// Force write of storable data to storage.
    /// Returns 0 on success, otherwise EOF
    virtual int sync (void);

    virtual Storable_Base& operator << (const ACE_CString&);
    virtual Storable_Base& operator >> (ACE_CString&);
    virtual Storable_Base& operator << (ACE_UINT32 );
    virtual Storable_Base& operator >> (ACE_UINT32 &);
    virtual Storable_Base& operator << (ACE_UINT64 );
    virtual Storable_Base& operator >> (ACE_UINT64 &);
    virtual Storable_Base& operator << (ACE_INT32 );
    virtual Storable_Base& operator >> (ACE_INT32 &);
    virtual Storable_Base& operator << (ACE_INT64 );
    virtual Storable_Base& operator >> (ACE_INT64 &);

    virtual Storable_Base& operator << (const TAO_OutputCDR & cdr);

    virtual size_t write (size_t size, const char * bytes);

    virtual size_t read (size_t size, char * bytes);

    virtual int restore_backup ();

  protected:
    virtual void do_remove ();

    virtual void remove_backup ();

    virtual int create_backup ();

  private:
    /// Throw a Storable_Read_Exception if the state
    /// is not good due to a read error.
    void throw_on_read_error (Storable_State state);

    /// Throw a Storable_Write_Exception if the state
    /// is not good due to a write error.
    void throw_on_write_error (Storable_State state);

    ACE_CString backup_file_name ();

    ACE_OS::ace_flock_t filelock_;
    FILE* fl_;
    ACE_CString file_;
    ACE_CString mode_;
  };

  class TAO_Export Storable_FlatFileFactory : public Storable_Factory
  {
  public:
    /// @param directory Directory to contain file passed in
    /// create_stream (). The directory is assumed to already exist.
    Storable_FlatFileFactory(const ACE_CString & directory,
                             bool use_backup = Storable_Base::use_backup_default);
    Storable_FlatFileFactory(const ACE_CString & directory,
                             bool use_backup,
                             bool retry_on_ebadf);

    const ACE_CString & get_directory () const;

    ~Storable_FlatFileFactory ();

  // Factory Methods

    /// Create the stream that can operate on a disk file
    virtual Storable_Base *create_stream (const ACE_CString & file,
                                          const char * mode,
                                          bool = false);
  private:
    static bool is_nfs (const ACE_CString &dir);
    ACE_CString directory_;
    bool use_backup_;
    bool retry_on_ebadf_;
  };
}

TAO_END_VERSIONED_NAMESPACE_DECL

#include /**/ "ace/post.h"

#endif /* STORABLE_FLATFILESTREAM_H */