| 4 |
<!-- |
<!-- |
| 5 |
Hamigaki.Archivers Library Document Source |
Hamigaki.Archivers Library Document Source |
| 6 |
|
|
| 7 |
Copyright Takeshi Mouri 2006-2008. |
Copyright Takeshi Mouri 2006-2018. |
| 8 |
Distributed under the Boost Software License, Version 1.0. |
Distributed under the Boost Software License, Version 1.0. |
| 9 |
(See accompanying file LICENSE_1_0.txt or copy at |
(See accompanying file LICENSE_1_0.txt or copy at |
| 10 |
http://www.boost.org/LICENSE_1_0.txt) |
http://www.boost.org/LICENSE_1_0.txt) |
| 78 |
namespace fs = boost::filesystem; |
namespace fs = boost::filesystem; |
| 79 |
namespace io = boost::iostreams; |
namespace io = boost::iostreams; |
| 80 |
|
|
| 81 |
|
bool is_valid_path(const fs::path& ph) |
| 82 |
|
{ |
| 83 |
|
if (ph.has_root_name() || ph.has_root_directory()) |
| 84 |
|
return false; |
| 85 |
|
for (typename Path::iterator it = ph.begin(); it != ph.end(); ++it) |
| 86 |
|
{ |
| 87 |
|
if (*it == "..") |
| 88 |
|
return false; |
| 89 |
|
} |
| 90 |
|
return true; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
void extract_file(ar::tar_file_source& tar) |
void extract_file(ar::tar_file_source& tar) |
| 94 |
{ |
{ |
| 95 |
const ar::tar::header& head = tar.header(); |
const ar::tar::header& head = tar.header(); |
| 96 |
|
if (!is_valid_path(head.path)) |
| 97 |
|
{ |
| 98 |
|
std::cerr << "Warning: skip invalid path: " << head.path.string() << std::endl; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
fs::ofstream file(head.path, std::ios_base::binary); |
fs::ofstream file(head.path, std::ios_base::binary); |
| 102 |
io::copy(tar, file); |
io::copy(tar, file); |
| 115 |
|
|
| 116 |
<para>tarファイルのオープンに成功したら、メンバ関数<methodname>next_entry</methodname>()を呼び出し、次のエントリを確認する。この関数は未読のエントリがある場合<code>true</code>を返す。全てのエントリを読み終わり、これ以上のエントリが見つからない場合は<code>false</code>が返され、ループは終了する。</para> |
<para>tarファイルのオープンに成功したら、メンバ関数<methodname>next_entry</methodname>()を呼び出し、次のエントリを確認する。この関数は未読のエントリがある場合<code>true</code>を返す。全てのエントリを読み終わり、これ以上のエントリが見つからない場合は<code>false</code>が返され、ループは終了する。</para> |
| 117 |
|
|
| 118 |
<para><methodname>next_entry</methodname>()の呼び出しに成功したら、アーカイブは<conceptname>Source</conceptname>として読み出し可能である。また、メンバ関数<methodname>header</methodname>()を呼び出すことで、ヘッダ情報を取得することもできる。ここでは、ヘッダから取得したパス名と同名のファイルを作成し、エントリから読み出した内容を書き出している。</para> |
<para><methodname>next_entry</methodname>()の呼び出しに成功したら、アーカイブは<conceptname>Source</conceptname>として読み出し可能である。また、メンバ関数<methodname>header</methodname>()を呼び出すことで、ヘッダ情報を取得することもできる。ここでは、ヘッダから取得したパス名と同名のファイルを作成し、エントリから読み出した内容を書き出している。特に意図していない限り、絶対パスやカレントディレクトリより上への相対パスを含むアーカイブは悪意のあるものである。ファイルシステムへ出力する前には必ずパス名を検証する必要がある。</para> |
| 119 |
</section> |
</section> |
| 120 |
|
|
| 121 |
<section id="archivers.tutorial.zip"> |
<section id="archivers.tutorial.zip"> |