@@ -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 ("\t Processing 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 \t Wrong 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 \t Wrong 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 \t Wrong book name, skipping: \" %s\" \n " , file .Name )
358364 }
359365 }
360366 if err := rc .Close (); err != nil {
0 commit comments