Posted 02 August 2019 - 06:52 AM
I'm working on a project involving the use of TAR files (hint: it's a port of a popular Linux utility) and I figured I'd share the library I wrote to interact with TAR archives. It can read & write Unix Standard TAR (UStar) and read original TAR. I've also added permission modification if running under CCKernel2. You can either load the file as an API, or you can run the file directly, in which case it will work similarly to the GNU tar command. Running it under the shell will also provide the ability to use gzipped archives with the -z flag; this is provided by a ComputerCraft fork of the LibDeflate library by SafeteeWow. Here's the API reference:
A tar object/tar entry/file entry/file object has these properties:You can use many of the switches from *nix tar on the command line, including `tar -xzf file.tar.gz` and `tar -cvf file.tar file1 file2 file3` just like on real machines. I've gotten rid of the switches that either a ) don't make sense on ComputerCraft, or b ) don't work at all on CraftOS, but much of the core functionality still works.
You can download it from the GitHub gist, which includes tar.lua + LibDeflate.lua. Note that this is BETA SOFTWARE and some functionality may be missing or buggy. I did not put it on pastebin yet since it doesn't support multiple files or keep revision history, though I might later without LibDeflate if I ever complete the CLI interface and add error checking.
API reference
tar.load(path, noser, rawdata)
Returns a table with the data in the archive file.- path: The path to the file to read, or the file data if rawdata is set
- noser: Set to true to return a list of tar entries (skip unserialization)
- rawdata: Set to true to read raw file data in path instead of from a file
- Returns: A hierarchical table with the contents of the file tree, or a list of file entries if noser is set
tar.unserialize(data)
Unserializes a list of tar objects into a file tree.- data: The list of file entries to unserialize
- Returns: A hierarchical table of files
tar.extract(data, path)
Writes a hierarchy to a directory.- data: The data to write (from load())
- path: The directory to write files inside
tar.read(base, p)
Reads a single file into a tar entry.- base: The base directory to read from
- p: The path to the file in the tarball
- Returns: A tar entry (see below)
tar.pack(base, path)
Reads a directory tree into a hierarchical table.- base: The base path to start reading from
- path: (optional) The name of the directory as it will be in the tarball
- Returns: A table with the contents of the directory
tar.serialize(data)
Serializes a hierarchy into a list of tar objects.- data: The file hierarchy to serialize
- Returns: A list of tar objects
tar.save(data, path, noser)
Saves a hierarchy into a TAR file.- data: The file tree to save
- path: The TAR file to save to, or nil to return the raw data
- noser: Set this to save a non-hierarchical table (a list of tar objects) instead of hierarchy
{
usr = {
bin = {
hello = <tar object>,
},
share = {
hello = {
["hello.txt"] = <tar object>,
},
},
},
etc = {
["hello.conf"] = <tar object>,
},
}
A tar object/tar entry/file entry/file object has these properties:
- name: The full path of the file in the TAR file
- mode: The UNIX file permissions of the file (only useful with CCKernel2)
- owner: The UID of the owner of the file (only useful with CCKernel2)
- group: The group of the file (not useful)
- timestamp: The last modified date of the file (set to time of TAR creation)
- type: The file type (0 = file, 5 = directory, others not useful)
- link: The path the file links to if it's a link (not useful)
- ownerName: The short name of the owner of the file (only useful with CCKernel2)
- groupName: The name of the group of the file (not useful)
- deviceNumber: Two-entry table with the device major and minor number (unknown)
- data: The contents of the file
You can download it from the GitHub gist, which includes tar.lua + LibDeflate.lua. Note that this is BETA SOFTWARE and some functionality may be missing or buggy. I did not put it on pastebin yet since it doesn't support multiple files or keep revision history, though I might later without LibDeflate if I ever complete the CLI interface and add error checking.
Edited on 02 August 2019 - 08:18 AM