Skip to content

Commit

Permalink
Improving handling of empty files (uncompressed_size == 0) inside of …
Browse files Browse the repository at this point in the history
…various archives:

    libmerge will not copy such entries from update archives to library ones.
    lib2inpx will give a proper warning about empty file rather then attempt to parse FB2 and fail.
Using "internal/zip" from up to date go distro "archive/zip" rather then old files from "https://github.com/rsc/zipmerge".
Using go 1.20.4 tool chain.
  • Loading branch information
rupor-github committed May 28, 2023
1 parent 7535f5f commit a32b03d
Show file tree
Hide file tree
Showing 12 changed files with 1,109 additions and 2,051 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)

# Project version number
set(PRJ_VERSION_MAJOR 9)
set(PRJ_VERSION_MINOR 46)
set(PRJ_VERSION_MINOR 47)

if(WIN32 AND NOT DEFINED ENV{MSYSTEM})
message(FATAL_ERROR "Currently unsuppored environment. Use MINGW for Windows builds. CMake willl exit now.")
Expand Down
7 changes: 3 additions & 4 deletions docker/Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ COPY get-maria.sh .
RUN ./get-maria.sh

# ---------- download golang

RUN curl -LO https://go.dev/dl/go1.20.2.linux-amd64.tar.gz \
&& tar -xvf go1.20.2.linux-amd64.tar.gz \
&& rm go1.20.2.linux-amd64.tar.gz \
RUN curl -LO https://go.dev/dl/go1.20.4.linux-amd64.tar.gz \
&& tar -xvf go1.20.4.linux-amd64.tar.gz \
&& rm go1.20.4.linux-amd64.tar.gz \
&& sed -i -e '$a export PATH=$PATH:/root/go/bin' .bashrc \
&& sed -i -e '$a export GOROOT=/root/go' .bashrc

Expand Down
4 changes: 4 additions & 0 deletions lib2inpx/lib2inpx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ bool read_fb2(const unzip& uz, const string& book_id, fb2_parser& fb, unz_file_i

uz.current(fi);

if (fi.uncompressed_size == 0) {
throw runtime_error("Empty fb2 file");
}

int len = 0;
bool continue_processing = true;

Expand Down
110 changes: 58 additions & 52 deletions src/inpxcreator/cmd/libmerge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func main() {
tmpOut = f.Name()
w = zip.NewWriter(f)

if (sizeBytes - last.info.Size()) > 0 {
if last.info != nil && (sizeBytes-last.info.Size()) > 0 {
fmt.Printf("Merging last archive, possibly fist time processing: %s\n", filepath.Join(last.dir, last.info.Name()))
skipFirst = true
tmp := make([]archive, len(updates)+1, len(updates)+1)
Expand Down Expand Up @@ -301,60 +301,66 @@ func main() {
}
fmt.Printf("\tProcessing update: %s\n", filepath.Join(u.dir, u.info.Name()))
for _, file := range rc.File {
if id := name2id(file.Name); id > 0 {

if firstBook == 0 {
firstBook = id
}
lastBook = id

// I know this is wrong, leftBytes could already be negative, but to repeat what libsplit did
// always copy first file...

if err := w.Copy(file); err != nil {
log.Printf("Error copying from %s (%s): %v", name, file.Name, err)
} else {

leftBytes -= int64(file.CompressedSize64)

if leftBytes <= 0 {
if err := w.Close(); err != nil {
log.Fatalf("Finishing zip file: %v", err)
}
if err := f.Close(); err != nil {
log.Fatalf("Finishing zip file: %v", err)
}
newName := fmt.Sprintf("fb2-%06d-%06d.zip", firstBook, lastBook)
fmt.Printf("\t--> Finalizing archive: %s\n", newName)

newName = filepath.Join(last.dir, newName)
if err := os.Rename(tmpOut, newName); err != nil {
log.Fatalf("Renaming archive: %v", err)
}

last.info, err = os.Stat(newName)
if err != nil {
log.Fatalf("Stat failed: %v", err)
}
last.begin = firstBook
last.end = lastBook
fmt.Printf("\t--> New last archive: %s\n", newName)

// We may want to rebuild inpx - have new "last" archive ready
code = 2

f, err = ioutil.TempFile(last.dir, "merge-")
if err != nil {
log.Fatalf("Unable to create temp file: %v", err)
}
tmpOut = f.Name()
w = zip.NewWriter(f)
leftBytes = sizeBytes
firstBook = 0
if file.FileInfo().Size() == 0 {
log.Printf("\t\tWrong book size - %d, skipping: \"%s\"\n", file.FileInfo().Size(), file.FileInfo().Name())
continue
}
id := int(0)
if id = name2id(file.FileInfo().Name()); id <= 0 {
log.Printf("\t\tWrong book name, skipping: \"%s\"\n", file.FileInfo().Name())
continue
}

if firstBook == 0 {
firstBook = id
}
lastBook = id

// I know this is wrong, leftBytes could already be negative, but to repeat what libsplit did
// always copy first file...

if err := w.Copy(file); err != nil {
log.Printf("Error copying from %s (%s): %v", name, file.FileInfo().Name(), err)
} else {

leftBytes -= int64(file.CompressedSize64)

if leftBytes <= 0 {
if err := w.Close(); err != nil {
log.Fatalf("Finishing zip file: %v", err)
}
if err := f.Close(); err != nil {
log.Fatalf("Finishing zip file: %v", err)
}
newName := fmt.Sprintf("fb2-%06d-%06d.zip", firstBook, lastBook)
fmt.Printf("\t--> Finalizing archive: %s\n", newName)

newName = filepath.Join(last.dir, newName)
if err := os.Rename(tmpOut, newName); err != nil {
log.Fatalf("Renaming archive: %v", err)
}

last.info, err = os.Stat(newName)
if err != nil {
log.Fatalf("Stat failed: %v", err)
}
last.begin = firstBook
last.end = lastBook
fmt.Printf("\t--> New last archive: %s\n", newName)

// We may want to rebuild inpx - have new "last" archive ready
code = 2

f, err = ioutil.TempFile(last.dir, "merge-")
if err != nil {
log.Fatalf("Unable to create temp file: %v", err)
}
tmpOut = f.Name()
w = zip.NewWriter(f)
leftBytes = sizeBytes
firstBook = 0
}
} else {
log.Printf("\t\tWrong book name, skipping: \"%s\"\n", file.Name)
}
}
if err := rc.Close(); err != nil {
Expand Down
94 changes: 0 additions & 94 deletions src/inpxcreator/internal/zip/example_test.go

This file was deleted.

Loading

0 comments on commit a32b03d

Please sign in to comment.