530 lines
13 KiB
Go
530 lines
13 KiB
Go
/*
|
|
|--------------------------------------------------------------------------
|
|
| Filesystem
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| Name:
|
|
| - Filesystem
|
|
|
|
|
| Purpose:
|
|
| - Execute deterministic filesystem operations through a unified interface.
|
|
|
|
|
| Guidelines:
|
|
| - Validate operation type before execution.
|
|
| - Validate configuration before execution.
|
|
| - Execute exactly one operation per invocation.
|
|
| - Return structured results.
|
|
| - Remain OS-agnostic.
|
|
|
|
|
| Examples:
|
|
| - ObrimFilesystem("list", config)
|
|
| - ObrimFilesystem("create", config)
|
|
|
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Credit
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| Contributor:
|
|
| - Rajon Ahmed
|
|
| - Scionite
|
|
|
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
package filesystem
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"go/module/essential/visible/service/helper/log"
|
|
"go/module/essential/visible/service/helper/status"
|
|
)
|
|
|
|
type filesystemResult struct {
|
|
Status bool `json:"status"`
|
|
Code string `json:"code"`
|
|
Payload map[string]any `json:"payload"`
|
|
}
|
|
|
|
// Function Name: ObrimFilesystem
|
|
// Function Purpose: Execute a filesystem operation and return a structured result.
|
|
func ObrimFilesystem(operationType string, config map[string]any) map[string]any {
|
|
log.ObrimLog("INIT", fmt.Sprintf("filesystem operation=%s", operationType))
|
|
|
|
if result := obrimFilesystemValidate(operationType, config); result != nil {
|
|
status.ObrimStatus("ERROR", result["code"].(string))
|
|
return result
|
|
}
|
|
|
|
resolvedPath, err := obrimFilesystemResolve(config)
|
|
if err != nil {
|
|
status.ObrimStatus("ERROR", err.Error())
|
|
|
|
return obrimFilesystemPayload(
|
|
false,
|
|
"FAILED_PATH_RESOLUTION",
|
|
nil,
|
|
)
|
|
}
|
|
|
|
config["_resolved_path"] = resolvedPath
|
|
|
|
switch operationType {
|
|
case "list":
|
|
return obrimFilesystemList(config)
|
|
|
|
case "check":
|
|
return obrimFilesystemCheck(config)
|
|
|
|
case "create":
|
|
return obrimFilesystemCreate(config)
|
|
|
|
case "delete":
|
|
return obrimFilesystemDelete(config)
|
|
|
|
case "permission":
|
|
return obrimFilesystemPermission(config)
|
|
}
|
|
|
|
return obrimFilesystemPayload(false, "FAILED_UNSUPPORTED_OPERATION", nil)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemValidate
|
|
// Function Purpose: Validate operation type, configuration, and supported keys.
|
|
func obrimFilesystemValidate(operationType string, config map[string]any) map[string]any {
|
|
validTypes := map[string]bool{
|
|
"list": true,
|
|
"check": true,
|
|
"create": true,
|
|
"delete": true,
|
|
"permission": true,
|
|
}
|
|
|
|
if !validTypes[operationType] {
|
|
return obrimFilesystemPayload(false, "FAILED_INVALID_TYPE", nil)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Function Name: obrimFilesystemResolve
|
|
// Function Purpose: Resolve and normalize filesystem paths.
|
|
func obrimFilesystemResolve(config map[string]any) (string, error) {
|
|
basePath := ""
|
|
|
|
strategy, _ := config["strategy"].(string)
|
|
|
|
switch strategy {
|
|
case "user":
|
|
currentUser, err := user.Current()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
basePath = currentUser.HomeDir
|
|
|
|
case "custom":
|
|
basePath, _ = config["base"].(string)
|
|
|
|
default:
|
|
basePath, _ = config["base"].(string)
|
|
}
|
|
|
|
name, _ := config["name"].(string)
|
|
|
|
if basePath == "" && name == "" {
|
|
basePath = "."
|
|
}
|
|
|
|
return filepath.Clean(filepath.Join(basePath, name)), nil
|
|
}
|
|
|
|
// Function Name: obrimFilesystemPayload
|
|
// Function Purpose: Build standardized output payload structures.
|
|
func obrimFilesystemPayload(statusValue bool, code string, payload map[string]any) map[string]any {
|
|
if !statusValue {
|
|
payload = nil
|
|
}
|
|
|
|
return map[string]any{
|
|
"status": statusValue,
|
|
"code": code,
|
|
"payload": payload,
|
|
}
|
|
}
|
|
|
|
// Function Name: obrimFilesystemEntityType
|
|
// Function Purpose: Determine filesystem entity type.
|
|
func obrimFilesystemEntityType(path string) string {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return "unknown"
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return "directory"
|
|
}
|
|
|
|
return "file"
|
|
}
|
|
|
|
// Function Name: obrimFilesystemPermissionValidate
|
|
// Function Purpose: Validate permission specifications before execution.
|
|
func obrimFilesystemPermissionValidate(permission string) error {
|
|
if permission == "" {
|
|
return fmt.Errorf("invalid permission")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Function Name: obrimFilesystemList
|
|
// Function Purpose: Execute filesystem listing operations.
|
|
func obrimFilesystemList(config map[string]any) map[string]any {
|
|
entries, err := obrimFilesystemListTraverse(config)
|
|
if err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_LIST", nil)
|
|
}
|
|
|
|
entries = obrimFilesystemListFilter(entries, config)
|
|
entries = obrimFilesystemListSort(entries, config)
|
|
|
|
payload := map[string]any{
|
|
"entries": entries,
|
|
"total": len(entries),
|
|
"recursive": config["recursive"] == true,
|
|
"base_path": config["_resolved_path"],
|
|
}
|
|
|
|
status.ObrimStatus("SUCCESS", "Filesystem listing completed.")
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_LIST", payload)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemListTraverse
|
|
// Function Purpose: Traverse filesystem entities according to listing configuration.
|
|
func obrimFilesystemListTraverse(config map[string]any) ([]map[string]any, error) {
|
|
// Logic:
|
|
var results []map[string]any
|
|
|
|
// Logic:
|
|
resolvedPath := config["_resolved_path"].(string)
|
|
|
|
// Logic:
|
|
recursive, _ := config["recursive"].(bool)
|
|
|
|
if recursive {
|
|
err := filepath.WalkDir(
|
|
resolvedPath,
|
|
func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
results = append(results, map[string]any{
|
|
"path": path,
|
|
"name": d.Name(),
|
|
})
|
|
|
|
return nil
|
|
},
|
|
)
|
|
|
|
return results, err
|
|
}
|
|
|
|
entries, err := os.ReadDir(resolvedPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
results = append(results, map[string]any{
|
|
"path": filepath.Join(resolvedPath, entry.Name()),
|
|
"name": entry.Name(),
|
|
})
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
// Function Name: obrimFilesystemListFilter
|
|
// Function Purpose: Apply entity and pattern filtering.
|
|
func obrimFilesystemListFilter(entries []map[string]any, config map[string]any) []map[string]any {
|
|
// Logic:
|
|
var filtered []map[string]any
|
|
|
|
// Logic:
|
|
pattern, _ := config["filter_pattern"].(string)
|
|
|
|
// Logic:
|
|
entity, _ := config["entity"].(string)
|
|
|
|
for _, entry := range entries {
|
|
path := entry["path"].(string)
|
|
|
|
if pattern != "" && !strings.Contains(filepath.Base(path), pattern) {
|
|
continue
|
|
}
|
|
|
|
if entity != "" && obrimFilesystemEntityType(path) != entity {
|
|
continue
|
|
}
|
|
|
|
filtered = append(filtered, entry)
|
|
}
|
|
|
|
return filtered
|
|
}
|
|
|
|
// Function Name: obrimFilesystemListSort
|
|
// Function Purpose: Apply result sorting behavior.
|
|
func obrimFilesystemListSort(entries []map[string]any, config map[string]any) []map[string]any {
|
|
sortBy, _ := config["sort_by"].(string)
|
|
sortOrder, _ := config["sort_order"].(string)
|
|
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
left := fmt.Sprintf("%v", entries[i][sortBy])
|
|
right := fmt.Sprintf("%v", entries[j][sortBy])
|
|
|
|
if sortOrder == "desc" {
|
|
return left > right
|
|
}
|
|
|
|
return left < right
|
|
})
|
|
|
|
return entries
|
|
}
|
|
|
|
// Function Name: obrimFilesystemCheck
|
|
// Function Purpose: Execute filesystem validation operations.
|
|
func obrimFilesystemCheck(config map[string]any) map[string]any {
|
|
path := config["_resolved_path"].(string)
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
payload := map[string]any{
|
|
"path": path,
|
|
"exists": err == nil,
|
|
"entity": obrimFilesystemEntityType(path),
|
|
"readable": obrimFilesystemCheckAccess(path, "read"),
|
|
"writable": obrimFilesystemCheckAccess(path, "write"),
|
|
}
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_CHECK", payload)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemCheckAccess
|
|
// Function Purpose: Verify filesystem accessibility requirements.
|
|
func obrimFilesystemCheckAccess(path string, mode string) bool {
|
|
if mode == "read" {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
_ = file.Close()
|
|
|
|
return true
|
|
}
|
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY, 0)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
_ = file.Close()
|
|
|
|
return true
|
|
}
|
|
|
|
// Function Name: obrimFilesystemCreate
|
|
// Function Purpose: Execute filesystem entity creation operations.
|
|
func obrimFilesystemCreate(config map[string]any) map[string]any {
|
|
entity, _ := config["entity"].(string)
|
|
|
|
switch entity {
|
|
case "file":
|
|
return obrimFilesystemCreateFile(config)
|
|
|
|
case "directory":
|
|
return obrimFilesystemCreateDirectory(config)
|
|
}
|
|
|
|
return obrimFilesystemPayload(false, "FAILED_CREATE", nil)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemCreateFile
|
|
// Function Purpose: Create a filesystem file.
|
|
func obrimFilesystemCreateFile(config map[string]any) map[string]any {
|
|
path := config["_resolved_path"].(string)
|
|
|
|
_ = os.MkdirAll(filepath.Dir(path), 0755)
|
|
|
|
file, err := os.Create(path)
|
|
if err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_CREATE_FILE", nil)
|
|
}
|
|
|
|
_ = file.Close()
|
|
|
|
payload := map[string]any{
|
|
"path": path,
|
|
"entity": "file",
|
|
"hidden": config["hidden"] == true,
|
|
"created": true,
|
|
}
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_CREATE_FILE", payload)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemCreateDirectory
|
|
// Function Purpose: Create a filesystem directory.
|
|
func obrimFilesystemCreateDirectory(config map[string]any) map[string]any {
|
|
path := config["_resolved_path"].(string)
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_CREATE_DIRECTORY", nil)
|
|
}
|
|
|
|
payload := map[string]any{
|
|
"path": path,
|
|
"entity": "directory",
|
|
"hidden": config["hidden"] == true,
|
|
"created": true,
|
|
}
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_CREATE_DIRECTORY", payload)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemDelete
|
|
// Function Purpose: Execute filesystem entity deletion operations.
|
|
func obrimFilesystemDelete(config map[string]any) map[string]any {
|
|
entity := obrimFilesystemEntityType(config["_resolved_path"].(string))
|
|
|
|
if entity == "file" {
|
|
return obrimFilesystemDeleteFile(config)
|
|
}
|
|
|
|
return obrimFilesystemDeleteDirectory(config)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemDeleteFile
|
|
// Function Purpose: Delete a filesystem file.
|
|
func obrimFilesystemDeleteFile(config map[string]any) map[string]any {
|
|
path := config["_resolved_path"].(string)
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_DELETE_FILE", nil)
|
|
}
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_DELETE_FILE", map[string]any{
|
|
"path": path,
|
|
"entity": "file",
|
|
"deleted": true,
|
|
})
|
|
}
|
|
|
|
// Function Name: obrimFilesystemDeleteDirectory
|
|
// Function Purpose: Delete a filesystem directory.
|
|
func obrimFilesystemDeleteDirectory(config map[string]any) map[string]any {
|
|
path := config["_resolved_path"].(string)
|
|
|
|
recursive, _ := config["recursive"].(bool)
|
|
|
|
var err error
|
|
|
|
if recursive {
|
|
err = os.RemoveAll(path)
|
|
} else {
|
|
err = os.Remove(path)
|
|
}
|
|
|
|
if err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_DELETE_DIRECTORY", nil)
|
|
}
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_DELETE_DIRECTORY", map[string]any{
|
|
"path": path,
|
|
"entity": "directory",
|
|
"deleted": true,
|
|
})
|
|
}
|
|
|
|
// Function Name: obrimFilesystemPermission
|
|
// Function Purpose: Execute filesystem permission operations.
|
|
func obrimFilesystemPermission(config map[string]any) map[string]any {
|
|
mode, _ := config["mode"].(string)
|
|
|
|
switch mode {
|
|
case "read":
|
|
return obrimFilesystemPermissionRead(config)
|
|
|
|
case "write":
|
|
return obrimFilesystemPermissionWrite(config)
|
|
}
|
|
|
|
return obrimFilesystemPayload(false, "FAILED_PERMISSION", nil)
|
|
}
|
|
|
|
// Function Name: obrimFilesystemPermissionRead
|
|
// Function Purpose: Retrieve filesystem permissions.
|
|
func obrimFilesystemPermissionRead(config map[string]any) map[string]any {
|
|
path := config["_resolved_path"].(string)
|
|
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_PERMISSION_READ", nil)
|
|
}
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_PERMISSION_READ", map[string]any{
|
|
"path": path,
|
|
"permission": info.Mode().Perm().String(),
|
|
"mode": "read",
|
|
"updated": false,
|
|
})
|
|
}
|
|
|
|
// Function Name: obrimFilesystemPermissionWrite
|
|
// Function Purpose: Update filesystem permissions.
|
|
func obrimFilesystemPermissionWrite(config map[string]any) map[string]any {
|
|
path := config["_resolved_path"].(string)
|
|
|
|
permission, _ := config["permission"].(string)
|
|
|
|
if err := obrimFilesystemPermissionValidate(permission); err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_INVALID_PERMISSION", nil)
|
|
}
|
|
|
|
var permissionValue fs.FileMode
|
|
|
|
_, err := fmt.Sscanf(permission, "%o", &permissionValue)
|
|
if err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_INVALID_PERMISSION", nil)
|
|
}
|
|
|
|
if err := os.Chmod(path, permissionValue); err != nil {
|
|
return obrimFilesystemPayload(false, "FAILED_PERMISSION_WRITE", nil)
|
|
}
|
|
|
|
return obrimFilesystemPayload(true, "SUCCESS_PERMISSION_WRITE", map[string]any{
|
|
"path": path,
|
|
"permission": permission,
|
|
"mode": "write",
|
|
"updated": true,
|
|
"timestamp": time.Now().UTC(),
|
|
})
|
|
}
|