diff --git a/fs/fs.go b/fs/fs.go new file mode 100644 index 0000000..b2fa879 --- /dev/null +++ b/fs/fs.go @@ -0,0 +1,46 @@ +package fs + +import ( + "os" + "path/filepath" +) + +// CaseInsensitive check +func CaseInsensitive(dir string) (yes bool, err error) { + pathLowercase := filepath.Join(dir, "file") + fLowercase, err := os.Create(pathLowercase) + if err != nil { + return + } + defer fLowercase.Close() + defer os.Remove(pathLowercase) + + pathUppercase := filepath.Join(dir, "FILE") + fUppercase, err := os.Create(pathUppercase) + if err != nil { + return + } + defer fUppercase.Close() + defer os.Remove(pathUppercase) + + statLowercase, err := fLowercase.Stat() + if err != nil { + return + } + + statUppercase, err := fUppercase.Stat() + if err != nil { + return + } + + yes = os.SameFile(statLowercase, statUppercase) + return +} + +// PathExists check +func PathExists(path string) bool { + if _, err := os.Stat(path); err != nil { + return false + } + return true +} diff --git a/images.go b/images.go index 755da19..b909af6 100644 --- a/images.go +++ b/images.go @@ -17,6 +17,7 @@ import ( "time" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/fs" "code.dumpstack.io/tools/out-of-tree/qemu" "github.com/rs/zerolog/log" ) @@ -58,7 +59,7 @@ func (cmd *ImageEditCmd) Run(g *Globals) (err error) { } image := usr.HomeDir + "/.out-of-tree/images/" + cmd.Name - if !exists(image) { + if !fs.PathExists(image) { fmt.Println("image does not exist") } diff --git a/kernel.go b/kernel.go index a13d7e5..a6ba9de 100644 --- a/kernel.go +++ b/kernel.go @@ -23,6 +23,7 @@ import ( "github.com/rs/zerolog/log" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/fs" ) type KernelCmd struct { @@ -352,7 +353,7 @@ func generateBaseDockerImage(registry string, commands []config.DockerCommand, return } - if exists(dockerPath) && string(rawOutput) != "" { + if fs.PathExists(dockerPath) && string(rawOutput) != "" { log.Info().Msgf("Base image for %s:%s found", sk.DistroType.String(), sk.DistroRelease) if !forceUpdate { @@ -650,7 +651,7 @@ func genRootfsImage(d containerImageInfo, download bool) (rootfs string, err err os.MkdirAll(imagesPath, os.ModePerm) rootfs = imagesPath + imageFile - if !exists(rootfs) { + if !fs.PathExists(rootfs) { if download { log.Info().Msgf("%v not available, start download", imageFile) err = downloadImage(imagesPath, imageFile) diff --git a/kernel_linux.go b/kernel_linux.go index 4309c86..36ea5a5 100644 --- a/kernel_linux.go +++ b/kernel_linux.go @@ -16,6 +16,7 @@ import ( "github.com/zcalusic/sysinfo" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/fs" ) func genHostKernels(download bool) (kcfg config.KernelConfig, err error) { @@ -85,7 +86,7 @@ func genHostKernels(download bool) (kcfg config.KernelConfig, err error) { vmlinux := "/usr/lib/debug/boot/vmlinux-" + krel log.Print("vmlinux", vmlinux) - if exists(vmlinux) { + if fs.PathExists(vmlinux) { ki.VmlinuxPath = vmlinux } diff --git a/main.go b/main.go index 6e5a3d3..146521c 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "github.com/alecthomas/kong" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/fs" ) type Globals struct { @@ -94,37 +95,6 @@ func (lw *LevelWriter) WriteLevel(l zerolog.Level, p []byte) (n int, err error) return len(p), nil } -func isFsCaseInsensitive(dir string) (yes bool, err error) { - pathLowercase := filepath.Join(dir, "file") - fLowercase, err := os.Create(pathLowercase) - if err != nil { - return - } - defer fLowercase.Close() - defer os.Remove(pathLowercase) - - pathUppercase := filepath.Join(dir, "FILE") - fUppercase, err := os.Create(pathUppercase) - if err != nil { - return - } - defer fUppercase.Close() - defer os.Remove(pathUppercase) - - statLowercase, err := fLowercase.Stat() - if err != nil { - return - } - - statUppercase, err := fUppercase.Stat() - if err != nil { - return - } - - yes = os.SameFile(statLowercase, statUppercase) - return -} - var tempDirBase string var consoleWriter, fileWriter LevelWriter @@ -197,7 +167,7 @@ func main() { } path := filepath.Join(usr.HomeDir, ".out-of-tree") - yes, err := isFsCaseInsensitive(path) + yes, err := fs.CaseInsensitive(path) if err != nil { log.Fatal().Err(err).Msg(path) } diff --git a/pack.go b/pack.go index c79cf6f..9ee8375 100644 --- a/pack.go +++ b/pack.go @@ -9,6 +9,8 @@ import ( "io/ioutil" "time" + "code.dumpstack.io/tools/out-of-tree/fs" + "github.com/rs/zerolog/log" ) @@ -41,7 +43,7 @@ func (cmd *PackCmd) Run(g *Globals) (err error) { for _, f := range files { workPath := g.WorkDir + "/" + f.Name() - if !exists(workPath + "/.out-of-tree.toml") { + if !fs.PathExists(workPath + "/.out-of-tree.toml") { continue } diff --git a/pew.go b/pew.go index 8107252..b4a2310 100644 --- a/pew.go +++ b/pew.go @@ -24,6 +24,7 @@ import ( "gopkg.in/logrusorgru/aurora.v2" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/fs" "code.dumpstack.io/tools/out-of-tree/qemu" ) @@ -691,7 +692,7 @@ func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup, if cmd.Test == "" { cmd.Test = result.BuildArtifact + "_test" - if !exists(cmd.Test) { + if !fs.PathExists(cmd.Test) { slog.Debug().Msgf("%s does not exist", cmd.Test) cmd.Test = tmp + "/source/" + "test.sh" } else { @@ -824,13 +825,6 @@ func (cmd PewCmd) performCI(ka config.Artifact) (err error) { return } -func exists(path string) bool { - if _, err := os.Stat(path); err != nil { - return false - } - return true -} - func kernelMask(kernel string) (km config.KernelMask, err error) { parts := strings.Split(kernel, ":") if len(parts) != 2 { diff --git a/preload.go b/preload.go index 2b6e5e5..acafadd 100644 --- a/preload.go +++ b/preload.go @@ -18,6 +18,7 @@ import ( "github.com/rs/zerolog/log" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/fs" "code.dumpstack.io/tools/out-of-tree/qemu" ) @@ -68,7 +69,7 @@ func buildAndInsmod(workPath string, q *qemu.System, ki config.KernelInfo, defer os.RemoveAll(tmp) var artifact string - if exists(cache) { + if fs.PathExists(cache) { artifact = cache } else { artifact, err = buildPreload(workPath, tmp, ki, dockerTimeout) @@ -124,7 +125,7 @@ func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, err workPath = filepath.Join(base, "/repos/", sha1sum(repo)) var r *git.Repository - if exists(workPath) { + if fs.PathExists(workPath) { r, err = git.PlainOpen(workPath) if err != nil { return