summaryrefslogtreecommitdiff
path: root/TAO/tao/Storable_File_Guard.cpp
blob: 7ca4d772c7d93c19bb316341efebaae3dc4ad11d (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
//=============================================================================
/**
 *  @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("TAO::Storable_File_Guard::Storable_File_Guard");
}

void
TAO::Storable_File_Guard::init(const char * mode)
{
  ACE_TRACE("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
          time_t new_last_changed = fl_->last_changed();
          if ( new_last_changed > this->get_parent_last_changed ())
            {
              this->set_parent_last_changed (new_last_changed);
              this->create_child ();
            }
        }
    }
  else if ( ! this->is_child_created () || (rwflags_ & mode_write) )
    {
      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(! this->is_child_created ())
        {
          this->create_child ();
        }
    }
  else
    {
      // Need to insure that fl_ gets deleted
      delete fl_;
    }
}

void
TAO::Storable_File_Guard::release (void)
{
  ACE_TRACE("TAO::Storable_File_Guard::release");
  if ( ! closed_ )
    {
      // If we updated the disk, save the time stamp
      if(redundant_)
        {
          if( rwflags_ & mode_write )
            this->set_parent_last_changed (fl_->last_changed());
          fl_->funlock(0, 0, 0);
        }
      fl_->close();
      delete fl_;
      closed_ = 1;
    }
}

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

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