From 6773d51caf62e55d03955378e57429a549d06355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=28=D0=A5?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D1=8B=D1=87=D0=AA=29=20=D0=A5=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B2?= Date: Sun, 21 Sep 2025 16:42:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9?= =?UTF-8?q?=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B8=20fil?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/shutils/helpers/files_find.go | 225 +++++++++++++++++++++++++ internal/shutils/helpers/helpers.go | 26 ++- 2 files changed, 244 insertions(+), 7 deletions(-) diff --git a/internal/shutils/helpers/files_find.go b/internal/shutils/helpers/files_find.go index 25e8b1f..570fb8f 100644 --- a/internal/shutils/helpers/files_find.go +++ b/internal/shutils/helpers/files_find.go @@ -177,3 +177,228 @@ func filesFindCmd(hc interp.HandlerContext, cmd string, args []string) error { return outputFiles(hc, foundFiles) } + +func filesFindBinCmd(hc interp.HandlerContext, cmd string, args []string) error { + namePattern := "*" + if len(args) > 0 { + namePattern = args[0] + } + + binPath := "./usr/bin/" + realPath := path.Join(hc.Dir, binPath) + + if err := validateDir(realPath, "files-find-bin"); err != nil { + return err + } + + var binFiles []string + err := filepath.Walk(realPath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && matchNamePattern(info.Name(), namePattern) { + relPath, relErr := makeRelativePath(hc.Dir, p) + if relErr != nil { + return relErr + } + binFiles = append(binFiles, relPath) + } + return nil + }) + if err != nil { + return fmt.Errorf("files-find-bin: %w", err) + } + + return outputFiles(hc, binFiles) +} + +func filesFindLibCmd(hc interp.HandlerContext, cmd string, args []string) error { + namePattern := "*" + if len(args) > 0 { + namePattern = args[0] + } + + libPaths := []string{"./usr/lib/", "./usr/lib64/"} + var libFiles []string + + for _, libPath := range libPaths { + realPath := path.Join(hc.Dir, libPath) + if _, err := os.Stat(realPath); os.IsNotExist(err) { + continue + } + + err := filepath.Walk(realPath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && matchNamePattern(info.Name(), namePattern) { + relPath, relErr := makeRelativePath(hc.Dir, p) + if relErr != nil { + return relErr + } + libFiles = append(libFiles, relPath) + } + return nil + }) + if err != nil { + return fmt.Errorf("files-find-lib: %w", err) + } + } + + return outputFiles(hc, libFiles) +} + +func filesFindIncludeCmd(hc interp.HandlerContext, cmd string, args []string) error { + namePattern := "*" + if len(args) > 0 { + namePattern = args[0] + } + + includePath := "./usr/include/" + realPath := path.Join(hc.Dir, includePath) + + if err := validateDir(realPath, "files-find-include"); err != nil { + return err + } + + var includeFiles []string + err := filepath.Walk(realPath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && matchNamePattern(info.Name(), namePattern) { + relPath, relErr := makeRelativePath(hc.Dir, p) + if relErr != nil { + return relErr + } + includeFiles = append(includeFiles, relPath) + } + return nil + }) + if err != nil { + return fmt.Errorf("files-find-include: %w", err) + } + + return outputFiles(hc, includeFiles) +} + +func filesFindShareCmd(hc interp.HandlerContext, cmd string, args []string) error { + namePattern := "*" + sharePath := "./usr/share/" + + if len(args) > 0 { + if len(args) == 1 { + sharePath = "./usr/share/" + args[0] + "/" + } else { + sharePath = "./usr/share/" + args[0] + "/" + namePattern = args[1] + } + } + + realPath := path.Join(hc.Dir, sharePath) + + if err := validateDir(realPath, "files-find-share"); err != nil { + return err + } + + var shareFiles []string + err := filepath.Walk(realPath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && matchNamePattern(info.Name(), namePattern) { + relPath, relErr := makeRelativePath(hc.Dir, p) + if relErr != nil { + return relErr + } + shareFiles = append(shareFiles, relPath) + } + return nil + }) + if err != nil { + return fmt.Errorf("files-find-share: %w", err) + } + + return outputFiles(hc, shareFiles) +} + +func filesFindManCmd(hc interp.HandlerContext, cmd string, args []string) error { + namePattern := "*" + manSection := "*" + + if len(args) > 0 { + if len(args) == 1 { + manSection = args[0] + } else { + manSection = args[0] + namePattern = args[1] + } + } + + manPath := "./usr/share/man/man" + manSection + "/" + realPath := path.Join(hc.Dir, manPath) + + if err := validateDir(realPath, "files-find-man"); err != nil { + return err + } + + var manFiles []string + err := filepath.Walk(realPath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && matchNamePattern(info.Name(), namePattern) { + relPath, relErr := makeRelativePath(hc.Dir, p) + if relErr != nil { + return relErr + } + manFiles = append(manFiles, relPath) + } + return nil + }) + if err != nil { + return fmt.Errorf("files-find-man: %w", err) + } + + return outputFiles(hc, manFiles) +} + +func filesFindConfigCmd(hc interp.HandlerContext, cmd string, args []string) error { + namePattern := "*" + if len(args) > 0 { + namePattern = args[0] + } + + configPath := "./etc/" + realPath := path.Join(hc.Dir, configPath) + + if err := validateDir(realPath, "files-find-config"); err != nil { + return err + } + + var configFiles []string + err := filepath.Walk(realPath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && matchNamePattern(info.Name(), namePattern) { + relPath, relErr := makeRelativePath(hc.Dir, p) + if relErr != nil { + return relErr + } + configFiles = append(configFiles, relPath) + } + return nil + }) + if err != nil { + return fmt.Errorf("files-find-config: %w", err) + } + + return outputFiles(hc, configFiles) +} diff --git a/internal/shutils/helpers/helpers.go b/internal/shutils/helpers/helpers.go index 89a04a6..c39b310 100644 --- a/internal/shutils/helpers/helpers.go +++ b/internal/shutils/helpers/helpers.go @@ -56,18 +56,30 @@ var Helpers = handlers.ExecFuncs{ "install-library": installLibraryCmd, "git-version": gitVersionCmd, - "files-find": filesFindCmd, - "files-find-lang": filesFindLangCmd, - "files-find-doc": filesFindDocCmd, + "files-find": filesFindCmd, + "files-find-lang": filesFindLangCmd, + "files-find-doc": filesFindDocCmd, + "files-find-bin": filesFindBinCmd, + "files-find-lib": filesFindLibCmd, + "files-find-include": filesFindIncludeCmd, + "files-find-share": filesFindShareCmd, + "files-find-man": filesFindManCmd, + "files-find-config": filesFindConfigCmd, } // Restricted contains restricted read-only helper commands // that don't modify any state var Restricted = handlers.ExecFuncs{ - "git-version": gitVersionCmd, - "files-find": filesFindCmd, - "files-find-lang": filesFindLangCmd, - "files-find-doc": filesFindDocCmd, + "git-version": gitVersionCmd, + "files-find": filesFindCmd, + "files-find-lang": filesFindLangCmd, + "files-find-doc": filesFindDocCmd, + "files-find-bin": filesFindBinCmd, + "files-find-lib": filesFindLibCmd, + "files-find-include": filesFindIncludeCmd, + "files-find-share": filesFindShareCmd, + "files-find-man": filesFindManCmd, + "files-find-config": filesFindConfigCmd, } func installHelperCmd(prefix string, perms os.FileMode) handlers.ExecFunc {