~mrms/tarum

de34cffb61cfdb33e1ec59416172444b4bff76a0 — Marek Maškarinec 5 months ago f1b81ec
Improve error handling

Signed-off-by: Marek Maškarinec <marek@mrms.cz>
4 files changed, 65 insertions(+), 56 deletions(-)

M Makefile
M README.md
M box.json
M tar.um
M Makefile => Makefile +1 -1
@@ 1,7 1,7 @@

CROSS_CC=x86_64-w64-mingw32-gcc
NAME=tar
UMKA=./umbox/umka/linux/umka
UMKA=umka
CFLAGS= \
	-Wall \
	-Wno-unused-label \

M README.md => README.md +33 -38
@@ 1,58 1,53 @@
# tar.um

A tar archive library for Umka. Install using
[PAK](https://pak.tophat2d.dev/package/tar).
[UmBox](https://umbox.tophat2d.dev/package/tar).

## Example

```
import (
        "pak/tar/tar.um"
    "std.um"
    mtar = "umbox/tar/tar.um"
)

fn main() {
    var err: tar.Errno
    var t: tar.Tar
        
    t, err = tar.open("test.tar", "w")
    if err != 0 {
        printf("Error opening tar file: %s\n", tar.strerror(err))
        return
    }
fn main() {  
    tar, err := mtar.open("test.tar", "wb")
    std.exitif(err)
    
    t.addFile("pak.json")
    t.addFile("pak/")
    t.addFile("README.md")
    tar.addFile("box.json")
    tar.addFile("umbox/tar/README.md")
    tar.addFile("umbox/tar/tar_linux.umi")
    
    err = t.finalize()
    if err != 0 {
        printf("Error finalizing tar file: %s\n", tar.strerror(err))
        return
    }
    err = tar.finalize()
    std.exitif(err)
    
    err = t.close()
    if err != 0 {
        printf("Error closing tar file: %s\n", tar.strerror(err))
        return
    }
    err = tar.close()
    std.exitif(err)
    
    tar, err = mtar.open("test.tar", "rb")
    std.exitif(err)
    
    t, err = tar.open("test.tar", "r")
    if err != 0 {
        printf("Error opening tar file: %s\n", tar.strerror(err))
        return
    files, err := tar.getFiles()
    std.exitif(err)
    
    printf("Files in tar file:\n")
    for i in files {
        printf("  %s\n", files[i].name)
    }
    
    testName := "box.json"
    printf("Reading " + testName + ":\n")
    umboxJson, err := tar.read(testName)
    std.exitif(err)

    printf("%s\n", str([]char(umboxJson)))
        
    err = t.extract("extracted")
    if err != 0 {
        printf("Error extracting tar file: %s\n", tar.strerror(err))
        return
    }
    err = tar.extract("extracted")
    std.exitif(err)
    
    err = t.close()
    if err != 0 {
        printf("Error closing tar file: %s\n", tar.strerror(err))
        return
    }
    err = tar.close()
    std.exitif(err)
}
```


M box.json => box.json +1 -1
@@ 18,5 18,5 @@
    ],
    "pre_build": "make",
    "run_posix": "make run",
    "run_windows": "./pak/umka/windows/umka.exe tar.um"
    "run_windows": "./pak/umka/umka.exe tar.um"
}

M tar.um => tar.um +30 -16
@@ 5,6 5,29 @@ import (
	"std.um"
)

type Errno = enum {
	success = 0
	failure = -1
	openFail = -2
	readFail = -3
	writeFail = -4
	seekFail = -5
	badChecksum = -6
	nullRecord = -7
	notFound = -8
}

fn umc__tar_strerror(err: Errno): str
								  
fn strerror(err: Errno): str {
	if int(err) == 0 { return "" }
	return umc__tar_strerror(err)
}

fn errFromErrno(e: Errno): std.Err {
	return std.error(int(e), strerror(e), "tar.um")
}

//~~struct File
// Struct representing a file in a tar archive.
type File* = struct {


@@ 23,15 46,6 @@ type File* = struct {
type Tar* = struct{ _: ^struct{} }
//~~

type Errno* = int32

fn umc__tar_strerror(err: Errno): str
								  
fn strerror*(err: Errno): str {
	if err == 0 { return "" }
	return umc__tar_strerror(err)
}

fn umc__tar_open(path: str, mode: str, out: ^Tar): Errno
														
//~~fn open


@@ 40,7 54,7 @@ fn open*(path: str, mode: str): (Tar, std.Err) {
//~~
	var t: Tar
	err := umc__tar_open(path, mode, &t)
	return t, std.error(err, strerror(err))
	return t, errFromErrno(err)
}

fn umc__tar_open_bytes(dat: ^[]uint8, out: ^Tar): Errno


@@ 51,7 65,7 @@ fn openBytes*(dat: []uint8): (Tar, std.Err) {
//~~
	var t: Tar
	err := umc__tar_open_bytes(&dat, &t)
	return t, std.error(err, strerror(err))
	return t, errFromErrno(err)
}

fn umc__tar_close(tar: ^struct{}): Errno


@@ 61,7 75,7 @@ fn umc__tar_close(tar: ^struct{}): Errno
fn (t: ^Tar) close*(): std.Err {
//~~
	err := umc__tar_close(t._)
		return std.error(err, strerror(err))
		return errFromErrno(err)
}

fn umc__tar_get_files(tar: ^struct{}, out: ^[]File, fileArrType: ^void): Errno


@@ 72,7 86,7 @@ fn (t: ^Tar) getFiles*(): ([]File, std.Err) {
//~~
	var files: []File
	err := umc__tar_get_files(t._, &files, typeptr([]File))
	return files, std.error(err, strerror(err))
	return files, errFromErrno(err)
}

fn umc__tar_read(tar: ^struct{}, path: str, out: ^[]char, bytesArrType: ^void): Errno


@@ 83,7 97,7 @@ fn (t: ^Tar) read*(path: str): ([]char, std.Err) {
//~~
	var s: []char
	err := umc__tar_read(t._, path, &s, typeptr([]char))
	return s, std.error(err, strerror(err))
	return s, errFromErrno(err)
}

//~~fn extract


@@ 140,7 154,7 @@ fn umc__tar_finalize(t: ^struct{}): Errno
fn (t: ^Tar) finalize*(): std.Err {
//~~
	err :=  umc__tar_finalize(t._)
	return std.error(err, strerror(err))
	return errFromErrno(err)
}

fn main() {


@@ 150,7 164,7 @@ fn main() {
	tar, err = open("test.tar", "w")
	std.exitif(err)
	
	tar.addFile("umbox.json")
	tar.addFile("box.json")
	tar.addFile("microtar/README.md")
	tar.addFile("tar_linux.umi")