146 lines
3.8 KiB
Plaintext
146 lines
3.8 KiB
Plaintext
var fd = use('fd')
|
|
var qop = use('qop')
|
|
|
|
function print_usage() {
|
|
log.console("Usage: qopconv [OPTION...] FILE...")
|
|
log.console(" -u <archive> ... unpack archive")
|
|
log.console(" -l <archive> ... list contents of archive")
|
|
log.console(" -d <dir> ....... change read dir when creating archives")
|
|
log.console(" <sources...> <archive> ... create archive from sources")
|
|
}
|
|
|
|
function list(archive_path) {
|
|
var blob = fd.slurp(archive_path)
|
|
if (!blob) {
|
|
log.console("Could not open archive " + archive_path)
|
|
return
|
|
}
|
|
var archive = null
|
|
try {
|
|
archive = qop.open(blob)
|
|
} catch(e) {
|
|
log.console("Could not open archive " + archive_path + ": " + e.message)
|
|
return
|
|
}
|
|
|
|
var files = archive.list()
|
|
for (var f of files) {
|
|
var s = archive.stat(f)
|
|
// Format: index hash size path
|
|
// We don't have index/hash easily available in JS binding yet, just size/path
|
|
log.console(`${f} (${s.size} bytes)`)
|
|
}
|
|
archive.close()
|
|
}
|
|
|
|
function unpack(archive_path) {
|
|
var blob = fd.slurp(archive_path)
|
|
if (!blob) {
|
|
log.console("Could not open archive " + archive_path)
|
|
return
|
|
}
|
|
var archive = null
|
|
try {
|
|
archive = qop.open(blob)
|
|
} catch(e) {
|
|
log.console("Could not open archive " + archive_path + ": " + e.message)
|
|
return
|
|
}
|
|
|
|
var files = archive.list()
|
|
for (var f of files) {
|
|
var data = archive.read(f)
|
|
if (data) {
|
|
// Ensure directory exists
|
|
var dir = f.substring(0, f.lastIndexOf('/'))
|
|
if (dir) {
|
|
// recursive mkdir
|
|
var parts = dir.split('/')
|
|
var curr = "."
|
|
for (var p of parts) {
|
|
curr += "/" + p
|
|
try { fd.mkdir(curr) } catch(e) {}
|
|
}
|
|
}
|
|
var fh = fd.open(f, "w")
|
|
fd.write(fh, data)
|
|
fd.close(fh)
|
|
log.console("Extracted " + f)
|
|
}
|
|
}
|
|
archive.close()
|
|
}
|
|
|
|
function pack(sources, archive_path, read_dir) {
|
|
var writer = qop.write(archive_path)
|
|
|
|
var base_dir = read_dir || "."
|
|
|
|
function add_recursive(path) {
|
|
var full_path = base_dir + "/" + path
|
|
if (path == ".") full_path = base_dir
|
|
if (read_dir == null && path != ".") full_path = path
|
|
|
|
var st = fd.stat(full_path)
|
|
if (!st) {
|
|
log.console("Could not stat " + full_path)
|
|
return
|
|
}
|
|
|
|
if (st.isDirectory) {
|
|
var list = fd.readdir(full_path)
|
|
for (var item of list) {
|
|
if (item == "." || item == "..") continue
|
|
var sub = path == "." ? item : path + "/" + item
|
|
add_recursive(sub)
|
|
}
|
|
} else {
|
|
var data = fd.slurp(full_path)
|
|
if (data) {
|
|
writer.add_file(path, data)
|
|
log.console("Added " + path)
|
|
}
|
|
}
|
|
}
|
|
|
|
for (var s of sources) {
|
|
add_recursive(s)
|
|
}
|
|
|
|
writer.finalize()
|
|
log.console("Created " + archive_path)
|
|
}
|
|
|
|
if (!is_array(arg) || arg.length < 1) {
|
|
print_usage()
|
|
} else {
|
|
if (arg[0] == "-l") {
|
|
if (arg.length < 2) print_usage()
|
|
else list(arg[1])
|
|
} else if (arg[0] == "-u") {
|
|
if (arg.length < 2) print_usage()
|
|
else unpack(arg[1])
|
|
} else {
|
|
var sources = []
|
|
var archive = null
|
|
var read_dir = null
|
|
var i = 0
|
|
if (arg[0] == "-d") {
|
|
read_dir = arg[1]
|
|
i = 2
|
|
}
|
|
|
|
for (; i < arg.length - 1; i++) {
|
|
sources.push(arg[i])
|
|
}
|
|
archive = arg[arg.length - 1]
|
|
|
|
if (sources.length == 0) {
|
|
print_usage()
|
|
} else {
|
|
pack(sources, archive, read_dir)
|
|
}
|
|
}
|
|
}
|
|
|
|
$stop() |