Skip to content

Commit a32b03d

Browse files
committed
Improving handling of empty files (uncompressed_size == 0) inside of 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.
1 parent 7535f5f commit a32b03d

File tree

12 files changed

+1109
-2051
lines changed

12 files changed

+1109
-2051
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
1313

1414
# Project version number
1515
set(PRJ_VERSION_MAJOR 9)
16-
set(PRJ_VERSION_MINOR 46)
16+
set(PRJ_VERSION_MINOR 47)
1717

1818
if(WIN32 AND NOT DEFINED ENV{MSYSTEM})
1919
message(FATAL_ERROR "Currently unsuppored environment. Use MINGW for Windows builds. CMake willl exit now.")

docker/Dockerfile.build

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ COPY get-maria.sh .
6666
RUN ./get-maria.sh
6767

6868
# ---------- download golang
69-
70-
RUN curl -LO https://go.dev/dl/go1.20.2.linux-amd64.tar.gz \
71-
&& tar -xvf go1.20.2.linux-amd64.tar.gz \
72-
&& rm go1.20.2.linux-amd64.tar.gz \
69+
RUN curl -LO https://go.dev/dl/go1.20.4.linux-amd64.tar.gz \
70+
&& tar -xvf go1.20.4.linux-amd64.tar.gz \
71+
&& rm go1.20.4.linux-amd64.tar.gz \
7372
&& sed -i -e '$a export PATH=$PATH:/root/go/bin' .bashrc \
7473
&& sed -i -e '$a export GOROOT=/root/go' .bashrc
7574

lib2inpx/lib2inpx.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ bool read_fb2(const unzip& uz, const string& book_id, fb2_parser& fb, unz_file_i
627627

628628
uz.current(fi);
629629

630+
if (fi.uncompressed_size == 0) {
631+
throw runtime_error("Empty fb2 file");
632+
}
633+
630634
int len = 0;
631635
bool continue_processing = true;
632636

src/inpxcreator/cmd/libmerge/main.go

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func main() {
259259
tmpOut = f.Name()
260260
w = zip.NewWriter(f)
261261

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

306-
if firstBook == 0 {
307-
firstBook = id
308-
}
309-
lastBook = id
310-
311-
// I know this is wrong, leftBytes could already be negative, but to repeat what libsplit did
312-
// always copy first file...
313-
314-
if err := w.Copy(file); err != nil {
315-
log.Printf("Error copying from %s (%s): %v", name, file.Name, err)
316-
} else {
317-
318-
leftBytes -= int64(file.CompressedSize64)
319-
320-
if leftBytes <= 0 {
321-
if err := w.Close(); err != nil {
322-
log.Fatalf("Finishing zip file: %v", err)
323-
}
324-
if err := f.Close(); err != nil {
325-
log.Fatalf("Finishing zip file: %v", err)
326-
}
327-
newName := fmt.Sprintf("fb2-%06d-%06d.zip", firstBook, lastBook)
328-
fmt.Printf("\t--> Finalizing archive: %s\n", newName)
329-
330-
newName = filepath.Join(last.dir, newName)
331-
if err := os.Rename(tmpOut, newName); err != nil {
332-
log.Fatalf("Renaming archive: %v", err)
333-
}
334-
335-
last.info, err = os.Stat(newName)
336-
if err != nil {
337-
log.Fatalf("Stat failed: %v", err)
338-
}
339-
last.begin = firstBook
340-
last.end = lastBook
341-
fmt.Printf("\t--> New last archive: %s\n", newName)
342-
343-
// We may want to rebuild inpx - have new "last" archive ready
344-
code = 2
345-
346-
f, err = ioutil.TempFile(last.dir, "merge-")
347-
if err != nil {
348-
log.Fatalf("Unable to create temp file: %v", err)
349-
}
350-
tmpOut = f.Name()
351-
w = zip.NewWriter(f)
352-
leftBytes = sizeBytes
353-
firstBook = 0
305+
if file.FileInfo().Size() == 0 {
306+
log.Printf("\t\tWrong book size - %d, skipping: \"%s\"\n", file.FileInfo().Size(), file.FileInfo().Name())
307+
continue
308+
}
309+
id := int(0)
310+
if id = name2id(file.FileInfo().Name()); id <= 0 {
311+
log.Printf("\t\tWrong book name, skipping: \"%s\"\n", file.FileInfo().Name())
312+
continue
313+
}
314+
315+
if firstBook == 0 {
316+
firstBook = id
317+
}
318+
lastBook = id
319+
320+
// I know this is wrong, leftBytes could already be negative, but to repeat what libsplit did
321+
// always copy first file...
322+
323+
if err := w.Copy(file); err != nil {
324+
log.Printf("Error copying from %s (%s): %v", name, file.FileInfo().Name(), err)
325+
} else {
326+
327+
leftBytes -= int64(file.CompressedSize64)
328+
329+
if leftBytes <= 0 {
330+
if err := w.Close(); err != nil {
331+
log.Fatalf("Finishing zip file: %v", err)
332+
}
333+
if err := f.Close(); err != nil {
334+
log.Fatalf("Finishing zip file: %v", err)
335+
}
336+
newName := fmt.Sprintf("fb2-%06d-%06d.zip", firstBook, lastBook)
337+
fmt.Printf("\t--> Finalizing archive: %s\n", newName)
338+
339+
newName = filepath.Join(last.dir, newName)
340+
if err := os.Rename(tmpOut, newName); err != nil {
341+
log.Fatalf("Renaming archive: %v", err)
342+
}
343+
344+
last.info, err = os.Stat(newName)
345+
if err != nil {
346+
log.Fatalf("Stat failed: %v", err)
354347
}
348+
last.begin = firstBook
349+
last.end = lastBook
350+
fmt.Printf("\t--> New last archive: %s\n", newName)
351+
352+
// We may want to rebuild inpx - have new "last" archive ready
353+
code = 2
354+
355+
f, err = ioutil.TempFile(last.dir, "merge-")
356+
if err != nil {
357+
log.Fatalf("Unable to create temp file: %v", err)
358+
}
359+
tmpOut = f.Name()
360+
w = zip.NewWriter(f)
361+
leftBytes = sizeBytes
362+
firstBook = 0
355363
}
356-
} else {
357-
log.Printf("\t\tWrong book name, skipping: \"%s\"\n", file.Name)
358364
}
359365
}
360366
if err := rc.Close(); err != nil {

src/inpxcreator/internal/zip/example_test.go

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)