1616#define round_down_blk (addr ) ((addr) & (~(SECTOR_SIZE - 1 )))
1717#define round_up_blk (addr ) (round_down_blk((addr) + SECTOR_SIZE - 1 ))
1818#define min (a, b ) (a) < (b) ? (a) : (b)
19- #define MAP_FILE_NAME " upper.map"
2019
2120#define EROFS_UNIMPLEMENTED 1
2221
@@ -257,7 +256,7 @@ static ssize_t erofs_read_photon_file(void *buf, u64 offset, size_t len,
257256 LOG_ERROR (" Fail to read sector %lld." , end - SECTOR_SIZE );
258257 return -1 ;
259258 }
260- memcpy (extra_buf, buf + end - SECTOR_SIZE - offset,
259+ memcpy (extra_buf, ( char *) buf + end - SECTOR_SIZE - offset,
261260 offset + len + SECTOR_SIZE - end);
262261 if (cache->write_sector (end - SECTOR_SIZE , extra_buf)
263262 != SECTOR_SIZE )
@@ -377,28 +376,13 @@ static int erofs_source_fallocate(struct erofs_vfile *vf,
377376
378377static int erofs_source_ftruncate (struct erofs_vfile *vf, u64 length)
379378{
380- return EROFS_UNIMPLEMENTED ;
379+ return - EROFS_UNIMPLEMENTED ;
381380}
382381
383382static ssize_t erofs_source_read (struct erofs_vfile *vf, void *buf,
384383 size_t bytes)
385384{
386- u64 i = 0 ;
387- while (bytes) {
388- u64 len = bytes > INT_MAX ? INT_MAX : bytes;
389- u64 ret;
390-
391- ret = _source->read (buf + i, len);
392- if (ret < 1 ) {
393- if (ret == 0 )
394- break ;
395- else
396- return -1 ;
397- }
398- bytes -= ret;
399- i += ret;
400- }
401- return i;
385+ return _source->read (buf, bytes);
402386}
403387
404388static off_t erofs_source_lseek (struct erofs_vfile *vf, u64 offset, int whence)
@@ -411,6 +395,7 @@ struct erofs_mkfs_cfg {
411395 struct erofs_tarfile *erofstar;
412396 bool incremental;
413397 bool ovlfs_strip;
398+ FILE *mp_fp;
414399};
415400
416401static int rebuild_src_count;
@@ -421,18 +406,18 @@ int erofs_mkfs(struct erofs_mkfs_cfg *cfg)
421406 struct erofs_tarfile *erofstar;
422407 struct erofs_sb_info *sbi;
423408 struct erofs_buffer_head *sb_bh;
424- struct erofs_inode *root;
409+ struct erofs_inode *root = NULL ;
425410 erofs_blk_t nblocks;
426411
427412 erofstar = cfg->erofstar ;
428413 sbi = cfg->sbi ;
429414 if (!erofstar || !sbi)
430415 return -EINVAL ;
431416
432- if (!erofstar-> mapfile )
417+ if (!cfg-> mp_fp )
433418 return -EINVAL ;
434419
435- err = erofs_blocklist_open (erofstar-> mapfile , true );
420+ err = erofs_blocklist_open (cfg-> mp_fp , true );
436421 if (err) {
437422 LOG_ERROR (" [erofs] Fail to open erofs blocklist." );
438423 return -EINVAL ;
@@ -444,7 +429,12 @@ int erofs_mkfs(struct erofs_mkfs_cfg *cfg)
444429 }
445430
446431 if (!cfg->incremental ) {
447- sb_bh = erofs_reserve_sb (sbi);
432+ sbi->bmgr = erofs_buffer_init (sbi, 0 );
433+ if (!sbi->bmgr ) {
434+ err = -ENOMEM ;
435+ goto exit;
436+ }
437+ sb_bh = erofs_reserve_sb (sbi->bmgr );
448438 if (IS_ERR (sb_bh)) {
449439 LOG_ERROR (" [erofs] Fail to reseve space for superblock." );
450440 err = PTR_ERR (sb_bh);
@@ -456,7 +446,11 @@ int erofs_mkfs(struct erofs_mkfs_cfg *cfg)
456446 LOG_ERROR (" [erofs] Fail to read superblock." );
457447 goto exit;
458448 }
459- erofs_buffer_init (sbi, sbi->primarydevice_blocks );
449+ sbi->bmgr = erofs_buffer_init (sbi, sbi->primarydevice_blocks );
450+ if (!sbi->bmgr ) {
451+ err = -ENOMEM ;
452+ goto exit;
453+ }
460454 sb_bh = NULL ;
461455 }
462456
@@ -471,17 +465,17 @@ int erofs_mkfs(struct erofs_mkfs_cfg *cfg)
471465
472466 while (!(err = tarerofs_parse_tar (root, erofstar)));
473467 if (err < 0 ) {
474- LOG_ERROR (" [erofs] Fail to parse tar file." );
468+ LOG_ERROR (" [erofs] Fail to parse tar file." , err );
475469 goto exit;
476470 }
477471
478472 err = erofs_rebuild_dump_tree (root, cfg->incremental );
479473 if (err < 0 ) {
480- LOG_ERROR (" [erofs] Fail to dump tree." );
474+ LOG_ERROR (" [erofs] Fail to dump tree." , err );
481475 goto exit;
482476 }
483477
484- err = erofs_bflush (NULL );
478+ err = erofs_bflush (sbi-> bmgr , NULL );
485479 if (err) {
486480 LOG_ERROR (" [erofs] Bflush failed." );
487481 goto exit;
@@ -498,23 +492,22 @@ int erofs_mkfs(struct erofs_mkfs_cfg *cfg)
498492 }
499493
500494 /* flush all remaining buffers */
501- err = erofs_bflush (NULL );
495+ err = erofs_bflush (sbi-> bmgr , NULL );
502496 if (err)
503497 goto exit;
504498
505499 err = erofs_dev_resize (sbi, nblocks);
506500exit:
507501 if (root)
508502 erofs_iput (root);
503+ erofs_buffer_exit (sbi->bmgr );
509504 erofs_blocklist_close ();
510505 return err;
511506}
512507
513508static int erofs_init_sbi (struct erofs_sb_info *sbi, photon::fs::IFile *fout,
514509 struct erofs_vfops *ops, int blkbits)
515510{
516- int err;
517-
518511 sbi->blkszbits = (char )blkbits;
519512 sbi->bdev .ops = ops;
520513 fout->lseek (0 , 0 );
@@ -524,26 +517,17 @@ static int erofs_init_sbi(struct erofs_sb_info *sbi, photon::fs::IFile *fout,
524517}
525518
526519static int erofs_init_tar (struct erofs_tarfile *erofstar,
527- photon::fs::IFile *tar_file, struct erofs_vfops *ops)
520+ struct erofs_vfops *ops)
528521{
529- int err;
530- struct stat st;
531-
532522 erofstar->global .xattrs = LIST_HEAD_INIT (erofstar->global .xattrs );
533- erofstar->mapfile = MAP_FILE_NAME ;
534523 erofstar->aufs = true ;
535524 erofstar->rvsp_mode = true ;
536525 erofstar->dev = rebuild_src_count + 1 ;
537526
538527 erofstar->ios .feof = false ;
539528 erofstar->ios .tail = erofstar->ios .head = 0 ;
540529 erofstar->ios .dumpfd = -1 ;
541- err = tar_file->fstat (&st);
542- if (err) {
543- LOG_ERROR (" Fail to fstat tar file." );
544- return err;
545- }
546- erofstar->ios .sz = st.st_size ;
530+ erofstar->ios .sz = 0 ;
547531 erofstar->ios .bufsize = 16384 ;
548532 do {
549533 erofstar->ios .buffer = (char *)malloc (erofstar->ios .bufsize );
@@ -560,18 +544,16 @@ static int erofs_init_tar(struct erofs_tarfile *erofstar,
560544 return 0 ;
561545}
562546
563- static int erofs_write_map_file (photon::fs::IFile *fout, uint64_t blksz)
547+ static int erofs_write_map_file (photon::fs::IFile *fout, uint64_t blksz, FILE *fp )
564548{
565- FILE *fp;
566549 uint64_t blkaddr, toff;
567550 uint32_t nblocks;
568551
569- fp = fopen (MAP_FILE_NAME , " r" );
570552 if (fp == NULL ) {
571553 LOG_ERROR (" unable to get upper.map, ignored" );
572554 return -1 ;
573555 }
574-
556+ rewind (fp);
575557 while (fscanf (fp, " %" PRIx64" %x %" PRIx64 " \n " , &blkaddr, &nblocks, &toff)
576558 >= 3 )
577559 {
@@ -584,9 +566,8 @@ static int erofs_write_map_file(photon::fs::IFile *fout, uint64_t blksz)
584566 LOG_ERRNO_RETURN (0 , -1 , " failed to write lba" );
585567 }
586568 }
587- fclose (fp);
588569
589- return unlink ( MAP_FILE_NAME ) ;
570+ return 0 ;
590571}
591572
592573static int erofs_close_sbi (struct erofs_sb_info *sbi, ErofsCache *cache)
@@ -643,7 +624,7 @@ int LibErofs::extract_tar(photon::fs::IFile *source, bool meta_only, bool first_
643624 return err;
644625 }
645626 /* initialization of erofstar */
646- err = erofs_init_tar (&erofstar, _source, &source_vfops);
627+ err = erofs_init_tar (&erofstar, &source_vfops);
647628 if (err) {
648629 LOG_ERROR (" Failed to init tarerofs" );
649630 goto exit;
@@ -654,6 +635,7 @@ int LibErofs::extract_tar(photon::fs::IFile *source, bool meta_only, bool first_
654635 cfg.incremental = !first_layer;
655636 erofs_cfg = erofs_get_configure ();
656637 erofs_cfg->c_ovlfs_strip = true ;
638+ cfg.mp_fp = std::tmpfile ();
657639
658640 err = erofs_mkfs (&cfg);
659641 if (err) {
@@ -662,14 +644,15 @@ int LibErofs::extract_tar(photon::fs::IFile *source, bool meta_only, bool first_
662644 }
663645
664646 /* write mapfile */
665- err = erofs_write_map_file (_target, blksize);
647+ err = erofs_write_map_file (_target, blksize, cfg. mp_fp );
666648 if (err) {
667649 LOG_ERROR (" Failed to write mapfile." );
668650 goto exit;
669651 }
670652exit:
671653 err = erofs_close_sbi (&sbi, &erofs_cache);
672654 erofs_close_tar (&erofstar);
655+ std::fclose (cfg.mp_fp );
673656 return err;
674657}
675658
0 commit comments