summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/RepositoryManager/RM_Helper.cpp
blob: 9c14b6b9608e8ef4b4ef99a0fec523a0200b7cae (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
// $Id$

#include "RM_Helper.h"
#include "ciao/Packaging_DataC.h"    //for the PackageConfiguration declaration
#include "tao/CDR.h"                 //for TAO CDR classes
#include "ace/Message_Block.h"       //for ACE_Message_Block
#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


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;
}


//---------------------------------------------------------------------
//These are a bit obsolete but until I am sure I will keep them

//This function attempts to write a sequence of bytes 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,
                 const CORBA::Octet* buffer,
                 size_t length
                )
{

  // 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")),
                           0);

  //write the data to the file
  if (ACE_OS::write (handle, buffer, length) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
               ACE_TEXT ("[RM::write_to_disk] file write error")),
                           0);

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

  return 1;
}


//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 0;

  // 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")),
                           0);

  //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 1;
}


//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 0;

  // 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")),
                         0);

  //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 1;
}


//This function attempts to read a sequence of bytes from a specified
//location and returns an octet sequence. A 0 is returned
//in the case of an error and a 1 upon success

CORBA::Octet* RM_Helper::read_from_disk (
                     const char* full_path,
                      size_t &length
                    )
{
  //open the file
  ACE_HANDLE 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_from_disk] file open error")),
                           0);

  ACE_stat file_info;

  ACE_OS::fstat (handle, &file_info);

  CORBA::Octet* buffer;
  ACE_NEW_RETURN (buffer, CORBA::Octet[file_info.st_size], 0);

  //read the contents of the file into the buffer
  if (ACE_OS::read_n (handle, buffer, file_info.st_size) == -1)
     ACE_ERROR_RETURN ((LM_ERROR,
                        ACE_TEXT ("%p\n"),
                        ACE_TEXT ("[RM::read_from_disk] file read error")),
                        0);

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

  length = file_info.st_size;
  return buffer;
}



  ///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 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);

  ACE_Message_Block* mb;
  ACE_NEW_RETURN (mb, ACE_Message_Block (file_info.st_size + 1), 0);

  //read the contents of the file into the buffer
  if (ACE_OS::read_n (handle, mb->wr_ptr (), file_info.st_size) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
               ACE_TEXT ("[RM::read_mb_from_disk] file read error")),
                           0);

  mb->length (file_info.st_size);

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

  length = file_info.st_size;
  return mb;
}