summaryrefslogtreecommitdiff
path: root/TAO/tao/Storable_File_Guard.cpp
blob: 3c7139d3e71be708567c9d447144b42c740d4028 (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
//=============================================================================
/**
 *  @file   Storable_File_Guard.cpp
 *
 *  $Id$
 *
 *  @author Rich Seibel (seibelr@ociweb.com)
 *  @author Byron Harris (harrisb@ociweb.com)
 */
//=============================================================================

#include "tao/Storable_File_Guard.h"
#include "tao/Storable_Base.h"

#include "tao/SystemException.h"

TAO::Storable_File_Guard::
Storable_File_Guard (bool redundant)
  : redundant_ (redundant)
  , closed_ (1)
{
  ACE_TRACE (ACE_TEXT ("TAO::Storable_File_Guard::Storable_File_Guard"));
}

void
TAO::Storable_File_Guard::init (const char * mode)
{
  ACE_TRACE (ACE_TEXT ("TAO::Storable_File_Guard::init"));

  // We only accept a subset of mode argument, check it
  rwflags_ = 0;
  for ( unsigned int i = 0; i<ACE_OS::strlen (mode); i++ )
    {
      switch (mode[i])
        {
        case 'r': rwflags_ |= mode_read;
          break;
        case 'w': rwflags_ |= mode_write;
          break;
        case 'c': rwflags_ |= mode_create;
          break;
        default: rwflags_ = -1;
        }
    }
  if( rwflags_ <= 0 )
    {
      errno = EINVAL;
      throw CORBA::PERSIST_STORE ();
    }

  // Create the stream
  fl_ = this->create_stream (mode);
  if (redundant_)
    {
      if (fl_->open () != 0)
        {
          delete fl_;
          throw CORBA::PERSIST_STORE ();
        }

      // acquire a lock on it
      if (fl_ -> flock (0, 0, 0) != 0)
        {
          fl_->close ();
          delete fl_;
          throw CORBA::INTERNAL ();
        }

      // now that the file is successfully opened and locked it must be
      // unlocked/closed before we leave this class
      closed_ = 0;

      if ( ! (rwflags_ & mode_create) )
        {
          // Check if our copy is up to date
          if (this->object_obsolete ())
            {
              this->mark_object_current ();
              this->load_from_stream ();
            }
        }
    }
  else if ( ! this->is_loaded_from_stream () || (rwflags_ & mode_write) )
    {
      bool file_has_data = fl_->exists ();

      if (fl_->open () != 0)
        {
          delete fl_;
          throw CORBA::PERSIST_STORE ();
        }

      // now that the file is successfully opened
      // unlocked/closed before we leave this class
      closed_ = 0;

      if (file_has_data && ! this->is_loaded_from_stream ())
        {
          this->load_from_stream ();
        }
    }
  else
    {
      // Need to insure that fl_ gets deleted
      delete fl_;
    }
}

bool
TAO::Storable_File_Guard::object_obsolete (void)
{ // Default implementation uses time to determine
  // if obsolete.
  return (fl_->last_changed () > this->get_object_last_changed ());
}

void
TAO::Storable_File_Guard::mark_object_current (void)
{ // Default implementation is to set the last changed
  // time to that of the file lock.
  this->set_object_last_changed (fl_->last_changed ());
}

void
TAO::Storable_File_Guard::release (void)
{
  ACE_TRACE (ACE_TEXT ("TAO::Storable_File_Guard::release"));
  if ( ! closed_ )
    {
      // If we updated the disk, save the time stamp
      if(redundant_)
        {
          if( rwflags_ & mode_write )
            {
              // This is a costly call, but necessary if
              // we are running redundant. It ensures that
              // the data is written to disk.
              fl_->sync ();

              this->mark_object_current ();
            }

          // Release the lock
          fl_->funlock (0, 0, 0);
        }
      fl_->close ();
      delete fl_;
      closed_ = 1;
    }

}

TAO::Storable_Base &
TAO::Storable_File_Guard::peer ()
{
  return *fl_;
}

TAO::Storable_File_Guard::
~Storable_File_Guard ()
{
  ACE_TRACE (ACE_TEXT ("TAO::Storable_File_Guard::~Storable_File_Guard"));
}