47 #if defined (HAVE_CONFIG_H) 74 #if defined (HAVE_BZLIB_H) 78 #if defined (HAVE_ZLIB_H) 97 CFile (
void) =
delete;
103 throw std::runtime_error (
"unable to open file");
119 throw std::runtime_error (
"unable to close file");
127 #if defined (HAVE_BZ2) 133 static const constexpr
char *extension =
".bz2";
138 bz2::zipper z (source_path, dest_path);
149 zipper (
void) =
delete;
152 : m_status (BZ_OK), m_source (source_path,
"rb"),
153 m_dest (dest_path,
"wb"),
154 m_bz (BZ2_bzWriteOpen (&m_status, m_dest.m_fp, 9, 0, 30))
156 if (m_status != BZ_OK)
157 throw std::runtime_error (
"failed to open bzip2 stream");
160 zipper (
const zipper&) =
delete;
162 zipper& operator = (
const zipper&) =
delete;
167 BZ2_bzWriteClose (&m_status, m_bz, 1,
nullptr,
nullptr);
172 const std::size_t buf_len = 8192;
175 while ((n_read = std::fread (buf,
sizeof (buf[0]), buf_len, m_source.m_fp)) != 0)
177 if (std::ferror (m_source.m_fp))
178 throw std::runtime_error (
"failed to read from source file");
179 BZ2_bzWrite (&m_status, m_bz, buf, n_read);
180 if (m_status == BZ_IO_ERROR)
181 throw std::runtime_error (
"failed to write or compress");
183 if (std::ferror (m_source.m_fp))
184 throw std::runtime_error (
"failed to read from source file");
189 int abandon = (m_status == BZ_IO_ERROR) ? 1 : 0;
190 BZ2_bzWriteClose (&m_status, m_bz, abandon,
nullptr,
nullptr);
191 if (m_status != BZ_OK)
192 throw std::runtime_error (
"failed to close bzip2 stream");
244 static const constexpr
char *extension =
".gz";
249 gz::zipper z (source_path, dest_path);
264 uchar_array (
void) =
delete;
268 p =
new Bytef[
str.length () + 1];
269 std::strcpy (reinterpret_cast<char *> (
p),
str.c_str ());
272 uchar_array (
const uchar_array&) =
delete;
274 uchar_array& operator = (
const uchar_array&) =
delete;
276 ~uchar_array (
void) {
delete[]
p; }
279 class gzip_header :
public gz_header
283 gzip_header (
void) =
delete;
286 : m_basename (sys::env::base_pathname (source_path))
288 const sys::file_stat source_stat (source_path);
290 throw std::runtime_error (
"unable to stat source file");
295 time = uLong (source_stat.mtime ().unix_time ());
341 #if defined (__WIN32__) 344 #elif defined (__APPLE__) 352 gzip_header (
const gzip_header&) =
delete;
354 gzip_header& operator = (
const gzip_header&) =
delete;
356 ~gzip_header (
void) =
default;
361 uchar_array m_basename;
368 zipper (
void) =
delete;
371 : m_source (source_path,
"rb"), m_dest (dest_path,
"wb"),
372 m_header (source_path), m_strm (new z_stream)
374 m_strm->zalloc = Z_NULL;
375 m_strm->zfree = Z_NULL;
376 m_strm->opaque = Z_NULL;
379 zipper (
const zipper&) =
delete;
381 zipper& operator = (
const zipper&) =
delete;
398 int status = deflateInit2 (m_strm, 8, Z_DEFLATED, 31, 8,
401 throw std::runtime_error (
"failed to open zlib stream");
403 deflateSetHeader (m_strm, &m_header);
405 const std::size_t buf_len = 8192;
406 unsigned char buf_in[buf_len];
407 unsigned char buf_out[buf_len];
413 m_strm->avail_in = std::fread (buf_in,
sizeof (buf_in[0]),
414 buf_len, m_source.m_fp);
416 if (std::ferror (m_source.m_fp))
417 throw std::runtime_error (
"failed to read source file");
419 m_strm->next_in = buf_in;
420 flush = (std::feof (m_source.m_fp) ? Z_FINISH : Z_NO_FLUSH);
427 m_strm->avail_out = buf_len;
428 m_strm->next_out = buf_out;
429 status = ::deflate (m_strm, flush);
430 if (status == Z_STREAM_ERROR)
431 throw std::runtime_error (
"failed to deflate");
433 std::fwrite (buf_out,
sizeof (buf_out[0]),
434 buf_len - m_strm->avail_out, m_dest.m_fp);
435 if (std::ferror (m_dest.m_fp))
436 throw std::runtime_error (
"failed to write file");
438 while (m_strm->avail_out == 0);
440 if (m_strm->avail_in != 0)
441 throw std::runtime_error (
"failed to write file");
443 }
while (flush != Z_FINISH);
445 if (status != Z_STREAM_END)
446 throw std::runtime_error (
"failed to write file");
451 if (deflateEnd (m_strm) != Z_OK)
452 throw std::runtime_error (
"failed to close zlib stream");
464 gzip_header m_header;
477 std::list<std::string> dest_paths;
479 std::function<void(const std::string&)> walk;
480 walk = [&walk, &mk_dest_path, &dest_paths] (
const std::string& path) ->
void 494 if (dirlist(
i) !=
"." && dirlist(
i) !=
"..")
504 X::zip (path, dest_path);
516 dest_paths.push_front (dest_path);
538 const std::function<std::string(const std::string&)> mk_dest_path
541 return source_path + ext;
543 return xzip<X> (source_patterns, mk_dest_path);
551 const std::function<std::string(const std::string&)> mk_dest_path
563 return xzip<X> (source_patterns, mk_dest_path);
575 = args(0).xcellstr_value (
"%s: FILES must be a character array or cellstr",
581 const std::string out_dir = args(1).string_value ();
615 return octave::xzip<octave::gz> (
"gzip", args);
619 octave_unused_parameter (args);
656 #if defined (HAVE_BZ2) 658 return octave::xzip<octave::bz2> (
"bzip2", args);
662 octave_unused_parameter (args);
string_vector xzip(const Array< std::string > &source_patterns, const std::function< std::string(const std::string &)> &mk_dest_path)
int unlink(const std::string &name)
OCTINTERP_API void print_usage(void)
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
std::string tilde_expand(const std::string &name)
static std::string basename(const std::string &s, bool strip_path=false)
CFile & operator=(const CFile &)=delete
std::string concat(const std::string &dir, const std::string &file)
nd deftypefn *std::string name
OCTAVE_EXPORT octave_value_list or both For fclose
RIIA wrapper for std::FILE*.
static std::string base_pathname(const std::string &s)
is longer than or if then or only for unique occurrences of the complete pattern(false). The default is true. If a cell array of strings ar
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
int mkdir(const std::string &nm, mode_t md)
octave_idx_type length(void) const
octave::sys::file_stat fs(filename)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin function.
octave_idx_type numel(void) const
Number of elements in the array.
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
CFile(const std::string &path, const std::string &mode)