Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
392 lines
11 KiB
Go
392 lines
11 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var (
|
|
ErrPathNotFound = errors.New("semantic path does not exist")
|
|
ErrPathConflict = errors.New("destination semantic path already exists")
|
|
)
|
|
|
|
type pathRename struct {
|
|
OldPath string
|
|
NewPath string
|
|
ObjectUUID string
|
|
CreatedAt int64
|
|
IsPrimary int
|
|
}
|
|
|
|
func (s *Store) RenamePath(oldPath, newPath string) error {
|
|
domainUUID, err := s.defaultDomainUUID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = s.RenamePathsInDomain(domainUUID, oldPath, newPath, false)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) RenamePathInDomain(domainUUID, oldPath, newPath string) error {
|
|
_, err := s.RenamePathsInDomain(domainUUID, oldPath, newPath, false)
|
|
return err
|
|
}
|
|
|
|
// RenamePathsInDomain performs rename as an atomic link/unlink operation.
|
|
// Page identity and Revision history remain stable while Path reachability
|
|
// changes. When a file-like basename changes, a new immutable Revision records
|
|
// the corresponding filename metadata.
|
|
func (s *Store) RenamePathsInDomain(
|
|
domainUUID, oldPath, newPath string,
|
|
recursive bool,
|
|
) (int, error) {
|
|
return s.renamePathsInDomain(domainUUID, oldPath, newPath, recursive, nil)
|
|
}
|
|
|
|
// RenamePathsInDomainChecked requires the exact store snapshot authorized by
|
|
// the caller. Trusted local tools can use RenamePathsInDomain without grants.
|
|
func (s *Store) RenamePathsInDomainChecked(domainUUID, oldPath, newPath string, recursive bool, snapshot *PathMutationSnapshot) (int, error) {
|
|
if snapshot == nil {
|
|
return 0, ErrPathSnapshotChanged
|
|
}
|
|
count, err := s.renamePathsInDomain(domainUUID, oldPath, newPath, recursive, snapshot)
|
|
return count, pathMutationTransactionError(err)
|
|
}
|
|
|
|
func (s *Store) renamePathsInDomain(domainUUID, oldPath, newPath string, recursive bool, snapshot *PathMutationSnapshot) (int, error) {
|
|
if err := s.ensureLocalDomain(domainUUID); err != nil {
|
|
return 0, err
|
|
}
|
|
oldPath = normalizePath(oldPath)
|
|
newPath = normalizePath(newPath)
|
|
if oldPath == "/" || newPath == "/" || oldPath == newPath {
|
|
return 0, ErrPathConflict
|
|
}
|
|
if recursive && strings.HasPrefix(newPath+"/", oldPath+"/") {
|
|
return 0, ErrPathConflict
|
|
}
|
|
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
renames, err := listMutationPaths(tx, domainUUID, oldPath, recursive)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if snapshot != nil {
|
|
if err := snapshot.validate(domainUUID, oldPath, recursive, renames); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if len(renames) == 0 {
|
|
return 0, ErrPathNotFound
|
|
}
|
|
for i := range renames {
|
|
renames[i].NewPath = newPath + strings.TrimPrefix(renames[i].OldPath, oldPath)
|
|
}
|
|
|
|
sourcePaths := make(map[string]bool, len(renames))
|
|
for _, item := range renames {
|
|
sourcePaths[item.OldPath] = true
|
|
}
|
|
for _, item := range renames {
|
|
var exists int
|
|
if err := tx.QueryRow(`
|
|
SELECT COUNT(*) FROM paths
|
|
WHERE domain_uuid = ? AND path = ?
|
|
`, domainUUID, item.NewPath).Scan(&exists); err != nil {
|
|
return 0, err
|
|
}
|
|
if exists > 0 && !sourcePaths[item.NewPath] {
|
|
return 0, ErrPathConflict
|
|
}
|
|
}
|
|
|
|
// Link all destinations before unlinking sources. Temporary source rows
|
|
// remain valid until the transaction commits.
|
|
for _, item := range renames {
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO paths (
|
|
domain_uuid, path, object_uuid, created_at, is_primary
|
|
) VALUES (?, ?, ?, ?, ?)
|
|
`, domainUUID, item.NewPath, item.ObjectUUID, item.CreatedAt, item.IsPrimary); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if err := renameUserSpacePathPoliciesTx(tx, domainUUID, oldPath, newPath, recursive); err != nil {
|
|
return 0, err
|
|
}
|
|
for _, item := range renames {
|
|
if _, err := tx.Exec(`
|
|
DELETE FROM paths WHERE domain_uuid = ? AND path = ?
|
|
`, domainUUID, item.OldPath); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if err := renameBrowserNavigationPathsTx(tx, domainUUID, oldPath, newPath, recursive); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
renamedObjects := map[string][2]string{}
|
|
for _, item := range renames {
|
|
oldName := filepath.Base(item.OldPath)
|
|
newName := filepath.Base(item.NewPath)
|
|
if oldName != newName && filepath.Ext(oldName) != "" {
|
|
renamedObjects[item.ObjectUUID] = [2]string{oldName, newName}
|
|
}
|
|
}
|
|
for objectUUID, names := range renamedObjects {
|
|
if err := cloneLatestRevisionFilenames(
|
|
tx,
|
|
objectUUID,
|
|
names[0],
|
|
names[1],
|
|
); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if recursive {
|
|
changed, err := renameCityPathPrefixesTx(tx, domainUUID, oldPath, newPath)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if changed > 0 {
|
|
if err := bumpTopologyVersion(tx, domainUUID, "system:path-rename", "city.paths.renamed", "path", oldPath); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
}
|
|
operationID := uuid.NewString()
|
|
payload, err := json.Marshal(map[string]any{
|
|
"operation_uuid": operationID, "old_path": oldPath, "new_path": newPath,
|
|
"recursive": recursive, "count": len(renames),
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if err := s.appendActivityOutboxIfAvailable(tx, ActivityOutboxEvent{
|
|
DomainUUID: domainUUID, EventType: "path.renamed", TargetKind: "path",
|
|
Payload: string(payload), IdempotencyKey: "path.renamed:" + operationID,
|
|
}); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
return 0, err
|
|
}
|
|
s.InvalidatePathCaches()
|
|
return len(renames), nil
|
|
}
|
|
|
|
func cloneLatestRevisionFilenames(
|
|
tx *sql.Tx,
|
|
objectUUID, oldName, newName string,
|
|
) error {
|
|
rows, err := tx.Query(`
|
|
WITH ranked AS (
|
|
SELECT
|
|
uuid, object_uuid, body, document_hash, lang, content_type,
|
|
filename, message, created_at_ms, created_at,
|
|
ROW_NUMBER() OVER (
|
|
PARTITION BY COALESCE(NULLIF(lang, ''), 'und')
|
|
ORDER BY COALESCE(created_at_ms, created_at * 1000) DESC, rowid DESC
|
|
) AS rn
|
|
FROM revisions
|
|
WHERE object_uuid = ?
|
|
)
|
|
SELECT
|
|
COALESCE(body, ''), COALESCE(document_hash, ''),
|
|
COALESCE(NULLIF(lang, ''), 'und'),
|
|
COALESCE(NULLIF(content_type, ''), 'application/octet-stream'),
|
|
COALESCE(filename, ''), COALESCE(message, '')
|
|
FROM ranked WHERE rn = 1
|
|
`, objectUUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
type latestRevision struct {
|
|
body, hash, lang, contentType, filename, message string
|
|
}
|
|
var revisions []latestRevision
|
|
for rows.Next() {
|
|
var revision latestRevision
|
|
if err := rows.Scan(
|
|
&revision.body,
|
|
&revision.hash,
|
|
&revision.lang,
|
|
&revision.contentType,
|
|
&revision.filename,
|
|
&revision.message,
|
|
); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
if revision.filename == "" || revision.filename == oldName {
|
|
revisions = append(revisions, revision)
|
|
}
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
if err := rows.Close(); err != nil {
|
|
return err
|
|
}
|
|
now := time.Now()
|
|
for index, revision := range revisions {
|
|
createdAtMS := now.UnixMilli() + int64(index)
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO revisions (
|
|
uuid, object_uuid, body, document_hash, lang, content_type,
|
|
filename, message, created_at_ms, created_at
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
`,
|
|
uuid.NewString(),
|
|
objectUUID,
|
|
revision.body,
|
|
revision.hash,
|
|
revision.lang,
|
|
revision.contentType,
|
|
newName,
|
|
"rename filename: "+oldName+" -> "+newName,
|
|
createdAtMS,
|
|
createdAtMS/1000,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) RemovePath(path string) error {
|
|
domainUUID, err := s.defaultDomainUUID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.RemovePathInDomain(domainUUID, path)
|
|
}
|
|
|
|
func (s *Store) RemovePathInDomain(domainUUID, path string) error {
|
|
if err := s.ensureLocalDomain(domainUUID); err != nil {
|
|
return err
|
|
}
|
|
_, err := s.DB.Exec(`
|
|
DELETE FROM paths WHERE domain_uuid = ? AND path = ?
|
|
`, domainUUID, normalizePath(path))
|
|
if err == nil {
|
|
s.InvalidatePathCaches()
|
|
}
|
|
return err
|
|
}
|
|
|
|
// CopyPathsInDomain duplicates a Page or a virtual-folder subtree. Document
|
|
// blobs remain content-addressed and are reused, while Object and Revision
|
|
// identities are newly allocated so later edits do not affect the source.
|
|
func (s *Store) CopyPathsInDomain(
|
|
domainUUID, sourcePath, destinationPath string,
|
|
recursive bool,
|
|
) (int, error) {
|
|
return s.copyPathsInDomain(domainUUID, sourcePath, destinationPath, recursive, nil)
|
|
}
|
|
|
|
func (s *Store) CopyPathsInDomainChecked(domainUUID, sourcePath, destinationPath string, recursive bool, snapshot *PathMutationSnapshot) (int, error) {
|
|
if snapshot == nil {
|
|
return 0, ErrPathSnapshotChanged
|
|
}
|
|
count, err := s.copyPathsInDomain(domainUUID, sourcePath, destinationPath, recursive, snapshot)
|
|
return count, pathMutationTransactionError(err)
|
|
}
|
|
|
|
func (s *Store) copyPathsInDomain(domainUUID, sourcePath, destinationPath string, recursive bool, snapshot *PathMutationSnapshot) (int, error) {
|
|
if err := s.ensureLocalDomain(domainUUID); err != nil {
|
|
return 0, err
|
|
}
|
|
sourcePath = normalizePath(sourcePath)
|
|
destinationPath = normalizePath(destinationPath)
|
|
if sourcePath == "/" || destinationPath == "/" || sourcePath == destinationPath {
|
|
return 0, ErrPathConflict
|
|
}
|
|
if recursive && strings.HasPrefix(destinationPath+"/", sourcePath+"/") {
|
|
return 0, ErrPathConflict
|
|
}
|
|
tx, err := s.DB.Begin()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer tx.Rollback()
|
|
paths, err := listMutationPaths(tx, domainUUID, sourcePath, recursive)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if snapshot != nil {
|
|
if err := snapshot.validate(domainUUID, sourcePath, recursive, paths); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if len(paths) == 0 {
|
|
return 0, ErrPathNotFound
|
|
}
|
|
for i := range paths {
|
|
paths[i].NewPath = destinationPath + strings.TrimPrefix(paths[i].OldPath, sourcePath)
|
|
}
|
|
for _, item := range paths {
|
|
var exists int
|
|
if err := tx.QueryRow(`SELECT COUNT(*) FROM paths WHERE domain_uuid=? AND path=?`,
|
|
domainUUID, item.NewPath).Scan(&exists); err != nil {
|
|
return 0, err
|
|
}
|
|
if exists != 0 {
|
|
return 0, ErrPathConflict
|
|
}
|
|
}
|
|
objectCopies := map[string]string{}
|
|
for _, item := range paths {
|
|
newObjectUUID := objectCopies[item.ObjectUUID]
|
|
if newObjectUUID == "" {
|
|
newObjectUUID = uuid.NewString()
|
|
objectCopies[item.ObjectUUID] = newObjectUUID
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO objects (uuid, kind, created_at)
|
|
SELECT ?, kind, ? FROM objects WHERE uuid=?`,
|
|
newObjectUUID, time.Now().Unix(), item.ObjectUUID); err != nil {
|
|
return 0, err
|
|
}
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO revisions (
|
|
uuid, object_uuid, body, document_hash, lang, content_type,
|
|
filename, message, created_at_ms, created_at
|
|
)
|
|
SELECT lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' ||
|
|
substr(lower(hex(randomblob(2))),2) || '-' ||
|
|
substr('89ab',abs(random()) % 4 + 1,1) ||
|
|
substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6))),
|
|
?, body, document_hash, lang, content_type, filename,
|
|
message, created_at_ms, created_at
|
|
FROM revisions WHERE object_uuid=?`,
|
|
newObjectUUID, item.ObjectUUID); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if _, err := tx.Exec(`
|
|
INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary)
|
|
VALUES(?,?,?,?,?)`,
|
|
domainUUID, item.NewPath, newObjectUUID, time.Now().Unix(), item.IsPrimary); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return 0, err
|
|
}
|
|
s.InvalidatePathCaches()
|
|
return len(paths), nil
|
|
}
|