Files
Gumble-Backend/internal/util/fs.go
Zane Walker 7e940c83a7
Some checks failed
Build & Test / build-test (push) Has been cancelled
Build & Test / swagger-codegen-cli (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
(Feat): Initial Commit
2026-07-03 19:41:31 +05:30

40 lines
938 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// nolint:revive
package util
import (
"fmt"
"os"
"time"
)
// TouchFile creates an empty file if the file doesnt already exist.
// If the file already exists then TouchFile updates the modified time of the file.
// Returns the modification time of the created / updated file.
func TouchFile(absolutePathToFile string) (time.Time, error) {
_, err := os.Stat(absolutePathToFile)
if os.IsNotExist(err) {
file, err := os.Create(absolutePathToFile)
if err != nil {
return time.Time{}, fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return time.Time{}, fmt.Errorf("failed to stat file: %w", err)
}
return stat.ModTime(), nil
}
currentTime := time.Now().Local()
err = os.Chtimes(absolutePathToFile, currentTime, currentTime)
if err != nil {
return time.Time{}, fmt.Errorf("failed to change file time: %w", err)
}
return currentTime, nil
}