| 1 | package projectiondb |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "io" |
| 6 | "os" |
| 7 | "sync" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // integrityCertificates owns reusable audits for unchanged disposable files. |
| 12 | // No connection or file descriptor is retained, so rebuild/rename and Windows |
| 13 | // deletion retain their existing lifecycle. A WAL change revokes the proof even |
| 14 | // before a checkpoint changes the main database. |
| 15 | type integrityCertificates struct { |
| 16 | mu sync.Mutex |
| 17 | entries map[integrityCertificateKey]integrityCertificate |
| 18 | } |
| 19 | |
| 20 | type integrityCertificateKey struct { |
| 21 | path string |
| 22 | quick bool |
| 23 | } |
| 24 | type integrityCertificate struct { |
| 25 | main os.FileInfo |
| 26 | header string |
| 27 | checked time.Time |
| 28 | } |
| 29 | |
| 30 | var reusableIntegrityChecks integrityCertificates |
| 31 | |
| 32 | func integrityFileState(path string) (integrityCertificate, bool) { |
| 33 | file, err := os.Open(path) |
| 34 | if err != nil { |
| 35 | return integrityCertificate{}, false |
| 36 | } |
| 37 | defer file.Close() |
| 38 | main, err := file.Stat() |
| 39 | if err != nil || !main.Mode().IsRegular() { |
| 40 | return integrityCertificate{}, false |
| 41 | } |
| 42 | wal, err := os.Stat(path + "-wal") |
| 43 | if err != nil && !os.IsNotExist(err) { |
| 44 | return integrityCertificate{}, false |
| 45 | } |
| 46 | // A zero-length WAL contains no data. Read-only connections may create |
| 47 | // and remove one without changing any database page. |
| 48 | if wal != nil { |
| 49 | // A writer may reuse WAL frames without changing its length, and some |
| 50 | // filesystems defer mtime publication until the writer closes. Do not |
| 51 | // cache an audit with outstanding WAL data. |
| 52 | if wal.Size() != 0 { |
| 53 | return integrityCertificate{}, false |
| 54 | } |
| 55 | } |
| 56 | var header [100]byte |
| 57 | n, err := file.ReadAt(header[:], 0) |
| 58 | if err != nil && !errors.Is(err, io.EOF) { |
| 59 | return integrityCertificate{}, false |
| 60 | } |
| 61 | // The header adds the schema cookie and, outside WAL mode, the change |
| 62 | // counter. A WAL commit need not bump that counter, so file identity, size |
| 63 | // and mtime stay the primary signal that a checkpoint changed the file. |
| 64 | return integrityCertificate{main: main, header: string(header[:n])}, true |
| 65 | } |
| 66 | |
| 67 | func sameIntegrityFile(a, b os.FileInfo) bool { |
| 68 | if a == nil || b == nil { |
| 69 | return a == nil && b == nil |
| 70 | } |
| 71 | return os.SameFile(a, b) && a.Size() == b.Size() && a.ModTime().Equal(b.ModTime()) |
| 72 | } |
| 73 | |
| 74 | func (a integrityCertificate) same(b integrityCertificate) bool { |
| 75 | return sameIntegrityFile(a.main, b.main) && a.header == b.header |
| 76 | } |
| 77 | |
| 78 | func (c *integrityCertificates) check(path string, quick bool, validate func() error) error { |
| 79 | opened, known := integrityFileState(path) |
| 80 | return c.checkOpened(path, quick, opened, known, validate) |
| 81 | } |
| 82 | |
| 83 | func (c *integrityCertificates) checkOpened(path string, quick bool, opened integrityCertificate, openedKnown bool, validate func() error) error { |
| 84 | key := integrityCertificateKey{path, quick} |
| 85 | before, known := integrityFileState(path) |
| 86 | // A path swapped while SQLite was opening must not certify the new file |
| 87 | // using an audit of the old connection's inode. |
| 88 | known = known && openedKnown && opened.same(before) |
| 89 | c.mu.Lock() |
| 90 | previous, exists := c.entries[key] |
| 91 | if known && exists && before.same(previous) { |
| 92 | c.mu.Unlock() |
| 93 | return nil |
| 94 | } |
| 95 | delete(c.entries, key) |
| 96 | c.mu.Unlock() |
| 97 | // Do not hold a process-wide mutex across disk I/O or SQLite validation. |
| 98 | // Simultaneous first readers may audit independently; neither waits for an |
| 99 | // unrelated session's audit, and only a stable successful audit is retained. |
| 100 | if err := validate(); err != nil { |
| 101 | return err |
| 102 | } |
| 103 | after, stable := integrityFileState(path) |
| 104 | if !known || !stable || !before.same(after) { |
| 105 | return nil |
| 106 | } |
| 107 | after.checked = time.Now() |
| 108 | c.mu.Lock() |
| 109 | defer c.mu.Unlock() |
| 110 | if c.entries == nil { |
| 111 | c.entries = make(map[integrityCertificateKey]integrityCertificate) |
| 112 | } |
| 113 | if len(c.entries) >= 128 { |
| 114 | var oldest integrityCertificateKey |
| 115 | var at time.Time |
| 116 | for candidate, certificate := range c.entries { |
| 117 | if at.IsZero() || certificate.checked.Before(at) { |
| 118 | oldest, at = candidate, certificate.checked |
| 119 | } |
| 120 | } |
| 121 | delete(c.entries, oldest) |
| 122 | } |
| 123 | c.entries[key] = after |
| 124 | return nil |
| 125 | } |
| 126 |