summaryrefslogtreecommitdiff
path: root/trunk/CIAO/DAnCE/RepositoryManager/RM_Helper.cpp
blob: cd4b92669d0e4e2afd06eb583632ee0b49873128 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// $Id$

#include "RM_Helper.h"
#include "ace/Auto_Ptr.h"            //for Auto_Ptr
#include "ace/OS_NS_fcntl.h"         //for open
#include "ace/OS_NS_unistd.h"        //for close
#include "ace/OS_NS_sys_stat.h"      //for filesize and mkdir
#include "ace/OS_NS_string.h"


void
RM_Helper::pc_to_cdr (const Deployment::PackageConfiguration& pc, TAO_OutputCDR& cdr)
{
  cdr << pc;
}


void
RM_Helper::cdr_to_pc (Deployment::PackageConfiguration& pc, TAO_InputCDR& cdr)
{
  cdr >> pc;
}


bool
RM_Helper::externalize (const Deployment::PackageConfiguration& pc, const char* path)
{
  size_t bufsiz = 0;
  TAO_OutputCDR out (bufsiz);

  RM_Helper::pc_to_cdr (pc, out);

  const ACE_Message_Block* mb = out.begin ();

  return write_pc_to_disk (path, *(const_cast<ACE_Message_Block*> (mb)));
}


bool
RM_Helper::reincarnate (Deployment::PackageConfiguration& pc, const char* path)
{
  size_t length = 0;
  ACE_Auto_Ptr<ACE_Message_Block> mb (read_pc_from_disk (path, length));

  if (!mb.get ())
    return false;

  TAO_InputCDR in (mb.get ());

  RM_Helper::cdr_to_pc (pc, in);

  return true;
}


/// This function attempts to copy the file from a specified location
/// to another specified location on the hard disk.
bool
RM_Helper::copy_from_disk_to_disk (const char* from_path, const char* to_path)
{
  if (ACE_OS::strcmp (from_path, to_path) == 0)
    return true;

  // Open the files
  ACE_HANDLE from_handle = ACE_OS::open (from_path, O_RDONLY);
  if (from_handle == ACE_INVALID_HANDLE)
      ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("%p\n"),
                          ACE_TEXT ("[RM::copy_from_disk_to_disk] file open error")),
                          0);

  ACE_stat file_info;
  ACE_OS::fstat (from_handle, &file_info);
  ACE_UINT64 file_length = file_info.st_size;

  ACE_HANDLE to_handle = ACE_OS::open (to_path, O_CREAT | O_TRUNC | O_WRONLY);
  if (to_handle == ACE_INVALID_HANDLE)
      ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("%p\n"),
                          ACE_TEXT ("[RM::copy_from_disk_to_disk] file creation error")),
                          0);


  // Read the contents of the file into the buffer and write the data to another file
  ACE_Message_Block *mb = 0;
  size_t length;
  size_t number = 0;
  bool last = false;

  while (true)
  {
    if ((file_length - BUFSIZ*number) > BUFSIZ)
      length = BUFSIZ;
    else
    {
      length = static_cast<size_t> (file_length - BUFSIZ*number);
      last = true;
    }

    mb = new ACE_Message_Block (length);

    if (ACE_OS::read_n (from_handle, mb->wr_ptr (), length) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                            ACE_TEXT ("%p\n"),
                            ACE_TEXT ("[RM::copy_from_disk_to_disk] file read error")),
                            0);

    ++number;

    mb->length (length);

    for (ACE_Message_Block *curr = mb; curr != 0; curr = curr->cont ())
      if (ACE_OS::write_n (to_handle, curr->rd_ptr (), curr->length ()) == -1)
      {
        mb->release ();
        ACE_ERROR_RETURN ((LM_ERROR,
                            ACE_TEXT ("%p\n"),
                            ACE_TEXT ("[RM::copy_from_disk_to_disk] file write error")),
                            0);
      }

    mb->release ();

    if (last)
      break;
  }

  // Close the files
  ACE_OS::close (from_handle);
  ACE_OS::close (to_handle);

  return true;
}


/// This function attempts to write a sequence of bytes from an
/// ACE_Message_Block to a specified location. A 0 is returned
/// in the case of an error and a 1 upon success
bool RM_Helper::write_to_disk (
                 const char* full_path,
                 ACE_Message_Block& mb,
                 bool replace
                )
{
  ACE_stat stat;

  if (ACE_OS::stat(full_path, &stat) != -1 && !replace)
    return false;

  // Open a file handle to the local filesystem
  ACE_HANDLE handle = ACE_OS::open (full_path, O_CREAT | O_TRUNC | O_WRONLY);
  if (handle == ACE_INVALID_HANDLE)
      ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("%p\n"),
                          ACE_TEXT ("[RM::write_to_disk] file creation error")),
                          false);

  // Write the data to the file
  for (ACE_Message_Block * curr = &mb; curr != 0; curr = curr->cont ())
    if (ACE_OS::write_n (handle, curr->rd_ptr(), curr->length()) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("write error")),
                          false);

  // Close the file handle
  ACE_OS::close (handle);

  return true;
}


/// This function attempts to write a sequence of bytes from an
/// ACE_Message_Block to a specified location. A 0 is returned
/// in the case of an error and a 1 upon success
///
/// @note This function write the contents in a way that preserves the
/// structure of the ACE_Message_Block. It is relevant for
/// PackageConfigurations ONLY
bool RM_Helper::write_pc_to_disk (
                  const char* full_path,
                  ACE_Message_Block& mb,
                  bool replace
                 )
{
  ACE_stat stat;

  if (ACE_OS::stat(full_path, &stat) != -1 && !replace)
    return false;

  // Open a file handle to the local filesystem
  ACE_HANDLE const handle = ACE_OS::open (full_path, O_CREAT | O_TRUNC | O_WRONLY);
  if (handle == ACE_INVALID_HANDLE)
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("%p\n"),
                         ACE_TEXT ("[RM::write_to_disk] file creation error")),
                         false);

  // write the data to the file
  for (ACE_Message_Block * curr = &mb; curr != 0; curr = curr->cont ())
    if (ACE_OS::write_n (handle, curr->rd_ptr(), curr->length()) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("write error")),
                          0);

  // Close the file handle
  ACE_OS::close (handle);

  return true;
}


/// Function to read the contents of a file from disk into an ACE_Message_Block
/// returns a pointer to an ACE_Message_Block and updates the lenght on success
/// 0 on failure
ACE_Message_Block*
RM_Helper::read_pc_from_disk (
                           const char* full_path,
                           size_t &length
                          )
{
  length = 0;

  // Open the file
  ACE_HANDLE const handle = ACE_OS::open (full_path, O_RDONLY);
  if (handle == ACE_INVALID_HANDLE)
      ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("%p\n"),
                          ACE_TEXT ("[RM::read_mb_from_disk] file open error")),
                          0);

  ACE_stat file_info;
  ACE_OS::fstat (handle, &file_info);

  // Get and check the length of the file
  length = static_cast<size_t> (file_info.st_size);

  ACE_INT64 check = length;
  if (check != file_info.st_size)
  {
    length = 0;
    ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT ("%p\n"),
                          ACE_TEXT ("[RM::read_mb_from_disk] file length error")),
                          0);
  }

  // Read the contents of the file into the buffer
  ACE_Message_Block* mb = 0;
  ACE_NEW_RETURN (mb, ACE_Message_Block (length + 1), 0);

  if (ACE_OS::read_n (handle, mb->wr_ptr (), length) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
               ACE_TEXT ("[RM::read_mb_from_disk] file read error")),
                           0);

  mb->length (length);

  // Close the file handle
  ACE_OS::close (handle);

  return mb;
}