summaryrefslogtreecommitdiffstats
path: root/Source/CPack/cmCPackTarCompressGenerator.cxx
blob: b73e553486294627a90bfa666c0bdf138a6a156d (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
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 notices for more information.

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

#include "cmCPackTarCompressGenerator.h"

#include "cmake.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmSystemTools.h"
#include "cmMakefile.h"
#include "cmGeneratedFileStream.h"
#include "cmCPackLog.h"

#include <cmsys/SystemTools.hxx>
#include <cmcompress/cmcompress.h>
#include <libtar/libtar.h>
#include <memory> // auto_ptr
#include <fcntl.h>
#include <errno.h>

//----------------------------------------------------------------------
class cmCPackTarCompressGeneratorForward
{
public:
  static int GenerateHeader(cmCPackTarCompressGenerator* gg, std::ostream* os)
    {
    return gg->GenerateHeader(os);
    }
};

//----------------------------------------------------------------------
cmCPackTarCompressGenerator::cmCPackTarCompressGenerator()
{
}

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

//----------------------------------------------------------------------
class cmCPackTarCompress_Data
{
public:
  cmCPackTarCompress_Data(cmCPackTarCompressGenerator* gen) :
    OutputStream(0), Generator(gen) {}
  std::ostream* OutputStream;
  cmCPackTarCompressGenerator* Generator;
  cmcompress_stream CMCompressStream;
};

//----------------------------------------------------------------------
extern "C" {
  // For cmTar
  int cmCPackTarCompress_Data_Open(void *client_data, const char* name,
    int oflags, mode_t mode);
  ssize_t cmCPackTarCompress_Data_Write(void *client_data, void *buff,
    size_t n);
  int cmCPackTarCompress_Data_Close(void *client_data);

  // For cmCompress
  int cmCPackTarCompress_Compress_Output(void* cdata, const char* data,
    int len);
}


//----------------------------------------------------------------------
int cmCPackTarCompress_Data_Open(void *client_data, const char* pathname,
  int, mode_t)
{
  cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;

  if ( !cmcompress_compress_initialize(&mydata->CMCompressStream) )
    {
    return -1;
    }

  mydata->CMCompressStream.client_data = mydata;
  mydata->CMCompressStream.output_stream = cmCPackTarCompress_Compress_Output;

  cmGeneratedFileStream* gf = new cmGeneratedFileStream;
  // Open binary
  gf->Open(pathname, false, true);
  mydata->OutputStream = gf;
  if ( !*mydata->OutputStream )
    {
    return -1;
    }

  if ( !cmcompress_compress_start(&mydata->CMCompressStream) )
    {
    return -1;
    }


  if ( !cmCPackTarCompressGeneratorForward::GenerateHeader(
      mydata->Generator,gf))
    {
    return -1;
    }

  return 0;
}

//----------------------------------------------------------------------
ssize_t cmCPackTarCompress_Data_Write(void *client_data, void *buff, size_t n)
{
  cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;

  if ( !cmcompress_compress(&mydata->CMCompressStream, buff, n) )
    {
    return 0;
    }
  return n;
}

//----------------------------------------------------------------------
int cmCPackTarCompress_Data_Close(void *client_data)
{
  cmCPackTarCompress_Data *mydata = (cmCPackTarCompress_Data*)client_data;

  if ( !cmcompress_compress_finalize(&mydata->CMCompressStream) )
    {
    delete mydata->OutputStream;
    return -1;
    }

  delete mydata->OutputStream;
  mydata->OutputStream = 0;
  return (0);
}

//----------------------------------------------------------------------
int cmCPackTarCompressGenerator::InitializeInternal()
{
  this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  return this->Superclass::InitializeInternal();
}

//----------------------------------------------------------------------
int cmCPackTarCompressGenerator::CompressFiles(const char* outFileName,
  const char* toplevel, const std::vector<std::string>& files)
{
  cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
                << (toplevel ? toplevel : "(NULL)") << std::endl);
  cmCPackTarCompress_Data mydata(this);
  TAR *t;
  char buf[TAR_MAXPATHLEN];
  char pathname[TAR_MAXPATHLEN];

  tartype_t compressType = {
    (openfunc_t)cmCPackTarCompress_Data_Open,
    (closefunc_t)cmCPackTarCompress_Data_Close,
    (readfunc_t)0,
    (writefunc_t)cmCPackTarCompress_Data_Write,
    &mydata
  };

  // 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 flags = O_WRONLY | O_CREAT;
  if (tar_open(&t, realName,
      &compressType,
      flags, 0644,
      (this->GeneratorVerbose?TAR_VERBOSE:0)
      | 0) == -1)
    {
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_open(): "
      << strerror(errno) << std::endl);
    return 0;
    }

  std::vector<std::string>::const_iterator fileIt;
  for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
    {
    std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
    strncpy(pathname, fileIt->c_str(), sizeof(pathname));
    pathname[sizeof(pathname)-1] = 0;
    strncpy(buf, rp.c_str(), sizeof(buf));
    buf[sizeof(buf)-1] = 0;
    if (tar_append_tree(t, pathname, buf) != 0)
      {
      cmCPackLogger(cmCPackLog::LOG_ERROR,
        "Problem with tar_append_tree(\"" << buf << "\", \""
        << pathname << "\"): "
        << strerror(errno) << std::endl);
      tar_close(t);
      return 0;
      }
    }
  if (tar_append_eof(t) != 0)
    {
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_append_eof(): "
      << strerror(errno) << std::endl);
    tar_close(t);
    return 0;
    }

  if (tar_close(t) != 0)
    {
    cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem with tar_close(): "
      << strerror(errno) << std::endl);
    return 0;
    }
  return 1;
}

//----------------------------------------------------------------------
int cmCPackTarCompress_Compress_Output(void* client_data,
  const char* data, int data_length)
{
  if(!client_data)
    {
    return 0;
    }
  cmcompress_stream *cstream = static_cast<cmcompress_stream*>(client_data);
  cmCPackTarCompress_Data *mydata
    = static_cast<cmCPackTarCompress_Data*>(cstream->client_data);
  if ( !mydata->OutputStream )
    {
    return 0;
    }
  mydata->OutputStream->write(data, data_length);
  return data_length;
}

//----------------------------------------------------------------------
int cmCPackTarCompressGenerator::GenerateHeader(std::ostream* os)
{
 (void)os;
  return 1;
}