ui: basic map scroller
This commit is contained in:
@@ -23,6 +23,7 @@ type fs struct {
|
||||
}
|
||||
|
||||
func NewFileStorage(path string) (*fs, error) {
|
||||
filepath.Join("", "")
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("path %s invalid: %s", path, err)
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
//go:build !windows
|
||||
|
||||
// for windows builds func [writable] should be refactored
|
||||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func dirExists(path string) (bool, error) {
|
||||
@@ -31,7 +27,3 @@ func pathExists(path string, isDir bool) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
func writable(filepath string) (bool, error) {
|
||||
return unix.Access(filepath, unix.W_OK) == nil, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build unix || (js && wasm) || wasip1
|
||||
|
||||
package fs
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
// writable reports whether path is writable on Windows.
|
||||
//
|
||||
// Semantics:
|
||||
// - for an existing regular file, it tries to open it for writing;
|
||||
// - for an existing directory, it tries to create and remove a temp file inside it;
|
||||
// - for other file types, it returns false with no error.
|
||||
func writable(filepath string) (bool, error) {
|
||||
return unix.Access(filepath, unix.W_OK) == nil, nil
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// writable reports whether path is writable on Windows.
|
||||
//
|
||||
// Semantics:
|
||||
// - for an existing regular file, it tries to open it for writing;
|
||||
// - for an existing directory, it tries to create and remove a temp file inside it;
|
||||
// - for other file types, it returns false with no error.
|
||||
//
|
||||
// This is intentionally an operational check, not a mode-bit check, because
|
||||
// on Windows effective writability is determined by ACLs and file attributes,
|
||||
// not by POSIX-like permission bits from os.FileMode.
|
||||
func writable(path string) (bool, error) {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return writableDir(path)
|
||||
}
|
||||
|
||||
if !info.Mode().IsRegular() {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return writableFile(path)
|
||||
}
|
||||
|
||||
// writableFile checks whether an existing regular file can be opened for writing.
|
||||
func writableFile(path string) (bool, error) {
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0)
|
||||
if err == nil {
|
||||
_ = f.Close()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if isPermissionLikeError(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
// writableDir checks whether a directory allows creating a child file.
|
||||
// That is usually the most useful definition of "directory is writable".
|
||||
func writableDir(path string) (bool, error) {
|
||||
pattern := filepath.Join(path, ".writable-check-*")
|
||||
f, err := os.CreateTemp(path, filepath.Base(pattern))
|
||||
if err == nil {
|
||||
name := f.Name()
|
||||
_ = f.Close()
|
||||
_ = os.Remove(name)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if isPermissionLikeError(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
// isPermissionLikeError normalizes the common Windows "access denied" style
|
||||
// failures to a simple false result instead of surfacing them as hard errors.
|
||||
func isPermissionLikeError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if errors.Is(err, os.ErrPermission) {
|
||||
return true
|
||||
}
|
||||
|
||||
var errno syscall.Errno
|
||||
if errors.As(err, &errno) {
|
||||
// ERROR_ACCESS_DENIED
|
||||
if errno == syscall.ERROR_ACCESS_DENIED {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
var pathErr *os.PathError
|
||||
if errors.As(err, &pathErr) && errors.Is(pathErr.Err, os.ErrPermission) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//go:build windows
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestWritable_NewFile verifies that a freshly created regular file is writable.
|
||||
func TestWritable_NewFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "file.txt")
|
||||
|
||||
err := os.WriteFile(path, []byte("x"), 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
ok, err := writable(path)
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
}
|
||||
|
||||
// TestWritable_NewDirectory verifies that a freshly created directory is writable
|
||||
// by checking that a temp file can be created inside it.
|
||||
func TestWritable_NewDirectory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
|
||||
ok, err := writable(dir)
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
}
|
||||
|
||||
// TestWritable_MissingPath verifies that a missing path returns an error from Stat.
|
||||
func TestWritable_MissingPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "missing")
|
||||
|
||||
ok, err := writable(path)
|
||||
require.Error(t, err)
|
||||
require.False(t, ok)
|
||||
}
|
||||
Reference in New Issue
Block a user