From e767446dcec4d011806e81e0578f196ed7f4c5e2 Mon Sep 17 00:00:00 2001 From: t4lz Date: Tue, 4 Feb 2025 14:11:01 +0100 Subject: [PATCH 1/5] go-statfs --- go-statfs/Dockerfile | 10 ++++++++ go-statfs/go.mod | 3 +++ go-statfs/main.go | 54 ++++++++++++++++++++++++++++++++++++++++++++ go-statfs/test.txt | 1 + 4 files changed, 68 insertions(+) create mode 100644 go-statfs/Dockerfile create mode 100644 go-statfs/go.mod create mode 100644 go-statfs/main.go create mode 100644 go-statfs/test.txt diff --git a/go-statfs/Dockerfile b/go-statfs/Dockerfile new file mode 100644 index 0000000..a011802 --- /dev/null +++ b/go-statfs/Dockerfile @@ -0,0 +1,10 @@ +FROM golang:1.23-alpine3.20 +WORKDIR /app +# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change +COPY go.mod test.txt ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build -v -o /app ./... + +CMD ["app"] diff --git a/go-statfs/go.mod b/go-statfs/go.mod new file mode 100644 index 0000000..38ea6f9 --- /dev/null +++ b/go-statfs/go.mod @@ -0,0 +1,3 @@ +module main.go + +go 1.23.5 diff --git a/go-statfs/main.go b/go-statfs/main.go new file mode 100644 index 0000000..9df3430 --- /dev/null +++ b/go-statfs/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "encoding/json" + "fmt" + _ "net" // for dynamic linking + "os" + "os/signal" + "syscall" +) + +func main() { + tempFile := "/" + var statfs syscall.Statfs_t + err := syscall.Statfs(tempFile, &statfs) + if err != nil { + fmt.Println("Error:", err) + return + } + + // Convert struct to a JSON-friendly format + data := map[string]interface{}{ + "bavail": statfs.Bavail, + "bfree": statfs.Bfree, + "blocks": statfs.Blocks, + "bsize": statfs.Bsize, + "ffree": statfs.Ffree, + "files": statfs.Files, + "flags": statfs.Flags, + "frsize": statfs.Frsize, + "fsid": []int64{int64(statfs.Fsid.X__val[0]), int64(statfs.Fsid.X__val[1])}, // Convert fsid to list + "namelen": statfs.Namelen, + "spare": statfs.Spare, + "type": statfs.Type, + } + + // Convert to JSON + jsonData, err := json.MarshalIndent(data, "", " ") + if err != nil { + fmt.Println("JSON Encoding Error:", err) + return + } + + // Print JSON + fmt.Println(string(jsonData)) + + // Create a channel to receive OS signals + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + // Block forever waiting for a signal + <-sigs + +} diff --git a/go-statfs/test.txt b/go-statfs/test.txt new file mode 100644 index 0000000..1b37687 --- /dev/null +++ b/go-statfs/test.txt @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. From 1914567cd50b350913b020904473761c7e94bccd Mon Sep 17 00:00:00 2001 From: t4lz Date: Tue, 4 Feb 2025 14:14:24 +0100 Subject: [PATCH 2/5] module name --- go-statfs/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-statfs/go.mod b/go-statfs/go.mod index 38ea6f9..93c750b 100644 --- a/go-statfs/go.mod +++ b/go-statfs/go.mod @@ -1,3 +1,3 @@ -module main.go +module go-statfs go 1.23.5 From 891c5ebf89e1c52d7cfe3148d607bfc004843b24 Mon Sep 17 00:00:00 2001 From: t4lz Date: Tue, 4 Feb 2025 14:18:16 +0100 Subject: [PATCH 3/5] dockerfile entrypoint --- go-statfs/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go-statfs/Dockerfile b/go-statfs/Dockerfile index a011802..26ad174 100644 --- a/go-statfs/Dockerfile +++ b/go-statfs/Dockerfile @@ -5,6 +5,6 @@ COPY go.mod test.txt ./ RUN go mod download && go mod verify COPY . . -RUN go build -v -o /app ./... +RUN go build -v -o /app/go-statfs ./... -CMD ["app"] +CMD ["/app/go-statfs"] From b04bd32739bccf89b06554bfa1b54b0517c95d8c Mon Sep 17 00:00:00 2001 From: t4lz Date: Thu, 6 Feb 2025 23:51:08 +0100 Subject: [PATCH 4/5] fsid is int32 --- go-statfs/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-statfs/main.go b/go-statfs/main.go index 9df3430..26268d0 100644 --- a/go-statfs/main.go +++ b/go-statfs/main.go @@ -28,7 +28,7 @@ func main() { "files": statfs.Files, "flags": statfs.Flags, "frsize": statfs.Frsize, - "fsid": []int64{int64(statfs.Fsid.X__val[0]), int64(statfs.Fsid.X__val[1])}, // Convert fsid to list + "fsid": []int32{int32(statfs.Fsid.X__val[0]), int32(statfs.Fsid.X__val[1])}, "namelen": statfs.Namelen, "spare": statfs.Spare, "type": statfs.Type, From a36452a66e86e3b46ad929e57539e04eef4625aa Mon Sep 17 00:00:00 2001 From: t4lz Date: Sun, 9 Feb 2025 13:46:30 +0100 Subject: [PATCH 5/5] varname --- go-statfs/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go-statfs/main.go b/go-statfs/main.go index 26268d0..aa45448 100644 --- a/go-statfs/main.go +++ b/go-statfs/main.go @@ -10,9 +10,9 @@ import ( ) func main() { - tempFile := "/" + rootPath := "/" var statfs syscall.Statfs_t - err := syscall.Statfs(tempFile, &statfs) + err := syscall.Statfs(rootPath, &statfs) if err != nil { fmt.Println("Error:", err) return