commit fcb09c1f0a1a4d3e6edc8d8d8ae7fca38a3445f6 Author: Brandon Dyck Date: Mon Aug 19 16:30:30 2024 -0600 First commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..4892872 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# Go file permissions test +by Brandon Dyck + +This is an experiment to see how `os.Chmod` and `os.Stat` interact on Windows. The program creates a temporary file, then for each possible set of permissions (0–0777), sets those permissions on the file with `os.Chmod` and checks the resulting permissions with `os.Stat`. + +## Usage + +### `go-perm-test` + +Prints the results as a CSV in the format + +```,``` + +where each set of permissions is an octal integer. + +### `go-perm-test ` + +Inserts the results into an SQLite database called `filename`. +There are two objects in the DB schema: a table `result_raw(expected INT, actual INT)` and a view `result`. + +The `result` view breaks down the permissions into separate fields: The raw data are in the `expected` and `actual` columns, 3-bit fields are in `{expected,actual}_{u,g,o}` columns, and 1-bit fields are in `{expected,actual}_{u,g,o}{r,w,x}` columns. + +## Findings + +Permissions as observed through `os.Stat` depend entirely upon the owner's write bit set through `os.Chmod`: + +``` +sqlite> select distinct actual, expected_uw from result; +actual|expected_uw +292|0 +438|1 +``` + +This obviously isn't an exhaustive treatment of the subject, and I'm not really interested in doing one, but it corraborates [Michal Pristas’s findings.](https://archive.is/RZ8WP) \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..604827b --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module git.codemonkeysoftware.net/b/go-perm-test + +go 1.22.1 + +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/mattn/go-isatty v0.0.16 // indirect + github.com/ncruces/go-strftime v0.1.9 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/sys v0.16.0 // indirect + modernc.org/libc v1.41.0 // indirect + modernc.org/mathutil v1.6.0 // indirect + modernc.org/memory v1.7.2 // indirect + modernc.org/sqlite v1.29.1 // indirect + zombiezen.com/go/sqlite v1.3.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..49f73c7 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +modernc.org/libc v1.41.0 h1:g9YAc6BkKlgORsUWj+JwqoB1wU3o4DE3bM3yvA3k+Gk= +modernc.org/libc v1.41.0/go.mod h1:w0eszPsiXoOnoMJgrXjglgLuDy/bt5RR4y3QzUUeodY= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= +modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= +modernc.org/sqlite v1.29.1 h1:19GY2qvWB4VPw0HppFlZCPAbmxFU41r+qjKZQdQ1ryA= +modernc.org/sqlite v1.29.1/go.mod h1:hG41jCYxOAOoO6BRK66AdRlmOcDzXf7qnwlwjUIOqa0= +zombiezen.com/go/sqlite v1.3.0 h1:98g1gnCm+CNz6AuQHu0gqyw7gR2WU3O3PJufDOStpUs= +zombiezen.com/go/sqlite v1.3.0/go.mod h1:yRl27//s/9aXU3RWs8uFQwjkTG9gYNGEls6+6SvrclY= diff --git a/main_windows.go b/main_windows.go new file mode 100644 index 0000000..049b088 --- /dev/null +++ b/main_windows.go @@ -0,0 +1,133 @@ +package main + +import ( + "fmt" + "io/fs" + "os" + "strings" + + "zombiezen.com/go/sqlite" + "zombiezen.com/go/sqlite/sqlitex" +) + +type Result struct{ Expected, Actual fs.FileMode } + +type SQLiteEmitter struct { + conn *sqlite.Conn +} + +func (s *SQLiteEmitter) Close() error { + return s.conn.Close() +} + +func sqlViewColSpec() string { + var cols []string + var whos = []rune{'o', 'g', 'u'} + var whats = []rune{'x', 'w', 'r'} + for _, fieldName := range []string{"expected", "actual"} { + cols = append(cols, fmt.Sprintf("%s", fieldName)) + for iwho, who := range whos { + cols = append(cols, fmt.Sprintf("%[1]s & (7 << %[3]d * 3) >> (%[3]d * 3) AS %[1]s_%[2]c", fieldName, who, iwho)) + for iwhat, what := range whats { + cols = append(cols, fmt.Sprintf("%[1]s & (1 << %[3]d * 3 + %[5]d) >> (%[3]d * 3 + %[5]d) AS %[1]s_%[2]c%[4]c", fieldName, who, iwho, what, iwhat)) + } + } + } + return strings.Join(cols, ",\n") +} + +const createTable = `CREATE TABLE result_raw ( + expected INT NOT NULL, + actual INT NOT NULL +);` + +var createView string = `CREATE VIEW result AS +SELECT +` + sqlViewColSpec() + ` +FROM result_raw;` + +func NewSQLite(path string) (*SQLiteEmitter, error) { + conn, err := sqlite.OpenConn(path, sqlite.OpenCreate|sqlite.OpenReadWrite|sqlite.OpenWAL) + if err != nil { + return nil, err + } + fmt.Println(createView) + err = sqlitex.Execute(conn, createTable, &sqlitex.ExecOptions{}) + if err != nil { + return nil, fmt.Errorf("NewSQLite: cannot create table: %w", err) + } + err = sqlitex.Execute(conn, createView, &sqlitex.ExecOptions{}) + if err != nil { + return nil, fmt.Errorf("NewSQLite: cannot create view: %w", err) + } + return &SQLiteEmitter{ + conn: conn, + }, nil +} + +func (s *SQLiteEmitter) Emit(r Result) error { + return sqlitex.Execute(s.conn, "INSERT INTO result_raw(expected, actual) VALUES (?, ?)", &sqlitex.ExecOptions{ + Args: []any{r.Expected, r.Actual}, + }) +} + +func emitCSV(r Result) error { + _, err := fmt.Printf("%03o,%03o\n", r.Expected, r.Actual) + if err != nil { + return fmt.Errorf("emitCSV: %w", err) + } + return nil +} + +func getData(emit func(Result) error) (err error) { + f, err := os.CreateTemp("", "go-perm-test-*") + if err != nil { + return err + } + path := f.Name() + f.Close() + + for mode := fs.FileMode(0); mode <= os.ModePerm; mode++ { + err := os.Chmod(path, mode) + if err != nil { + return fmt.Errorf("cannot set mode 0%03o on %s: %w", mode, path, err) + } + info, err := os.Stat(path) + if err != nil { + return fmt.Errorf("cannot stat %s: %w", path, err) + } + result := Result{ + Expected: mode, + Actual: info.Mode(), + } + err = emit(result) + if err != nil { + return err + } + } + return nil +} + +func run() error { + var emit func(Result) error + if len(os.Args) < 2 { + emit = emitCSV + } else { + dbPath := os.Args[1] + db, err := NewSQLite(dbPath) + if err != nil { + return err + } + defer db.Close() + emit = db.Emit + } + return getData(emit) +} + +func main() { + err := run() + if err != nil { + fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err) + os.Exit(1) + } +} diff --git a/modes.db b/modes.db new file mode 100644 index 0000000..9ee6054 Binary files /dev/null and b/modes.db differ