Skip to content

Commit 173d3fa

Browse files
authoredMar 10, 2025··
SquashFS | Fix some bugs (#289)
* squashfs: add support for Disk.CreateFilesystem API Signed-off-by: Xynnn007 <[email protected]> * filesystem: add Close() method for cleaning jobs When building filesystems, we usually have some temp files or directories. The `Close()` method will help to do clean jobs when we destroy the FileSystem struct. Fixes #288 Signed-off-by: Xynnn007 <[email protected]> * squashfs: fix example disk block size squashfs requires block size must be >= 4KB. Signed-off-by: Xynnn007 <[email protected]> --------- Signed-off-by: Xynnn007 <[email protected]>
1 parent 6693615 commit 173d3fa

File tree

8 files changed

+43
-2
lines changed

8 files changed

+43
-2
lines changed
 

‎disk/disk.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (d *Disk) CreateFilesystem(spec FilesystemSpec) (filesystem.FileSystem, err
177177
case filesystem.TypeExt4:
178178
return ext4.Create(d.Backend, size, start, d.LogicalBlocksize, nil)
179179
case filesystem.TypeSquashfs:
180-
return nil, filesystem.ErrReadonlyFilesystem
180+
return squashfs.Create(d.Backend, size, start, d.LogicalBlocksize)
181181
default:
182182
return nil, errors.New("unknown filesystem type requested")
183183
}

‎examples/iso_create.go

+7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ func CreateIso(diskImg string) {
2525
fspec := disk.FilesystemSpec{Partition: 0, FSType: filesystem.TypeISO9660, VolumeLabel: "label"}
2626
fs, err := mydisk.CreateFilesystem(fspec)
2727
check(err)
28+
defer func() {
29+
if err := fs.Close(); err != nil {
30+
check(err)
31+
}
32+
}()
33+
2834
rw, err := fs.OpenFile("demo.txt", os.O_CREATE|os.O_RDWR)
35+
check(err)
2936
content := []byte("demo")
3037
_, err = rw.Write(content)
3138
check(err)

‎examples/squashfs_create.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ func CreateSquashfs(diskImg string) {
1616
log.Fatal("must have a valid path for diskImg")
1717
}
1818
var diskSize int64 = 10 * 1024 * 1024 // 10 MB
19-
mydisk, err := diskfs.Create(diskImg, diskSize, diskfs.SectorSizeDefault)
19+
mydisk, err := diskfs.Create(diskImg, diskSize, diskfs.SectorSize4k)
2020
check(err)
2121

2222
fspec := disk.FilesystemSpec{Partition: 0, FSType: filesystem.TypeSquashfs, VolumeLabel: "label"}
2323
fs, err := mydisk.CreateFilesystem(fspec)
2424
check(err)
25+
defer func() {
26+
if err := fs.Close(); err != nil {
27+
check(err)
28+
}
29+
}()
2530
rw, err := fs.OpenFile("demo.txt", os.O_CREATE|os.O_RDWR)
31+
check(err)
2632
content := []byte("demo")
2733
_, err = rw.Write(content)
2834
check(err)

‎filesystem/ext4/ext4.go

+5
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ func Read(b backend.Storage, size, start, sectorsize int64) (*FileSystem, error)
693693
// interface guard
694694
var _ filesystem.FileSystem = (*FileSystem)(nil)
695695

696+
// Do cleaning job for ext4. Note that ext4 does not have side-effects so we do not do anything.
697+
func (fs *FileSystem) Close() error {
698+
return nil
699+
}
700+
696701
// Type returns the type code for the filesystem. Always returns filesystem.TypeExt4
697702
func (fs *FileSystem) Type() filesystem.Type {
698703
return filesystem.TypeExt4

‎filesystem/fat32/fat32.go

+5
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ func (fs *FileSystem) writeFat() error {
489489
// interface guard
490490
var _ filesystem.FileSystem = (*FileSystem)(nil)
491491

492+
// Do cleaning job for fat32. Note that fat32 does not have side-effects so we do not do anything.
493+
func (fs *FileSystem) Close() error {
494+
return nil
495+
}
496+
492497
// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
493498
func (fs *FileSystem) Type() filesystem.Type {
494499
return filesystem.TypeFat32

‎filesystem/filesystem.go

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type FileSystem interface {
4646
// SetLabel changes the label on the writable filesystem. Different file system may hav different
4747
// length constraints.
4848
SetLabel(label string) error
49+
// Close will cleanup the temporary files created by the filesystem generation steps
50+
Close() error
4951
}
5052

5153
// Type represents the type of disk this is

‎filesystem/iso9660/iso9660.go

+8
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ func Read(b backend.Storage, size, start, blocksize int64) (*FileSystem, error)
289289
// interface guard
290290
var _ filesystem.FileSystem = (*FileSystem)(nil)
291291

292+
// Delete the temporary directory created during the iso9660 image creation
293+
func (fsm *FileSystem) Close() error {
294+
if fsm.workspace != "" {
295+
return os.RemoveAll(fsm.workspace)
296+
}
297+
return nil
298+
}
299+
292300
// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
293301
func (fsm *FileSystem) Type() filesystem.Type {
294302
return filesystem.TypeISO9660

‎filesystem/squashfs/squashfs.go

+8
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ func Read(b backend.Storage, size, start, blocksize int64) (*FileSystem, error)
232232
// interface guard
233233
var _ filesystem.FileSystem = (*FileSystem)(nil)
234234

235+
// Delete the temporary directory created during the SquashFS image creation
236+
func (fs *FileSystem) Close() error {
237+
if fs.workspace != "" {
238+
return os.RemoveAll(fs.workspace)
239+
}
240+
return nil
241+
}
242+
235243
// Type returns the type code for the filesystem. Always returns filesystem.TypeFat32
236244
func (fs *FileSystem) Type() filesystem.Type {
237245
return filesystem.TypeSquashfs

0 commit comments

Comments
 (0)
Please sign in to comment.