refactor: Start implementation of public API

This commit is contained in:
Felicitas Pojtinger
2021-12-06 21:01:41 +01:00
parent bbfde631a9
commit 4f5d298c8e
45 changed files with 211 additions and 54 deletions

View File

@@ -0,0 +1,66 @@
package converters
import (
"archive/tar"
"encoding/json"
models "github.com/pojntfx/stfs/internal/db/sqlite/models/metadata"
)
func DBHeaderToTarHeader(dbhdr *models.Header) (*tar.Header, error) {
paxRecords := map[string]string{}
if err := json.Unmarshal([]byte(dbhdr.Paxrecords), &paxRecords); err != nil {
return nil, err
}
hdr := &tar.Header{
Typeflag: byte(dbhdr.Typeflag),
Name: dbhdr.Name,
Linkname: dbhdr.Linkname,
Size: dbhdr.Size,
Mode: dbhdr.Mode,
Uid: int(dbhdr.UID),
Gid: int(dbhdr.Gid),
Uname: dbhdr.Uname,
Gname: dbhdr.Gname,
ModTime: dbhdr.Modtime,
AccessTime: dbhdr.Accesstime,
ChangeTime: dbhdr.Changetime,
Devmajor: dbhdr.Devmajor,
Devminor: dbhdr.Devminor,
PAXRecords: paxRecords,
Format: tar.Format(dbhdr.Format),
}
return hdr, nil
}
func TarHeaderToDBHeader(record, block int64, tarhdr *tar.Header) (*models.Header, error) {
paxRecords, err := json.Marshal(tarhdr.PAXRecords)
if err != nil {
return nil, err
}
hdr := models.Header{
Record: record,
Block: block,
Typeflag: int64(tarhdr.Typeflag),
Name: tarhdr.Name,
Linkname: tarhdr.Linkname,
Size: tarhdr.Size,
Mode: tarhdr.Mode,
UID: int64(tarhdr.Uid),
Gid: int64(tarhdr.Gid),
Uname: tarhdr.Uname,
Gname: tarhdr.Gname,
Modtime: tarhdr.ModTime,
Accesstime: tarhdr.AccessTime,
Changetime: tarhdr.ChangeTime,
Devmajor: tarhdr.Devmajor,
Devminor: tarhdr.Devminor,
Paxrecords: string(paxRecords),
Format: int64(tarhdr.Format),
}
return &hdr, nil
}