summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/Random_File.cpp
blob: 9fbb6e11c20b8281b53fd7d1c93d8f93e99deba4 (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
// $Id$

#include "Random_File.h"

#include "ace/OS.h"
#include <tao/debug.h>
//#define DEBUG_LEVEL 9
#ifndef DEBUG_LEVEL
# define DEBUG_LEVEL TAO_debug_level
#endif //DEBUG_LEVEL

namespace TAO_Notify
{

Random_File::Random_File()
  : block_size_(512)
{
}

Random_File::~Random_File()
{
  this->close();
}

size_t
Random_File::block_size() const
{
  return this->block_size_;
}

size_t
Random_File::size() const
{
  Random_File * mutable_this = ACE_const_cast (Random_File *, this);
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, mutable_this->lock_, 0);
  size_t original_pos = mutable_this->tell ();
  mutable_this->ACE_FILE::seek(0, SEEK_END);
  size_t cursize = mutable_this->tell();
  mutable_this->ACE_FILE::seek (original_pos, SEEK_SET);
  if ((cursize % this->block_size_) != 0)
  {
    cursize += this->block_size_;
  }
  return cursize / this->block_size_;
}

bool
Random_File::open(const char* filename, size_t block_size)
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, false);
  this->block_size_ = block_size;
  bool result = (this->close() == 0);

  if (result)
  {
    if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
      ACE_TEXT ("(%P|%t) Opening file %s\n")
      , filename
      ));
    ACE_HANDLE handle = ACE_OS::open(filename,
      O_CREAT | O_RDWR | O_BINARY,
      ACE_DEFAULT_FILE_PERMS);

    if (handle == ACE_INVALID_HANDLE)
    {
      result = false;
    }
    else
    {
      this->set_handle(handle);
      if (this->get_handle() == 0)
      {
        result = false;
      }
      else
      {
        result = (this->addr_.set(filename) == 0);
      }
    }
  }
  return result;
}

bool
Random_File::write(const size_t block_number, void* buf, bool atomic)
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, false);
  if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
    ACE_TEXT ("(%P|%t) Write block %d %c\n"),
    ACE_static_cast (int, block_number),
    (atomic ? '*' : ' ')
    ));
  bool result = this->seek(block_number);
  if (result)
  {
    if (atomic)
    {
      // sync before so that any block pointed to from this block
      // will be there when this block is written.
      result = sync();
    }
    // ACE uses an ssize_t for buffer size, so we do this to make it happy.
    ssize_t block_size = this->block_size_;
    if (result && (block_size !=
      ACE_OS::write(this->get_handle(), buf, block_size)))
    {
      result = false;
    }
    if (result && atomic)
    {
      // sync after to provide the caller with a guarantee that
      // this block is physically written to the storage device.
      result = sync();
    }
  }
  return result;
}

bool
Random_File::read(const size_t block_number, void* buf)
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->lock_, false);
  if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
    ACE_TEXT ("(%P|%t) Read block %d\n"),
    ACE_static_cast (int, block_number)
    ));
  bool result = this->seek(block_number);
  if (result)
  {
    ssize_t block_size = this->block_size_;
    if (block_size !=
      ACE_OS::read(this->get_handle(), buf, block_size))
    {
      result = false;
    }
  }
  return result;
}

bool
Random_File::seek(const size_t block_number)
{
  ssize_t destloc = block_number * this->block_size_;
  bool result = (destloc == this->ACE_FILE::seek(destloc, SEEK_SET));
  return result;
}

bool
Random_File::sync()
{
  bool result = false;
  result = (0 == ACE_OS::fsync(this->get_handle()));
  return result;
}

} /* namespace TAO_Notify */

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */