Skip to content

Commit e57dec9

Browse files
author
Hongzhen Luo
committed
[EROFS]: cleanup: get rid of the global static variables in liberofs.cpp
Remove the global variables `_target` and `_source`, and introduce `struct liberofs_file` to wrap the source and target files. Signed-off-by: Hongzhen Luo <hongzhen@linux.alibaba.com>
1 parent 1059cf4 commit e57dec9

1 file changed

Lines changed: 67 additions & 39 deletions

File tree

src/overlaybd/tar/erofs/liberofs.cpp

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ struct liberofs_inmem_sector {
2727

2828
class ErofsCache {
2929
public:
30-
ErofsCache() {}
30+
ErofsCache(photon::fs::IFile *file, unsigned long int capacity):
31+
file(file), capacity(capacity)
32+
{}
3133
~ErofsCache() {}
3234
ssize_t write_sector(u64 addr, char *buf);
3335
ssize_t read_sector(u64 addr, char *buf);
@@ -39,11 +41,11 @@ class ErofsCache {
3941
std::set<u64> dirty;
4042
};
4143

42-
static struct erofs_vfops target_vfops;
43-
static struct erofs_vfops source_vfops;
44-
static ErofsCache erofs_cache;
45-
static photon::fs::IFile *_target;
46-
static photon::fs::IFile *_source;
44+
struct liberofs_file {
45+
struct erofs_vfops ops;
46+
photon::fs::IFile *file;
47+
ErofsCache *cache;
48+
};
4749

4850
ssize_t ErofsCache::write_sector(u64 addr, char *buf)
4951
{
@@ -297,7 +299,13 @@ static ssize_t erofs_read_photon_file(void *buf, u64 offset, size_t len,
297299
static ssize_t erofs_target_pread(struct erofs_vfile *vf, void *buf, u64 offset,
298300
size_t len)
299301
{
300-
if (erofs_read_photon_file(buf, offset, len, &erofs_cache) != (ssize_t)len)
302+
struct liberofs_file *target_file =
303+
reinterpret_cast<struct liberofs_file *>(vf->ops);
304+
305+
if (!target_file)
306+
return -EINVAL;
307+
if (erofs_read_photon_file(buf, offset, len, target_file->cache)
308+
!= (ssize_t)len)
301309
return -1;
302310

303311
return len;
@@ -306,15 +314,25 @@ static ssize_t erofs_target_pread(struct erofs_vfile *vf, void *buf, u64 offset,
306314
static ssize_t erofs_target_pwrite(struct erofs_vfile *vf, const void *buf,
307315
u64 offset, size_t len)
308316
{
317+
struct liberofs_file *target_file =
318+
reinterpret_cast<struct liberofs_file *>(vf->ops);
319+
320+
if (!target_file)
321+
return -EINVAL;
309322
if (!buf)
310323
return -EINVAL;
311324

312-
return erofs_write_photon_file(buf, offset, len, &erofs_cache);
325+
return erofs_write_photon_file(buf, offset, len, target_file->cache);
313326
}
314327

315328
static int erofs_target_fsync(struct erofs_vfile *vf)
316329
{
317-
return erofs_cache.flush();
330+
struct liberofs_file *target_file =
331+
reinterpret_cast<struct liberofs_file *>(vf->ops);
332+
333+
if (!target_file)
334+
return -EINVAL;
335+
return target_file->cache->flush();
318336
}
319337

320338
static int erofs_target_fallocate(struct erofs_vfile *vf, u64 offset,
@@ -384,12 +402,21 @@ static int erofs_source_ftruncate(struct erofs_vfile *vf, u64 length)
384402
static ssize_t erofs_source_read(struct erofs_vfile *vf, void *buf,
385403
size_t bytes)
386404
{
387-
return _source->read(buf, bytes);
405+
struct liberofs_file *source_file =
406+
reinterpret_cast<struct liberofs_file *>(vf->ops);
407+
408+
if (!source_file)
409+
return -EINVAL;
410+
return source_file->file->read(buf, bytes);
388411
}
389412

390413
static off_t erofs_source_lseek(struct erofs_vfile *vf, u64 offset, int whence)
391414
{
392-
return _source->lseek(offset, whence);
415+
struct liberofs_file *source_file =
416+
reinterpret_cast<struct liberofs_file *>(vf->ops);
417+
if (!source_file)
418+
return -EINVAL;
419+
return source_file->file->lseek(offset, whence);
393420
}
394421

395422
struct erofs_mkfs_cfg {
@@ -594,41 +621,41 @@ int LibErofs::extract_tar(photon::fs::IFile *source, bool meta_only, bool first_
594621
struct erofs_tarfile erofstar = {};
595622
struct erofs_mkfs_cfg cfg;
596623
struct erofs_configure *erofs_cfg;
624+
struct liberofs_file target_file, source_file;
597625
int err;
598626

599-
_target = target;
600-
_source = source;
601-
602-
target_vfops.pread = erofs_target_pread;
603-
target_vfops.pwrite = erofs_target_pwrite;
604-
target_vfops.pread = erofs_target_pread;
605-
target_vfops.pwrite = erofs_target_pwrite;
606-
target_vfops.fsync = erofs_target_fsync;
607-
target_vfops.fallocate = erofs_target_fallocate;
608-
target_vfops.ftruncate = erofs_target_ftruncate;
609-
target_vfops.read = erofs_target_read;
610-
target_vfops.lseek = erofs_target_lseek;
611-
612-
source_vfops.pread = erofs_source_pread;
613-
source_vfops.pwrite = erofs_source_pwrite;
614-
source_vfops.fsync = erofs_source_fsync;
615-
source_vfops.fallocate = erofs_source_fallocate;
616-
source_vfops.ftruncate = erofs_source_ftruncate;
617-
source_vfops.read = erofs_source_read;
618-
source_vfops.lseek = erofs_source_lseek;
619-
620-
erofs_cache.file = _target;
621-
erofs_cache.capacity = 128;
627+
target_file.ops.pread = erofs_target_pread;
628+
target_file.ops.pwrite = erofs_target_pwrite;
629+
target_file.ops.pread = erofs_target_pread;
630+
target_file.ops.pwrite = erofs_target_pwrite;
631+
target_file.ops.fsync = erofs_target_fsync;
632+
target_file.ops.fallocate = erofs_target_fallocate;
633+
target_file.ops.ftruncate = erofs_target_ftruncate;
634+
target_file.ops.read = erofs_target_read;
635+
target_file.ops.lseek = erofs_target_lseek;
636+
target_file.file = target;
637+
target_file.cache = new ErofsCache(target, 128);
638+
639+
source_file.ops.pread = erofs_source_pread;
640+
source_file.ops.pwrite = erofs_source_pwrite;
641+
source_file.ops.fsync = erofs_source_fsync;
642+
source_file.ops.fallocate = erofs_source_fallocate;
643+
source_file.ops.ftruncate = erofs_source_ftruncate;
644+
source_file.ops.read = erofs_source_read;
645+
source_file.ops.lseek = erofs_source_lseek;
646+
source_file.file = source;
647+
source_file.cache = NULL;
622648

623649
/* initialization of sbi */
624-
err = erofs_init_sbi(&sbi, _target, &target_vfops, ilog2(blksize));
650+
err = erofs_init_sbi(&sbi, target_file.file, &target_file.ops, ilog2(blksize));
625651
if (err) {
626-
erofs_close_sbi(&sbi, &erofs_cache);
652+
erofs_close_sbi(&sbi, target_file.cache);
653+
delete target_file.cache;
627654
LOG_ERROR("Failed to init sbi.");
628655
return err;
629656
}
630657
/* initialization of erofstar */
631-
err = erofs_init_tar(&erofstar, &source_vfops);
658+
err = erofs_init_tar(&erofstar, &source_file.ops);
632659
if (err) {
633660
LOG_ERROR("Failed to init tarerofs");
634661
goto exit;
@@ -655,15 +682,16 @@ int LibErofs::extract_tar(photon::fs::IFile *source, bool meta_only, bool first_
655682
}
656683

657684
/* write mapfile */
658-
err = erofs_write_map_file(_target, blksize, cfg.mp_fp);
685+
err = erofs_write_map_file(target_file.file, blksize, cfg.mp_fp);
659686
if (err) {
660687
LOG_ERROR("Failed to write mapfile.");
661688
goto exit;
662689
}
663690
exit:
664-
err = erofs_close_sbi(&sbi, &erofs_cache);
691+
err = erofs_close_sbi(&sbi, target_file.cache);
665692
erofs_close_tar(&erofstar);
666693
std::fclose(cfg.mp_fp);
694+
delete target_file.cache;
667695
return err;
668696
}
669697

0 commit comments

Comments
 (0)