circache: append: resize dest to avoid recycling while appending. Clarifications

This commit is contained in:
Jean-Francois Dockes
2021-03-24 09:31:37 +01:00
parent 5e82af9798
commit 9eac638bb9
5 changed files with 144 additions and 82 deletions
+52 -39
View File
@@ -23,13 +23,15 @@ using namespace std;
static char *thisprog;
static char usage [] =
" -c [-u] <dirname> <sizekbs>: create\n"
" -c [-u] <dirname> <sizekbs>: create new store or possibly resize existing one\n"
" -u: set the 'unique' flag (else unset it)\n"
" None of this changes the existing data\n"
" -p <dirname> <apath> [apath ...] : put files\n"
" -d <dirname> : dump\n"
" -g [-i instance] [-D] <dirname> <udi>: get\n"
" -D: also dump data\n"
" -e <dirname> <udi> : erase\n"
" -a <targetdir> <dir> [<dir> ...]: append old content to target\n"
" -a <targetdir> <dir> [<dir> ...]: append content from existing cache(s) to target\n"
" The target should be first resized to hold all the data, else only\n"
" as many entries as capacity permit will be retained\n"
;
@@ -53,6 +55,8 @@ static int op_flags;
#define OPT_e 0x200
#define OPT_a 0x800
bool storeFile(CirCache& cc, const std::string fn);
int main(int argc, char **argv)
{
int instance = -1;
@@ -143,7 +147,7 @@ b1:
}
while (argc) {
string reason;
if (CirCache::append(dir, *argv++, &reason) < 0) {
if (CirCache::appendCC(dir, *argv++, &reason) < 0) {
cerr << reason << endl;
return 1;
}
@@ -160,42 +164,8 @@ b1:
while (argc) {
string fn = *argv++;
argc--;
char dic[1000];
string data, reason;
if (!file_to_string(fn, data, &reason)) {
cerr << "File_to_string: " << reason << endl;
exit(1);
}
string udi;
make_udi(fn, "", udi);
string cmd("xdg-mime query filetype ");
// Should do more quoting here...
cmd += "'" + fn + "'";
FILE *fp = popen(cmd.c_str(), "r");
char* buf=0;
size_t sz = 0;
if (::getline(&buf, &sz, fp) -1) {
cerr << "Could not read from xdg-mime output\n";
exit(1);
}
pclose(fp);
string mimetype(buf);
free(buf);
trimstring(mimetype, "\n\r");
cout << "Got [" << mimetype << "]\n";
string s;
ConfSimple conf(s);
conf.set("udi", udi);
conf.set("mimetype", mimetype);
//ostringstream str; conf.write(str); cout << str.str() << endl;
if (!cc.put(udi, &conf, data, 0)) {
cerr << "Put failed: " << cc.getReason() << endl;
cerr << "conf: [";
conf.write(cerr);
cerr << "]" << endl;
exit(1);
if (!storeFile(cc, fn)) {
return 1;
}
}
cc.open(CirCache::CC_OPREAD);
@@ -243,3 +213,46 @@ b1:
exit(0);
}
bool storeFile(CirCache& cc, const std::string fn)
{
char dic[1000];
string data, reason;
if (!file_to_string(fn, data, &reason)) {
std::cerr << "File_to_string: " << reason << endl;
return false;
}
string udi;
make_udi(fn, "", udi);
string cmd("xdg-mime query filetype ");
// Should do more quoting here...
cmd += "'" + fn + "'";
FILE *fp = popen(cmd.c_str(), "r");
char* buf=0;
size_t sz = 0;
if (::getline(&buf, &sz, fp) -1) {
std::cerr << "Could not read from xdg-mime output\n";
return false;
}
pclose(fp);
string mimetype(buf);
free(buf);
trimstring(mimetype, "\n\r");
//std::cerr << "Got [" << mimetype << "]\n";
string s;
ConfSimple conf(s);
conf.set("udi", udi);
conf.set("mimetype", mimetype);
//ostringstream str; conf.write(str); cout << str.str() << endl;
if (!cc.put(udi, &conf, data, 0)) {
std::cerr << "Put failed: " << cc.getReason() << endl;
std::cerr << "conf: [";
conf.write(std::cerr);
std::cerr << "]" << endl;
return false;
}
return true;
}