Skip to content

Commit c48b90e

Browse files
committed
more path fixes
1 parent b4db982 commit c48b90e

10 files changed

Lines changed: 52 additions & 34 deletions

File tree

src/odr/internal/common/path.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ Path Path::join(const Path &b) const {
186186
}
187187

188188
Path Path::rebase(const Path &b) const {
189-
Path result = Path(m_absolute ? "/" : "");
189+
if (m_absolute != b.m_absolute) {
190+
throw std::invalid_argument("cannot rebase absolute and relative path");
191+
}
192+
193+
Path result = Path("");
190194
auto common_root = this->common_root(b);
191195

192196
Path sub_a;
@@ -217,7 +221,7 @@ Path Path::rebase(const Path &b) const {
217221
result.join_("..");
218222
}
219223

220-
for (auto part : sub_a) {
224+
for (const auto &part : sub_a) {
221225
result.join_(part);
222226
}
223227

@@ -251,7 +255,7 @@ Path Path::common_root(const Path &b) const {
251255

252256
Path::Iterator Path::begin() const { return Iterator(*this); }
253257

254-
Path::Iterator Path::end() const { return Iterator(); }
258+
Path::Iterator Path::end() const { return {}; }
255259

256260
Path::Iterator::Iterator() : m_path{nullptr}, m_begin{std::string::npos} {}
257261

src/odr/internal/odf/odf_crypto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ odf::decrypt(const std::shared_ptr<abstract::ReadableFilesystem> &filesystem,
170170
throw NotEncryptedError();
171171
}
172172

173-
if (auto it = manifest.entries.find(common::Path("encrypted-package"));
173+
if (auto it = manifest.entries.find(common::Path("/encrypted-package"));
174174
it != std::end(manifest.entries)) {
175175
try {
176176
const std::string start_key = odf::start_key(it->second, password);

src/odr/internal/odf/odf_document.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Document::Document(const FileType file_type, const DocumentType document_type,
1818
std::shared_ptr<abstract::ReadableFilesystem> filesystem)
1919
: common::TemplateDocument<Element>(file_type, document_type,
2020
std::move(filesystem)) {
21-
m_content_xml = util::xml::parse(*m_filesystem, common::Path("content.xml"));
21+
m_content_xml = util::xml::parse(*m_filesystem, common::Path("/content.xml"));
2222

23-
if (m_filesystem->exists(common::Path("styles.xml"))) {
24-
m_styles_xml = util::xml::parse(*m_filesystem, common::Path("styles.xml"));
23+
if (m_filesystem->exists(common::Path("/styles.xml"))) {
24+
m_styles_xml = util::xml::parse(*m_filesystem, common::Path("/styles.xml"));
2525
}
2626

2727
m_root_element = parse_tree(
@@ -43,33 +43,33 @@ void Document::save(const common::Path &path) const {
4343
zip::ZipArchive archive;
4444

4545
// `mimetype` has to be the first file and uncompressed
46-
if (m_filesystem->is_file(common::Path("mimetype"))) {
47-
archive.insert_file(std::end(archive), common::Path("mimetype"),
48-
m_filesystem->open(common::Path("mimetype")), 0);
46+
if (m_filesystem->is_file(common::Path("/mimetype"))) {
47+
archive.insert_file(std::end(archive), common::Path("/mimetype"),
48+
m_filesystem->open(common::Path("/mimetype")), 0);
4949
}
5050

51-
for (auto walker = m_filesystem->file_walker(common::Path(""));
51+
for (auto walker = m_filesystem->file_walker(common::Path("/"));
5252
!walker->end(); walker->next()) {
5353
auto p = walker->path();
54-
if (p == common::Path("mimetype")) {
54+
if (p == common::Path("/mimetype")) {
5555
continue;
5656
}
5757
if (m_filesystem->is_directory(p)) {
5858
archive.insert_directory(std::end(archive), p);
5959
continue;
6060
}
61-
if (p == common::Path("content.xml")) {
61+
if (p == common::Path("/content.xml")) {
6262
// TODO stream
6363
std::stringstream out;
6464
m_content_xml.print(out, "", pugi::format_raw);
6565
auto tmp = std::make_shared<common::MemoryFile>(out.str());
6666
archive.insert_file(std::end(archive), p, tmp);
6767
continue;
6868
}
69-
if (p == common::Path("META-INF/manifest.xml")) {
69+
if (p == common::Path("/META-INF/manifest.xml")) {
7070
// TODO
7171
auto manifest = util::xml::parse(*m_filesystem,
72-
common::Path("META-INF/manifest.xml"));
72+
common::Path("/META-INF/manifest.xml"));
7373

7474
for (auto &&node : manifest.select_nodes("//manifest:encryption-data")) {
7575
node.node().parent().remove_child(node.node());

src/odr/internal/odf/odf_file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ namespace odr::internal::odf {
1717
OpenDocumentFile::OpenDocumentFile(
1818
std::shared_ptr<abstract::ReadableFilesystem> filesystem)
1919
: m_filesystem{std::move(filesystem)} {
20-
if (m_filesystem->exists(common::Path("META-INF/manifest.xml"))) {
20+
if (m_filesystem->exists(common::Path("/META-INF/manifest.xml"))) {
2121
auto manifest =
22-
util::xml::parse(*m_filesystem, common::Path("META-INF/manifest.xml"));
22+
util::xml::parse(*m_filesystem, common::Path("/META-INF/manifest.xml"));
2323

2424
m_file_meta = parse_file_meta(*m_filesystem, &manifest, false);
2525
m_manifest = parse_manifest(manifest);

src/odr/internal/odf/odf_manifest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ Manifest parse_manifest(const pugi::xml_document &manifest) {
7474
std::optional<std::uint64_t> smallest_file_size;
7575

7676
for (auto &&e : manifest.child("manifest:manifest").children()) {
77-
const common::Path path =
77+
common::Path path =
7878
common::Path(e.attribute("manifest:full-path").as_string());
79+
if (path.relative()) {
80+
path = common::Path("/").join(path);
81+
}
7982
const pugi::xml_node crypto = e.child("manifest:encryption-data");
8083
if (!crypto) {
8184
continue;

src/odr/internal/odf/odf_meta.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,22 @@ FileMeta parse_file_meta(const abstract::ReadableFilesystem &filesystem,
6565

6666
result.password_encrypted = decrypted;
6767

68-
if (!filesystem.is_file(common::Path("content.xml")) && manifest == nullptr &&
69-
!filesystem.is_file(common::Path("mimetype"))) {
68+
if (!filesystem.is_file(common::Path("/content.xml")) &&
69+
manifest == nullptr && !filesystem.is_file(common::Path("/mimetype"))) {
7070
throw NoOpenDocumentFile();
7171
}
7272

73-
if (filesystem.is_file(common::Path("mimetype"))) {
73+
if (filesystem.is_file(common::Path("/mimetype"))) {
7474
const auto mimeType = util::stream::read(
75-
*filesystem.open(common::Path("mimetype"))->stream());
75+
*filesystem.open(common::Path("/mimetype"))->stream());
7676
lookup_file_type(mimeType, result.type);
7777
}
7878

7979
pugi::xml_document manifest_xml;
8080
if (manifest == nullptr &&
81-
filesystem.is_file(common::Path("META-INF/manifest.xml"))) {
81+
filesystem.is_file(common::Path("/META-INF/manifest.xml"))) {
8282
manifest_xml =
83-
util::xml::parse(filesystem, common::Path("META-INF/manifest.xml"));
83+
util::xml::parse(filesystem, common::Path("/META-INF/manifest.xml"));
8484
manifest = &manifest_xml;
8585
}
8686

@@ -112,9 +112,9 @@ FileMeta parse_file_meta(const abstract::ReadableFilesystem &filesystem,
112112
}
113113

114114
if ((result.password_encrypted == decrypted) &&
115-
filesystem.is_file(common::Path("meta.xml"))) {
115+
filesystem.is_file(common::Path("/meta.xml"))) {
116116
const auto meta_xml =
117-
util::xml::parse(filesystem, common::Path("meta.xml"));
117+
util::xml::parse(filesystem, common::Path("/meta.xml"));
118118

119119
const pugi::xml_node statistics = meta_xml.child("office:document-meta")
120120
.child("office:meta")

src/odr/internal/ooxml/ooxml_meta.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ FileMeta parse_file_meta(abstract::ReadableFilesystem &filesystem) {
1717
};
1818

1919
static const std::unordered_map<common::Path, TypeInfo> types = {
20-
{common::Path("word/document.xml"),
20+
{common::Path("/word/document.xml"),
2121
{FileType::office_open_xml_document, DocumentType::text}},
22-
{common::Path("ppt/presentation.xml"),
22+
{common::Path("/ppt/presentation.xml"),
2323
{FileType::office_open_xml_presentation, DocumentType::presentation}},
24-
{common::Path("xl/workbook.xml"),
24+
{common::Path("/xl/workbook.xml"),
2525
{FileType::office_open_xml_workbook, DocumentType::spreadsheet}},
2626
};
2727

src/odr/internal/ooxml/text/ooxml_text_document.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ Document::Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem)
2020
DocumentType::text,
2121
std::move(filesystem)) {
2222
m_document_xml =
23-
util::xml::parse(*m_filesystem, common::Path("word/document.xml"));
23+
util::xml::parse(*m_filesystem, common::Path("/word/document.xml"));
2424
m_styles_xml =
25-
util::xml::parse(*m_filesystem, common::Path("word/styles.xml"));
25+
util::xml::parse(*m_filesystem, common::Path("/word/styles.xml"));
2626

2727
m_document_relations =
28-
parse_relationships(*m_filesystem, common::Path("word/document.xml"));
28+
parse_relationships(*m_filesystem, common::Path("/word/document.xml"));
2929

3030
m_root_element =
3131
parse_tree(*this, m_document_xml.document_element().child("w:body"));
@@ -43,14 +43,14 @@ void Document::save(const common::Path &path) const {
4343
// TODO this would decrypt/inflate and encrypt/deflate again
4444
zip::ZipArchive archive;
4545

46-
for (auto walker = m_filesystem->file_walker(common::Path(""));
46+
for (auto walker = m_filesystem->file_walker(common::Path("/"));
4747
!walker->end(); walker->next()) {
4848
auto p = walker->path();
4949
if (m_filesystem->is_directory(p)) {
5050
archive.insert_directory(std::end(archive), p);
5151
continue;
5252
}
53-
if (p == common::Path("word/document.xml")) {
53+
if (p == common::Path("/word/document.xml")) {
5454
// TODO stream
5555
std::stringstream out;
5656
m_document_xml.print(out, "", pugi::format_raw);

src/odr/internal/zip/zip_archive.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ void ZipArchive::save(std::ostream &out) const {
9595

9696
for (auto &&entry : *this) {
9797
auto path = entry.path();
98+
if (path.absolute()) {
99+
path = path.rebase(common::Path("/"));
100+
}
98101

99102
if (entry.is_file()) {
100103
auto file = entry.file();

test/src/internal/common/path_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ TEST(Path, rebase) {
4545
Path("./ppt/media/image8.png").rebase(Path("ppt/media")).string());
4646
}
4747

48+
TEST(Path, rebase_to_relative) {
49+
EXPECT_EQ("mimetype", Path("/mimetype").rebase(Path("/")).string());
50+
}
51+
4852
TEST(Path, rebase_separate_tree) {
4953
EXPECT_EQ("../../other/directory",
5054
Path("ppt/media/other/directory")
@@ -57,6 +61,10 @@ TEST(Path, rebase_relative) {
5761
Path("../../other/directory").rebase(Path("../..")).string());
5862
}
5963

64+
TEST(Path, common_root_simple) {
65+
EXPECT_EQ("/", Path("/").common_root(Path("/mimetype")).string());
66+
}
67+
6068
TEST(Path, common_root) {
6169
EXPECT_EQ(
6270
"ppt/media",

0 commit comments

Comments
 (0)