Collect data on dirs
This commit is contained in:
parent
fcb09c1f0a
commit
1a787118d8
26
README.md
26
README.md
@ -5,30 +5,26 @@ This is an experiment to see how `os.Chmod` and `os.Stat` interact on Windows. T
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### `go-perm-test`
|
```
|
||||||
|
go-perm-test <filename>
|
||||||
Prints the results as a CSV in the format
|
```
|
||||||
|
|
||||||
```<expected permissions>,<actual permissions>```
|
|
||||||
|
|
||||||
where each set of permissions is an octal integer.
|
|
||||||
|
|
||||||
### `go-perm-test <filename>`
|
|
||||||
|
|
||||||
Inserts the results into an SQLite database called `filename`.
|
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`.
|
There are four objects in the DB schema: tables named `{file,dir}_result_raw(expected INT, actual INT)` and views named `{file,dir}_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.
|
The `result` views 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
|
## Findings
|
||||||
|
|
||||||
Permissions as observed through `os.Stat` depend entirely upon the owner's write bit set through `os.Chmod`:
|
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;
|
sqlite> select distinct format('%o', actual), expected_uw from dir_result;
|
||||||
actual|expected_uw
|
20000000555|0
|
||||||
292|0
|
20000000777|1
|
||||||
438|1
|
sqlite> select distinct format('%o', actual), expected_uw from file_result;
|
||||||
|
444|0
|
||||||
|
666|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)
|
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)
|
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
@ -36,56 +37,66 @@ func sqlViewColSpec() string {
|
|||||||
return strings.Join(cols, ",\n")
|
return strings.Join(cols, ",\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
const createTable = `CREATE TABLE result_raw (
|
const createTableFile = `CREATE TABLE file_result_raw (
|
||||||
expected INT NOT NULL,
|
expected INT NOT NULL,
|
||||||
actual INT NOT NULL
|
actual INT NOT NULL
|
||||||
);`
|
);`
|
||||||
|
|
||||||
var createView string = `CREATE VIEW result AS
|
var createViewFile string = `CREATE VIEW file_result AS
|
||||||
SELECT
|
SELECT
|
||||||
` + sqlViewColSpec() + `
|
` + sqlViewColSpec() + `
|
||||||
FROM result_raw;`
|
FROM file_result_raw;`
|
||||||
|
|
||||||
|
var createTableDir = strings.ReplaceAll(createTableFile, "file_result", "dir_result")
|
||||||
|
var createViewDir = strings.ReplaceAll(createViewFile, "file_result", "dir_result")
|
||||||
|
|
||||||
func NewSQLite(path string) (*SQLiteEmitter, error) {
|
func NewSQLite(path string) (*SQLiteEmitter, error) {
|
||||||
conn, err := sqlite.OpenConn(path, sqlite.OpenCreate|sqlite.OpenReadWrite|sqlite.OpenWAL)
|
conn, err := sqlite.OpenConn(path, sqlite.OpenCreate|sqlite.OpenReadWrite|sqlite.OpenWAL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fmt.Println(createView)
|
script := strings.Join([]string{createTableFile, createTableDir, createViewFile, createViewDir}, "\n")
|
||||||
err = sqlitex.Execute(conn, createTable, &sqlitex.ExecOptions{})
|
fmt.Println(script)
|
||||||
|
err = sqlitex.ExecuteScript(conn, script, &sqlitex.ExecOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("NewSQLite: cannot create table: %w", err)
|
return nil, fmt.Errorf("NewSQLite: cannot create schema: %w", err)
|
||||||
}
|
|
||||||
err = sqlitex.Execute(conn, createView, &sqlitex.ExecOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("NewSQLite: cannot create view: %w", err)
|
|
||||||
}
|
}
|
||||||
|
// err = sqlitex.Execute(conn, createTableFile, &sqlitex.ExecOptions{})
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("NewSQLite: cannot create table: %w", err)
|
||||||
|
// }
|
||||||
|
// err = sqlitex.Execute(conn, createViewFile, &sqlitex.ExecOptions{})
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("NewSQLite: cannot create view: %w", err)
|
||||||
|
// }
|
||||||
return &SQLiteEmitter{
|
return &SQLiteEmitter{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteEmitter) Emit(r Result) error {
|
func (s *SQLiteEmitter) Emit(tablePrefix string) func(r Result) error {
|
||||||
return sqlitex.Execute(s.conn, "INSERT INTO result_raw(expected, actual) VALUES (?, ?)", &sqlitex.ExecOptions{
|
query := fmt.Sprintf("INSERT INTO %sresult_raw(expected, actual) VALUES (?, ?)", tablePrefix)
|
||||||
|
return func(r Result) error {
|
||||||
|
return sqlitex.Execute(s.conn, query, &sqlitex.ExecOptions{
|
||||||
Args: []any{r.Expected, r.Actual},
|
Args: []any{r.Expected, r.Actual},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func emitCSV(r Result) error {
|
func createFile(dir, pattern string) (path string, err error) {
|
||||||
_, err := fmt.Printf("%03o,%03o\n", r.Expected, r.Actual)
|
f, err := os.CreateTemp(dir, pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("emitCSV: %w", err)
|
return "", err
|
||||||
}
|
}
|
||||||
return nil
|
f.Close()
|
||||||
|
return f.Name(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getData(emit func(Result) error) (err error) {
|
func getData(create func(string, string) (string, error), emit func(Result) error) (err error) {
|
||||||
f, err := os.CreateTemp("", "go-perm-test-*")
|
path, err := create("", "go-perm-test-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
path := f.Name()
|
|
||||||
f.Close()
|
|
||||||
|
|
||||||
for mode := fs.FileMode(0); mode <= os.ModePerm; mode++ {
|
for mode := fs.FileMode(0); mode <= os.ModePerm; mode++ {
|
||||||
err := os.Chmod(path, mode)
|
err := os.Chmod(path, mode)
|
||||||
@ -109,19 +120,18 @@ func getData(emit func(Result) error) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
var emit func(Result) error
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
emit = emitCSV
|
fmt.Fprintf(os.Stderr, "usage: %s <filename>", os.Args[0])
|
||||||
} else {
|
}
|
||||||
|
|
||||||
dbPath := os.Args[1]
|
dbPath := os.Args[1]
|
||||||
db, err := NewSQLite(dbPath)
|
db, err := NewSQLite(dbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
emit = db.Emit
|
|
||||||
}
|
return errors.Join(getData(createFile, db.Emit("file_")), getData(os.MkdirTemp, db.Emit("dir_")))
|
||||||
return getData(emit)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user