From fcb09c1f0a1a4d3e6edc8d8d8ae7fca38a3445f6 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 19 Aug 2024 16:30:30 -0600 Subject: [PATCH] First commit --- README.md | 34 +++++++++++++ go.mod | 17 +++++++ go.sum | 23 +++++++++ main_windows.go | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ modes.db | Bin 0 -> 16384 bytes 5 files changed, 207 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main_windows.go create mode 100644 modes.db 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 0000000000000000000000000000000000000000..9ee60547f65f38aafa707b6237bb7e9da0d7fd78 GIT binary patch literal 16384 zcmeI2dze*K{fF0@bI#i5>?;=$x#{74zYTInU}hK;7(niVBFw;m3?MKJ7ePSI(p0QW z3`^8Byd`BSmL_H?Y8Iv@W-4YXY9?wXrY2_geGhYFMkas%{^~hA&+vH^NGmzLM0`>95%pg@gGrbAR*17-i6iNo??CMGtx(XC0#Vdol;s>S~AtGT)Vt-UQOlv9$!$pQ@YE*^sr&+KIv}h zeqGbUho`&LV$qaz?)o{)>aXbg#fo(Ibe}q_j(n`Qa_v_v?OT7Te5|*0%~vcftiMz~ z)>~R#e`$7$eUEOj7V9oqSbs%!i`mPLZn4hCdMg{+V(!}7E!O#1Z)rnY%w1c%#X2AB zEp2Fvg-5qoi*=W*YG8}m%Z_fb&c}Ky8`@&-+S)DF`B-mhLtD&UTf4^$2nG|NW@uR>>Cs!?Fe+)7&!I-hWuu;3Jw__Qlq|*i!cI`N&gU zi*he&FPDe&OTS6(si^(Gug=FhtB&1L?z#pa%iM2v?3QxN8hj*k%Z}Yr_V4w`mTK|n zA5I>|FKsFJ6x431&c`~dj@?r3x&|M^+C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