Skip to content

Commit b32fc04

Browse files
refacrot GetDirectorySize and preallocation
Signed-off-by: Nikita Korolev <[email protected]>
1 parent ff219ab commit b32fc04

File tree

2 files changed

+32
-35
lines changed
  • images/cdi-cloner/cloner-startup

2 files changed

+32
-35
lines changed

images/cdi-cloner/cloner-startup/cmd/cloner-startup/main.go

+9-21
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
)
2626

2727
func main() {
28+
var uploadBytes uint64
29+
2830
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
2931
slog.SetDefault(logger)
3032

@@ -61,6 +63,8 @@ func main() {
6163
os.Exit(1)
6264
}
6365

66+
logger.Info(fmt.Sprintf("Start clone block with %s", helpers.FormatBytes(float64(uploadBytes))))
67+
6468
if err = helpers.RunCloner("blockdevice-clone", uploadBytes, mountPoint); err != nil {
6569
logger.Error("Error running cdi-cloner: %v\n", slog.String("error", err.Error()))
6670
os.Exit(1)
@@ -73,38 +77,22 @@ func main() {
7377
os.Exit(1)
7478
}
7579

76-
// TODO correct log message
77-
if preallocation {
78-
logger.Info("Get only used blocks in bytes")
79-
} else {
80-
logger.Info("Preallocating filesystem, uploading all bytes")
81-
}
82-
// directory bytes, directory bytes
83-
// total count bytes, used count bytes
84-
// uploadBytes, err := helpers.GetDirectorySize(".", preallocation)
8580
totalBytes, totalUsedBytes, err := helpers.GetDirectorySize(".")
8681
if err != nil {
8782
logger.Error("Directory size calculation failed: %v\n", slog.String("error", err.Error()))
8883
os.Exit(1)
8984
}
9085

91-
/*
92-
if preallocation {
93-
uploadBytes := totalUsedBytes
94-
}else
95-
{
96-
uploadBytes := totalBytes
97-
}
98-
*/
99-
var uploadBytes uint64
100-
10186
if preallocation {
102-
uploadBytes = totalUsedBytes
103-
} else {
10487
uploadBytes = totalBytes
88+
logger.Info("Preallocating filesystem, uploading all bytes")
89+
} else {
90+
uploadBytes = totalUsedBytes
91+
logger.Info("Not preallocating filesystem, get only used blocks in bytes")
10592
}
10693

10794
logger.Info(fmt.Sprintf("Start clone with %d bytes", uploadBytes))
95+
logger.Info(fmt.Sprintf("Start clone with %s", helpers.FormatBytes(float64(uploadBytes))))
10896

10997
if err = helpers.RunCloner("filesystem-clone", uploadBytes, mountPoint); err != nil {
11098
logger.Error("Error running cdi-cloner: %v\n", slog.String("error", err.Error()))

images/cdi-cloner/cloner-startup/internal/helpers/size.go

+23-14
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func GetBlockDeviceSize(device string) (uint64, error) {
6464
return size, nil
6565
}
6666

67-
// GetDirectorySize calculates directory size using Go's filepath.Walk
68-
// func GetDirectorySize(path string, usedBlocks bool) (uint64, uint64, error) {
67+
// Calculates directory size using Go's filepath.Walk
68+
//
69+
// return totalBytes and totalUsedBytes(Blocks * Blksize)
6970
func GetDirectorySize(path string) (uint64, uint64, error) {
7071
var (
71-
// total uint64
7272
totalBytes uint64
7373
totalUsedBytes uint64
7474
)
@@ -83,18 +83,27 @@ func GetDirectorySize(path string) (uint64, uint64, error) {
8383
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
8484
totalUsedBytes += uint64(stat.Blocks * int64(stat.Blksize))
8585
}
86-
87-
// if usedBlocks {
88-
// // only used blocks
89-
// if stat, ok := info.Sys().(*syscall.Stat_t); ok {
90-
// total += uint64(stat.Blocks * int64(stat.Blksize))
91-
// }
92-
// } else {
93-
// // all bytes
94-
// total += uint64(info.Size())
95-
// }
9686
return nil
9787
})
9888
return totalBytes, totalUsedBytes, err
99-
// return total, err
89+
}
90+
91+
// Convert byte size to human readable format
92+
func FormatBytes(s float64) string {
93+
var base float64 = 1024.0
94+
sizes := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
95+
96+
unitsLimit := len(sizes)
97+
i := 0
98+
for s >= base && i < unitsLimit {
99+
s = s / base
100+
i++
101+
}
102+
103+
f := "%.0f %s"
104+
if i > 1 {
105+
f = "%.2f %s"
106+
}
107+
108+
return fmt.Sprintf(f, s, sizes[i])
100109
}

0 commit comments

Comments
 (0)