Skip to content

Commit 8689148

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

File tree

2 files changed

+33
-37
lines changed
  • images/cdi-cloner/cloner-startup

2 files changed

+33
-37
lines changed

Diff for: images/cdi-cloner/cloner-startup/cmd/cloner-startup/main.go

+10-23
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"cloner-startup/internal/helpers"
2021
"fmt"
2122
"log/slog"
2223
"os"
23-
24-
"cloner-startup/internal/helpers"
2524
)
2625

2726
func main() {
27+
var uploadBytes uint64
28+
2829
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
2930
slog.SetDefault(logger)
3031

@@ -61,6 +62,8 @@ func main() {
6162
os.Exit(1)
6263
}
6364

65+
logger.Info(fmt.Sprintf("Start clone block with %s", helpers.FormatBytes(float64(uploadBytes))))
66+
6467
if err = helpers.RunCloner("blockdevice-clone", uploadBytes, mountPoint); err != nil {
6568
logger.Error("Error running cdi-cloner: %v\n", slog.String("error", err.Error()))
6669
os.Exit(1)
@@ -73,38 +76,22 @@ func main() {
7376
os.Exit(1)
7477
}
7578

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)
8579
totalBytes, totalUsedBytes, err := helpers.GetDirectorySize(".")
8680
if err != nil {
8781
logger.Error("Directory size calculation failed: %v\n", slog.String("error", err.Error()))
8882
os.Exit(1)
8983
}
9084

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

10793
logger.Info(fmt.Sprintf("Start clone with %d bytes", uploadBytes))
94+
logger.Info(fmt.Sprintf("Start clone with %s", helpers.FormatBytes(float64(uploadBytes))))
10895

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

Diff for: 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 /= 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)