50 #if defined (HAVE_CONFIG_H)
78 #if defined (HAVE_BZLIB_H)
82 #if defined (HAVE_ZLIB_H)
103 CFile (
const std::string&
path,
const std::string& mode)
107 throw std::runtime_error (
"unable to open file");
122 if (std::fclose (
m_fp))
123 throw std::runtime_error (
"unable to close file");
131 #if defined (HAVE_BZ2)
137 static const constexpr
char *extension =
".bz2";
139 static void zip (
const std::string& source_path,
140 const std::string& dest_path)
142 bz2::zipper z (source_path, dest_path);
153 zipper (
void) =
delete;
155 zipper (
const std::string& source_path,
const std::string& dest_path)
156 : m_status (BZ_OK), m_source (source_path,
"rb"),
157 m_dest (dest_path,
"wb"),
158 m_bz (BZ2_bzWriteOpen (&m_status, m_dest.m_fp, 9, 0, 30))
160 if (m_status != BZ_OK)
161 throw std::runtime_error (
"failed to open bzip2 stream");
164 zipper (
const zipper&) =
delete;
166 zipper& operator = (
const zipper&) =
delete;
171 BZ2_bzWriteClose (&m_status, m_bz, 1,
nullptr,
nullptr);
176 const std::size_t buf_len = 8192;
179 while ((n_read = std::fread (buf,
sizeof (buf[0]), buf_len, m_source.m_fp)) != 0)
181 if (std::ferror (m_source.m_fp))
182 throw std::runtime_error (
"failed to read from source file");
183 BZ2_bzWrite (&m_status, m_bz, buf, n_read);
184 if (m_status == BZ_IO_ERROR)
185 throw std::runtime_error (
"failed to write or compress");
187 if (std::ferror (m_source.m_fp))
188 throw std::runtime_error (
"failed to read from source file");
193 int abandon = (m_status == BZ_IO_ERROR) ? 1 : 0;
194 BZ2_bzWriteClose (&m_status, m_bz, abandon,
nullptr,
nullptr);
195 if (m_status != BZ_OK)
196 throw std::runtime_error (
"failed to close bzip2 stream");
248 static const constexpr
char *extension =
".gz";
250 static void zip (
const std::string& source_path,
251 const std::string& dest_path)
253 gz::zipper z (source_path, dest_path);
268 uchar_array (
void) =
delete;
270 uchar_array (
const std::string& str)
272 p =
new Bytef[str.length () + 1];
273 std::strcpy (
reinterpret_cast<char *
> (p), str.c_str ());
276 uchar_array (
const uchar_array&) =
delete;
278 uchar_array& operator = (
const uchar_array&) =
delete;
280 ~uchar_array (
void) {
delete[] p; }
283 class gzip_header :
public gz_header
287 gzip_header (
void) =
delete;
289 gzip_header (
const std::string& source_path)
290 : m_basename (sys::env::base_pathname (source_path))
292 const sys::file_stat source_stat (source_path);
294 throw std::runtime_error (
"unable to stat source file");
299 time = uLong (source_stat.mtime ().unix_time ());
345 #if defined (__WIN32__)
348 #elif defined (__APPLE__)
356 gzip_header (
const gzip_header&) =
delete;
358 gzip_header& operator = (
const gzip_header&) =
delete;
360 ~gzip_header (
void) =
default;
365 uchar_array m_basename;
372 zipper (
void) =
delete;
374 zipper (
const std::string& source_path,
const std::string& dest_path)
375 : m_source (source_path,
"rb"), m_dest (dest_path,
"wb"),
376 m_header (source_path), m_strm (new z_stream)
378 m_strm->zalloc = Z_NULL;
379 m_strm->zfree = Z_NULL;
380 m_strm->opaque = Z_NULL;
383 zipper (
const zipper&) =
delete;
385 zipper& operator = (
const zipper&) =
delete;
402 int status = deflateInit2 (m_strm, 8, Z_DEFLATED, 31, 8,
405 throw std::runtime_error (
"failed to open zlib stream");
407 deflateSetHeader (m_strm, &m_header);
409 const std::size_t buf_len = 8192;
410 unsigned char buf_in[buf_len];
411 unsigned char buf_out[buf_len];
417 m_strm->avail_in = std::fread (buf_in,
sizeof (buf_in[0]),
418 buf_len, m_source.m_fp);
420 if (std::ferror (m_source.m_fp))
421 throw std::runtime_error (
"failed to read source file");
423 m_strm->next_in = buf_in;
424 flush = (std::feof (m_source.m_fp) ? Z_FINISH : Z_NO_FLUSH);
431 m_strm->avail_out = buf_len;
432 m_strm->next_out = buf_out;
433 status = ::deflate (m_strm, flush);
434 if (status == Z_STREAM_ERROR)
435 throw std::runtime_error (
"failed to deflate");
437 std::fwrite (buf_out,
sizeof (buf_out[0]),
438 buf_len - m_strm->avail_out, m_dest.m_fp);
439 if (std::ferror (m_dest.m_fp))
440 throw std::runtime_error (
"failed to write file");
442 while (m_strm->avail_out == 0);
444 if (m_strm->avail_in != 0)
445 throw std::runtime_error (
"failed to write file");
447 }
while (flush != Z_FINISH);
449 if (status != Z_STREAM_END)
450 throw std::runtime_error (
"failed to write file");
455 if (deflateEnd (m_strm) != Z_OK)
456 throw std::runtime_error (
"failed to close zlib stream");
468 gzip_header m_header;
479 const std::function<std::string(
const std::string&)>& mk_dest_path)
481 std::list<std::string> dest_paths;
483 std::function<void(
const std::string&)> walk;
484 walk = [&walk, &mk_dest_path, &dest_paths] (
const std::string&
path) ->
void
499 if (dirlist(i) !=
"." && dirlist(i) !=
"..")
506 const std::string dest_path = mk_dest_path (
path);
509 X::zip (
path, dest_path);
521 dest_paths.push_front (dest_path);
542 const std::string ext = X::extension;
543 const std::function<std::string(
const std::string&)> mk_dest_path
544 = [&ext] (
const std::string& source_path) -> std::string
546 return source_path + ext;
548 return xzip<X> (source_patterns, mk_dest_path);
555 const std::string ext = X::extension;
556 const std::function<std::string(
const std::string&)> mk_dest_path
557 = [&out_dir, &ext] (
const std::string& source_path) -> std::string
562 (pos == std::string::npos ? source_path : source_path.substr (pos+1));
571 return xzip<X> (source_patterns, mk_dest_path);
579 if (nargin < 1 || nargin > 2)
583 = args(0).xcellstr_value (
"%s: FILES must be a character array or cellstr",
589 const std::string out_dir = args(1).string_value ();
623 return octave::xzip<octave::gz> (
"gzip", args);
627 octave_unused_parameter (args);
664 #if defined (HAVE_BZ2)
666 return octave::xzip<octave::bz2> (
"bzip2", args);
670 octave_unused_parameter (args);
octave_idx_type numel(void) const
Number of elements in the array.
string_vector glob(void) const
RIIA wrapper for std::FILE*.
CFile(const CFile &)=delete
CFile(const std::string &path, const std::string &mode)
CFile & operator=(const CFile &)=delete
octave_idx_type length(void) const
octave_idx_type numel(void) const
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin function.
OCTINTERP_API void print_usage(void)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
static std::string basename(const std::string &s, bool strip_path=false)
std::string dir_sep_str(void)
std::string tilde_expand(const std::string &name)
std::string concat(const std::string &dir, const std::string &file)
std::FILE * fopen(const std::string &filename, const std::string &mode)
int mkdir(const std::string &nm, mode_t md)
int unlink(const std::string &name)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
string_vector xzip(const Array< std::string > &source_patterns, const std::function< std::string(const std::string &)> &mk_dest_path)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))