summaryrefslogtreecommitdiff
path: root/TAO/tao/Storable_Base.h
blob: 14f4f2af7f1c9d5e55f9fecb8ca4392dbd8b8dcb (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// -*- C++ -*-

//=============================================================================
/**
 *  @file   Storable_Base.h
 *
 *  @author Bruce Trask <trask_b@ociweb.com>
 *  @author Chanaka Liyanaarachchi <chanaka@ociweb.com>
 *  @author Byron Harris <harrisb@ociweb.com>
 */
//=============================================================================

#ifndef TAO_STORABLE_BASE_H
#define TAO_STORABLE_BASE_H

#include "tao/TAO_Export.h"
#include "tao/orbconf.h"
#include "tao/CDR.h"

#include "ace/SString.h"

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

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{

  class TAO_Export Storable_Base
  {
  public:

    Storable_Base (bool use_backup, bool retry_ebadf);

    virtual ~Storable_Base ();

    /// The process-wide default policy for doing a backup when close ()
    /// is called.
    /// The backup can then be restored if restore_backup () is called.
    /// The initial value for the default is false.
    static bool use_backup_default;

    /// The process-wide default policy for retring certain flock operations
    /// if an ebadf is returned. This can happen spurously on nfs mounted
    /// file.
    static bool retry_on_ebadf_default;

    bool use_backup ();

    bool retry_on_ebadf ();

    /// Remove the file that is assumed to not be open.
    /// If backup are used, the backup will also be removed.
    void remove();

    virtual int create_backup () = 0;

    virtual int exists() = 0;

    virtual int open () = 0;

    virtual int close () = 0;

    virtual int flock (int whence, int start, int len) = 0;

    virtual int funlock (int whence, int start, int len) = 0;

    virtual time_t last_changed(void) = 0;

    // Mimic a portion of the std::ios interface.  We need to be able
    // to indicate error states from the extraction operators below.
    enum Storable_State { goodbit = 0,
                          badbit  = 1,
                          eofbit  = 2,
                          failbit = 4
    };

    void clear (Storable_State state = goodbit);

    void setstate (Storable_State state);

    Storable_State rdstate (void) const;

    bool good (void) const;

    bool bad (void) const;

    bool eof (void) const;

    bool fail (void) const;

    static ACE_CString state_as_string (Storable_State state);

    virtual void rewind (void) = 0;

    virtual bool flush (void) = 0;

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

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

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

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

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

    virtual int restore_backup () = 0;

  protected:

    virtual void do_remove () = 0;

    /// If a backup file exists, remove it.
    virtual void remove_backup () = 0;

    bool use_backup_;
    bool retry_on_ebadf_;

  private:
    Storable_State state_;

  };

  /// Base class for exceptions thrown when encountering
  /// errors working with persistent files.
  class TAO_Export Storable_Exception
  {
  public:
    Storable_Exception (const ACE_CString & file_name);

    virtual ~Storable_Exception ();

    const ACE_CString & get_file_name () const;

  private:
    ACE_CString file_name_;
  };

  /// Exception thrown when an error is encountered
  /// during reading of the persistent store.
  class TAO_Export Storable_Read_Exception : public Storable_Exception
  {
  public:
    Storable_Read_Exception (Storable_Base::Storable_State state,
                             const ACE_CString & file_name);

    Storable_Base::Storable_State get_state () const;

  private:
    TAO::Storable_Base::Storable_State storable_state_;
  };

  /// Exception thrown when an error is encountered
  /// during writing to the persistent store.
  class TAO_Export Storable_Write_Exception : public Storable_Exception
  {
  public:
    Storable_Write_Exception (Storable_Base::Storable_State state,
                              const ACE_CString & file_name);

    Storable_Base::Storable_State get_state () const;

  private:
    TAO::Storable_Base::Storable_State storable_state_;
  };

}

TAO_END_VERSIONED_NAMESPACE_DECL

#if defined (__ACE_INLINE__)
#include "tao/Storable_Base.inl"
#endif


#endif