File: vtkKWRemoteIOUtilities.cxx

package info (click to toggle)
volview 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 25,204 kB
  • sloc: cpp: 132,585; ansic: 11,612; tcl: 236; sh: 64; makefile: 25; xml: 8
file content (376 lines) | stat: -rw-r--r-- 9,978 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*=========================================================================

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkKWRemoteIOUtilities.h"

// Includes needed for implementation of RenameFile.  This is not in
// system tools because it is not implemented robustly enough to move
// files across directories.
#ifdef _WIN32
# include <windows.h>
# include <sys/stat.h>
#endif

#include "vtkObjectFactory.h"
#include <libtar.h>
//#include <libtar/config.h>

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#if defined(_WIN32) && !defined(__CYGWIN__)
//#include <libtar/compat.h>
#include <io.h>
#else
#include <sys/param.h>
#endif

#ifdef STDC_HEADERS
# include <string.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
# include <stdlib.h>
#endif

#ifdef DEBUG
# include <signal.h>
#endif

# include <vtk_zlib.h>
# define cm_zlib_gzdopen gzdopen
# define cm_zlib_gzclose gzclose
# define cm_zlib_gzread gzread
# define cm_zlib_gzwrite gzwrite
#include <sstream>

#include <memory> // for auto_ptr

char *progname;
int mdttar_verbose = 0;
int use_gnu = 0;

#include "vtksys/SystemTools.hxx"

//----------------------------------------------------------------------------
vtkCxxRevisionMacro(vtkKWRemoteIOUtilities, "$Revision: 1.8 $");
vtkStandardNewMacro(vtkKWRemoteIOUtilities);

//----------------------------------------------------------------------------
vtkKWRemoteIOUtilities::vtkKWRemoteIOUtilities()
{
}

//----------------------------------------------------------------------------
vtkKWRemoteIOUtilities::~vtkKWRemoteIOUtilities()
{
}

//----------------------------------------------------------------------------
void vtkKWRemoteIOUtilities::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}


struct vtkKWRemoteIOUtilitiesGZStruct
{
  gzFile GZFile;
};

extern "C" {
  int vtkKWRemoteIOUtilitiesGZStructOpen(void* call_data, const char *pathname, 
                                int oflags, mode_t mode);
  int vtkKWRemoteIOUtilitiesGZStructClose(void* call_data);
  ssize_t vtkKWRemoteIOUtilitiesGZStructRead(void* call_data, void* buf, size_t count);
  ssize_t vtkKWRemoteIOUtilitiesGZStructWrite(void* call_data, const void* buf, 
                                     size_t count);
}

int vtkKWRemoteIOUtilitiesGZStructOpen(void* call_data, const char *pathname, 
                              int oflags, mode_t mode)
{
  const char *gzoflags;
  int fd;

  vtkKWRemoteIOUtilitiesGZStruct* gzf = static_cast<vtkKWRemoteIOUtilitiesGZStruct*>(call_data);

  switch (oflags & O_ACCMODE)
  {
  case O_WRONLY:
    gzoflags = "wb";
    break;
  case O_RDONLY:
    gzoflags = "rb";
    break;
  default:
  case O_RDWR:
    errno = EINVAL;
    return -1;
  }

  fd = open(pathname, oflags, mode);
  if (fd == -1)
    {
    return -1;
    }

// no fchmod on BeOS 5...do pathname instead.
#if defined(__BEOS__) && !defined(__ZETA__) 
  if ((oflags & O_CREAT) && chmod(pathname, mode))
    {
    return -1;
    }
#elif !defined(_WIN32) || defined(__CYGWIN__)
  if ((oflags & O_CREAT) && fchmod(fd, mode))
    {
    return -1;
    }
#endif

  gzf->GZFile = gzdopen(fd, gzoflags);
  if (!gzf->GZFile)
  {
    errno = ENOMEM;
    return -1;
  }

  return fd;
}

int vtkKWRemoteIOUtilitiesGZStructClose(void* call_data)
{
  vtkKWRemoteIOUtilitiesGZStruct* gzf = static_cast<vtkKWRemoteIOUtilitiesGZStruct*>(call_data);
  return gzclose(gzf->GZFile);
}

ssize_t vtkKWRemoteIOUtilitiesGZStructRead(void* call_data, void* buf, size_t count)
{
  vtkKWRemoteIOUtilitiesGZStruct* gzf = static_cast<vtkKWRemoteIOUtilitiesGZStruct*>(call_data);
  return gzread(gzf->GZFile, buf, static_cast<int>(count));
}

ssize_t vtkKWRemoteIOUtilitiesGZStructWrite(void* call_data, const void* buf,
                                   size_t count)
{
  vtkKWRemoteIOUtilitiesGZStruct* gzf = static_cast<vtkKWRemoteIOUtilitiesGZStruct*>(call_data);
  return gzwrite(gzf->GZFile, (void*)buf, static_cast<int>(count));
}
  
//----------------------------------------------------------------------------
int vtkKWRemoteIOUtilities::CreateTar(const char* outFileName, 
                              const std::vector<vtkstd::string> & files,
                              const char* rootDirectory,
                              bool gzip, bool verbose)
{
#if 0
  TAR *t;
  char buf[TAR_MAXPATHLEN];
  char pathname[TAR_MAXPATHLEN];
  vtkKWRemoteIOUtilitiesGZStruct gzs;

  tartype_t gztype = {
    (openfunc_t)vtkKWRemoteIOUtilitiesGZStructOpen,
    (closefunc_t)vtkKWRemoteIOUtilitiesGZStructClose,
    (readfunc_t)vtkKWRemoteIOUtilitiesGZStructRead,
    (writefunc_t)vtkKWRemoteIOUtilitiesGZStructWrite,
    &gzs
  };

  // Ok, this libtar is not const safe. for now use auto_ptr hack
  char* realName = new char[ strlen(outFileName) + 1 ];
  std::auto_ptr<char> realNamePtr(realName);
  strcpy(realName, outFileName);
  int options = 0;
  if(verbose)
    {
    options |= TAR_VERBOSE;
    }
#ifdef __CYGWIN__
  options |= TAR_GNU;
#endif 
  if (tar_open(&t, realName,
      (gzip? &gztype : NULL),
      O_WRONLY | O_CREAT, 0644,
      options) == -1)
    {
    vtkGenericWarningMacro("Problem with tar_open(): ");
    return 0;
    }

  std::vector<vtkstd::string>::const_iterator it;
  for (it = files.begin(); it != files.end(); ++ it )
    {
    strncpy(pathname, it->c_str(), sizeof(pathname));
    pathname[sizeof(pathname)-1] = 0;
    if (pathname[0] != '/' && rootDirectory != NULL)
      {
      snprintf(buf, sizeof(buf), "%s/%s", rootDirectory, pathname);
      }
    else
      {
      strncpy(buf, pathname, sizeof(buf));
      }
    buf[sizeof(buf)-1] = 0;
    if (tar_append_tree(t, buf, pathname) != 0)
      {
      std::ostringstream ostr;
      ostr << "Problem with tar_append_tree(\"" << buf << "\", \"" 
           << pathname << "\"): " << std::ends;
      vtkGenericWarningMacro( << ostr.str().c_str());
      tar_close(t);
      return 0;
      }
    }

  if (tar_append_eof(t) != 0)
    {
    vtkGenericWarningMacro("Problem with tar_append_eof(): ");
    tar_close(t);
    return 0;
    }

  if (tar_close(t) != 0)
    {
    vtkGenericWarningMacro("Problem with tar_close(): ");
    return 0;
    }
#endif
  return 1;
}

//----------------------------------------------------------------------------
int vtkKWRemoteIOUtilities::
ExtractTar( const char *outFileName, 
            const char *root_name, 
            bool gzip)
{
#if 0
  TAR *t;

  vtkKWRemoteIOUtilitiesGZStruct gzs;

  tartype_t gztype = {
    vtkKWRemoteIOUtilitiesGZStructOpen,
    vtkKWRemoteIOUtilitiesGZStructClose,
    vtkKWRemoteIOUtilitiesGZStructRead,
    vtkKWRemoteIOUtilitiesGZStructWrite,
    &gzs
  };
  
  // Ok, this libtar is not const safe. for now use auto_ptr hack
  char* realName = new char[ strlen(outFileName) + 1 ];
  std::auto_ptr<char> realNamePtr(realName);
  strcpy(realName, outFileName);

#if 0
  cout << "About to open: " << realName << ", gzip: " << gzip << endl;
  if (!vtksys::SystemTools::FileExists(realName))
    {
    cout << "File does not exist: " << realName << endl;
    }
  cout << "File length: " << vtksys::SystemTools::FileLength(realName) << endl;
#endif
     
  if (tar_open(&t, realName,
      (gzip ? &gztype : NULL),
      O_RDONLY
#ifdef _WIN32
      | O_BINARY
#endif
      , 0, 0))
    {
    vtkGenericWarningMacro("Problem with tar_open(): ");
    return 0;
    }

  bool extracted = false;
  if (root_name)
    {
    char* realRootName = new char[ strlen(root_name) + 1 ];
    std::auto_ptr<char> realRootNamePtr(realRootName);
    strcpy(realRootName, root_name);
    extracted = (tar_extract_all(t, realRootName ) == 0);
    }
  else
    {
    extracted = (tar_extract_all(t, NULL ) == 0);
    }

  if (!extracted)
    {
    vtkGenericWarningMacro("Problem with tar_extract_all(): ");
    return 0;
    }

  if (tar_close(t) != 0)
    {
    vtkGenericWarningMacro( << "Problem with tar_close(): " );
    return 0;
    }
#endif
  return 1;
}


//----------------------------------------------------------------------------
int vtkKWRemoteIOUtilities::RenameFile(const char* oldname,
                                       const char* newname)
{
#ifdef _WIN32
  /* On Windows the move functions will not replace existing files.
     Check if the destination exists.  */
  struct stat newFile;
  if(stat(newname, &newFile) == 0)
    {
    /* The destination exists.  We have to replace it carefully.  The
       MoveFileEx function does what we need but is not available on
       Win9x.  */
    OSVERSIONINFO osv;
    DWORD attrs;

    /* Make sure the destination is not read only.  */
    attrs = GetFileAttributes(newname);
    if(attrs & FILE_ATTRIBUTE_READONLY)
      {
      SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
      }

    /* Check the windows version number.  */
    osv.dwOSVersionInfoSize = sizeof(osv);
    GetVersionEx(&osv);
    if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
      {
      /* This is Win9x.  There is no MoveFileEx implementation.  We
         cannot quite rename the file atomically.  Just delete the
         destination and then move the file.  */
      DeleteFile(newname);
      return MoveFile(oldname, newname);
      }
    else
      {
      /* This is not Win9x.  Use the MoveFileEx implementation.  */
      return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING);
      }
    }
  else
    {
    /* The destination does not exist.  Just move the file.  */
    return MoveFile(oldname, newname);
    }
#else
  /* On UNIX we have an OS-provided call to do this atomically.  */
  return rename(oldname, newname) == 0;
#endif
}