s
This commit is contained in:
+11
-7
@@ -27,6 +27,7 @@ import (
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"simplegit/common"
|
||||
@@ -76,7 +77,7 @@ var (
|
||||
|
||||
type LocalState struct {
|
||||
engine *xorm.Engine
|
||||
root string
|
||||
cfg *common.Config
|
||||
// skipAuth bypasses the DB: AccessBy* always allow, RepoPath resolves any
|
||||
// path under root. engine is nil in this mode.
|
||||
skipAuth bool
|
||||
@@ -92,17 +93,19 @@ func init() {
|
||||
sql.Register(sqliteDriver, &sqlite.Driver{})
|
||||
}
|
||||
|
||||
func OpenWithDSN(root string, driver string, dsn string) (*LocalState, error) {
|
||||
engine, err := InitEngine(driver, dsn)
|
||||
func Open(cfg *common.Config) (*LocalState, error) {
|
||||
engine, err := InitEngine(cfg.Driver(), cfg.DSN())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("state: init engine: %w", err)
|
||||
}
|
||||
if driver == sqliteDriver {
|
||||
if cfg.Driver() == sqliteDriver {
|
||||
engine.SetMaxOpenConns(1)
|
||||
}
|
||||
log.Printf("state: RepoRoot: %s", cfg.RepoRoot())
|
||||
log.Printf("state: DB engine %s opened, %s", cfg.Driver(), cfg.DSN())
|
||||
return &LocalState{
|
||||
engine: engine,
|
||||
root: root,
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -164,7 +167,7 @@ func (s *LocalState) loadOrCreateHostKey(ty string) (ssh.Signer, error) {
|
||||
default:
|
||||
return nil, errors.New("loadOrCreateHostKey")
|
||||
}
|
||||
hostKeyPath := filepath.Join(s.root, fmt.Sprintf("host_key_%v.pem", ty))
|
||||
hostKeyPath := filepath.Join(s.cfg.Root, fmt.Sprintf("host_key_%v.pem", ty))
|
||||
if data, err := os.ReadFile(hostKeyPath); err == nil {
|
||||
return ssh.ParsePrivateKey(data)
|
||||
} else if !os.IsNotExist(err) {
|
||||
@@ -214,13 +217,14 @@ func (s *LocalState) findRepo(owner, name string) (*Repo, error) {
|
||||
// repo is registered in the DB -- the DB is the authority, not the filesystem.
|
||||
// In skip-auth mode the DB check is skipped (any path under root resolves).
|
||||
func (s *LocalState) RepoPath(owner, name string) (string, error) {
|
||||
name = normalizeName(name)
|
||||
rel := relPath(owner, name)
|
||||
if !s.skipAuth {
|
||||
if _, err := s.findRepo(owner, name); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return common.Resolve(s.root, rel)
|
||||
return common.Resolve(s.cfg.RepoRoot(), rel)
|
||||
}
|
||||
|
||||
// Open returns a gitcmd.Repository for owner/name. The repo must be registered
|
||||
|
||||
+6
-7
@@ -46,11 +46,11 @@ func (s *LocalState) nsDir(ns string) (string, error) {
|
||||
if ns == "" || strings.Contains(ns, "..") {
|
||||
return "", fmt.Errorf("invalid namespace %q", ns)
|
||||
}
|
||||
abs, err := filepath.Abs(filepath.Join(s.root, ns))
|
||||
abs, err := filepath.Abs(filepath.Join(s.cfg.RepoRoot(), ns))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rootAbs, err := filepath.Abs(s.root)
|
||||
rootAbs, err := filepath.Abs(s.cfg.RepoRoot())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -106,8 +106,7 @@ func (s *LocalState) nextNamespaceID() (int64, error) {
|
||||
// namespace must already exist (have at least one repo) so NamespaceID can be
|
||||
// resolved; NameID is freshly assigned. skip-auth: disk-only (git init, no DB).
|
||||
func (s *LocalState) CreateRepo(ns, name string) error {
|
||||
name = normalizeName(name)
|
||||
path, err := common.Resolve(s.root, relPath(ns, name))
|
||||
path, err := s.RepoPath(ns, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -164,7 +163,7 @@ func (s *LocalState) ExistRepo(ns, name string) error {
|
||||
// DeleteRepo removes the repo row, its repo-targeted ACL grants, and the
|
||||
// on-disk bare directory. Hard-deletes. skip-auth: disk-only.
|
||||
func (s *LocalState) DeleteRepo(ns, name string) error {
|
||||
path, _ := common.Resolve(s.root, relPath(ns, name))
|
||||
path, _ := s.RepoPath(ns, name)
|
||||
if s.skipAuth {
|
||||
if path != "" {
|
||||
_ = os.RemoveAll(path)
|
||||
@@ -199,11 +198,11 @@ func (s *LocalState) MoveRepo(old, new string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oldPath, err := common.Resolve(s.root, relPath(oldNs, oldName))
|
||||
oldPath, err := s.RepoPath(oldNs, oldName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newPath, err := common.Resolve(s.root, relPath(newNs, newName))
|
||||
newPath, err := s.RepoPath(newNs, newName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user