package cachedb import ( `os` `path/filepath` `github.com/jmoiron/sqlx` _ `github.com/mattn/go-sqlite3` `r00t2.io/gobroke/conf` `r00t2.io/sysutils/paths` ) /* NewCache returns a Cache from path to SQLite file `db` (or ":memory:" for an in-memory one). It will be created if it doesn't exist for persistent caches. */ func NewCache(db string, perms *conf.Perms) (c *Cache, err error) { var cache Cache var exists bool switch db { case ":memory:": // NO-OP for now; exists should be false, but it is since it's zero-val. default: if perms == nil { perms = new(conf.Perms) if err = perms.SetMissing(); err != nil { return } } if exists, err = paths.RealPathExists(&db); err != nil { return } if !exists { if err = os.MkdirAll(filepath.Dir(db), *perms.ParentDir.Mode); err != nil { return } if err = os.WriteFile(db, nil, *perms.File.Mode); err != nil { return } } if err = perms.Chown(filepath.Dir(db)); err != nil { return } if err = perms.Chmod(filepath.Dir(db), !exists); err != nil { return } if err = perms.Chown(db); err != nil { return } if err = perms.Chmod(db, !exists); err != nil { return } } if cache.db, err = sqlx.Connect("sqlite3", db); err != nil { return } if !exists { // New DB, so write the schema. if _, err = cache.db.Exec(string(schemaBytes)); err != nil { return } } c = &cache return }