Skip to content

Commit 0829896

Browse files
authored
Merge 67670e6 into 20ac4bf
2 parents 20ac4bf + 67670e6 commit 0829896

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

README.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,52 @@ Hyperpack is a command line utility used to create a hyperpage database
3838
file:
3939

4040
```
41-
Usage: hyperpack [--help] [--output VAR] directory
41+
Usage: hyperpack [--help] [--version] [--output VAR] [--verbose] directories...
4242
4343
Positional arguments:
44-
directory Directory to scan for files to pack into the hyperpage database [required]
44+
directories Directories to scan for files to pack into the hyperpage database [nargs: 1 or more] [required]
4545
4646
Optional arguments:
4747
-h, --help shows help message and exits
48+
-v, --version prints version information and exits
4849
-o, --output Output file for the hyperpage database [nargs=0..1] [default: "hyperpage.db"]
50+
-v, --verbose Show detailed output information
4951
```
5052

51-
It will iterate through a given directory, storing its files based on
52-
paths relative to the given directory. It will also detect the mime
53-
type and store the contents of each file.
53+
### Note on Overwriting
5454

55+
If two or more files share the same **relative subpath** (i.e., the same path within their respective parent directories), the file from the **rightmost directory** specified on the command line will overwrite the others in the final archive.
56+
57+
Only **exact path matches** are considered conflicts — differing subdirectories or filenames will coexist as separate entries.
58+
59+
#### Example
60+
61+
Suppose you run:
62+
```bash
63+
hyperpack -o output.hp dir1 dir2 dir3
64+
```
65+
And the directories contain:
66+
```bash
67+
dir1/Subdir1/index.html
68+
dir2/Subdir2/index.html
69+
dir3/Index.html
70+
```
71+
These will result in three distinct files inside the archive:
72+
```bash
73+
Subdir1/index.html
74+
/Subdir2/index.html
75+
/Index.html
76+
```
77+
However, if two or more directories contain the same relative path, for example:
78+
```bash
79+
dir1/public/index.html
80+
dir2/public/index.html
81+
```
82+
then the file from dir2 (the rightmost one) will overwrite the file from dir1 in the resulting archive entry:
83+
```bash
84+
/public/index.html
85+
```
5586
### Documentation and Example
5687

5788
This is only intended to cover basic usage. For more info about the API,
58-
see the [docs](https://maxtek6.github.io/docs/hyperpage). To see how hyperpage is used in a basic use case, the [example](https://github.com/maxtek6/hyperpage/tree/master/example) should be helpful.
89+
see the [docs](https://maxtek6.github.io/docs/hyperpage). To see how hyperpage is used in a basic use case, the [example](https://github.com/maxtek6/hyperpage/tree/master/example) should be helpful.

hyperpack.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class mapped_page : public hyperpage::page
4848
};
4949

5050
static void run(int argc, char *argv[]);
51+
static void write_directory_to_file(const std::string &directory,
52+
std::unique_ptr<hyperpage::writer> &writer);
5153

5254
int main(int argc, char *argv[])
5355
{
@@ -92,13 +94,36 @@ size_t mapped_page::get_length() const
9294
return _mmap->length();
9395
}
9496

97+
void write_directory_to_file(const std::string &directory, std::unique_ptr<hyperpage::writer> &writer)
98+
{
99+
if (!std::filesystem::exists(directory))
100+
{
101+
throw std::runtime_error("The specified directory does not exist");
102+
}
103+
104+
if(!std::filesystem::is_directory(directory))
105+
{
106+
throw std::runtime_error("The specified directory is not a directory.");
107+
}
108+
109+
for (const auto &entry : std::filesystem::recursive_directory_iterator(directory))
110+
{
111+
if (entry.is_regular_file())
112+
{
113+
mapped_page page(directory, entry.path());
114+
writer->store(page);
115+
}
116+
}
117+
}
118+
95119
void run(int argc, char *argv[])
96120
{
97121
argparse::ArgumentParser program("hyperpack");
98122
std::unique_ptr<hyperpage::writer> writer;
99123

100-
program.add_argument("directory")
101-
.help("Directory to scan for files to pack into the hyperpage database")
124+
program.add_argument("directories")
125+
.help("Directories to scan for files to pack into the hyperpage database")
126+
.nargs(argparse::nargs_pattern::at_least_one)
102127
.required();
103128
program.add_argument("-o", "--output")
104129
.help("Output file for the hyperpage database")
@@ -110,22 +135,12 @@ void run(int argc, char *argv[])
110135

111136
program.parse_args(argc, argv);
112137

113-
114-
auto directory = program.get<std::string>("directory");
115-
if (!std::filesystem::exists(directory) || !std::filesystem::is_directory(directory))
116-
{
117-
throw std::runtime_error("The specified directory does not exist or is not a directory.");
118-
}
119-
120138
auto output_file = program.get<std::string>("--output");
121-
122139
writer = std::make_unique<hyperpage::writer>(output_file);
123-
for (const auto &entry : std::filesystem::recursive_directory_iterator(directory))
124-
{
125-
if (entry.is_regular_file())
126-
{
127-
mapped_page page(directory, entry.path());
128-
writer->store(page);
129-
}
130-
}
131-
}
140+
std::vector<std::string> directories = program.get<std::vector<std::string>>("directories");
141+
std::for_each(directories.begin(), directories.end(), [&](const auto &directory)
142+
{
143+
write_directory_to_file(directory,
144+
writer);
145+
});
146+
}

0 commit comments

Comments
 (0)