pikuri-thunderbird 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -8
- data/design/architecture.md +827 -0
- data/lib/pikuri/thunderbird/calendar.rb +216 -70
- data/lib/pikuri/thunderbird/calendar_create.rb +16 -15
- data/lib/pikuri/thunderbird/calendar_read.rb +8 -3
- data/lib/pikuri/thunderbird/calendar_search.rb +16 -7
- data/lib/pikuri/thunderbird/compose_guard.rb +16 -11
- data/lib/pikuri/thunderbird/contact_search.rb +13 -3
- data/lib/pikuri/thunderbird/database_snapshot.rb +144 -78
- data/lib/pikuri/thunderbird/extension.rb +167 -55
- data/lib/pikuri/thunderbird/forwarded_mail.rb +191 -0
- data/lib/pikuri/thunderbird/gloda/attachment.rb +36 -0
- data/lib/pikuri/thunderbird/gloda/contacts.rb +53 -35
- data/lib/pikuri/thunderbird/gloda/mail.rb +75 -19
- data/lib/pikuri/thunderbird/gloda.rb +35 -15
- data/lib/pikuri/thunderbird/ical_line.rb +3 -3
- data/lib/pikuri/thunderbird/launcher.rb +10 -5
- data/lib/pikuri/thunderbird/mail_attachment_read.rb +282 -0
- data/lib/pikuri/thunderbird/mail_compose.rb +16 -16
- data/lib/pikuri/thunderbird/mail_read.rb +94 -15
- data/lib/pikuri/thunderbird/mail_search.rb +15 -6
- data/lib/pikuri/thunderbird/message_store.rb +203 -0
- data/lib/pikuri/thunderbird/profile.rb +64 -19
- data/lib/pikuri/thunderbird/recurrence.rb +207 -0
- data/lib/pikuri-thunderbird.rb +5 -5
- metadata +60 -13
- data/DESIGN.md +0 -460
|
@@ -53,14 +53,24 @@ module Pikuri
|
|
|
53
53
|
)
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
-
# @return [
|
|
56
|
+
# @return [Pikuri::Tool::Result] the ranked candidates (+"Found 3
|
|
57
|
+
# contacts"+), the no-match line (+"Found 0 contacts"+), or
|
|
58
|
+
# {Pikuri::Tool::Result.error} while Thunderbird is too busy to read.
|
|
57
59
|
def self.run_search(backend:, query:, limit:)
|
|
58
60
|
limit = limit.to_i.clamp(1, MAX_LIMIT)
|
|
59
61
|
candidates = backend.resolve(query:, limit:)
|
|
60
|
-
|
|
62
|
+
if candidates.empty?
|
|
63
|
+
return Pikuri::Tool::Result.new(content: "No contacts found matching #{query.inspect}.",
|
|
64
|
+
summary: 'Found 0 contacts')
|
|
65
|
+
end
|
|
61
66
|
|
|
62
67
|
header = "#{candidates.size} contact#{candidates.size == 1 ? '' : 's'} (best first):"
|
|
63
|
-
|
|
68
|
+
Pikuri::Tool::Result.new(
|
|
69
|
+
content: [header, *candidates.map { |c| render(c) }].join("\n\n"),
|
|
70
|
+
summary: "Found #{Pikuri::Tool.pluralize(candidates.size, 'contact')}"
|
|
71
|
+
)
|
|
72
|
+
rescue DatabaseSnapshot::TornError => e
|
|
73
|
+
Pikuri::Tool::Result.error(e.message)
|
|
64
74
|
end
|
|
65
75
|
|
|
66
76
|
# @param c [Hash] one candidate from {Gloda::Contacts#resolve}.
|
|
@@ -11,66 +11,113 @@ module Pikuri
|
|
|
11
11
|
# +cp --reflink=auto+; every actual query runs against the copy we own.
|
|
12
12
|
#
|
|
13
13
|
# snap = DatabaseSnapshot.new(source: profile.gloda_path, dir: cache_dir)
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
14
|
+
# unless db && snap.current?
|
|
15
|
+
# if snap.refresh || db.nil? # false: unchanged, or Thunderbird busy → keep the older copy
|
|
16
|
+
# db&.close # the older copy; the next refresh overwrites it
|
|
17
|
+
# db = SQLite3::Database.new(snap.path)
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# Open the copy normally, never +?immutable=1+: for a WAL store the copy
|
|
22
|
+
# is the main file *plus* its +-wal+, and only a normal open applies the
|
|
23
|
+
# WAL. Opening read-only is fine; SQLite builds the +-shm+ in our dir.
|
|
17
24
|
#
|
|
18
25
|
# == Why bracketed
|
|
19
26
|
#
|
|
20
27
|
# A reflink clone is atomic w.r.t. concurrent writes, but *file
|
|
21
28
|
# atomicity ≠ DB consistency*: a clone taken mid-transaction faithfully
|
|
22
|
-
# photographs a torn DB. So each copy is bracketed — read the
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
29
|
+
# photographs a torn DB. So each copy is bracketed — read the source
|
|
30
|
+
# {.generation} and check +-journal+ absence *before and after* the copy;
|
|
31
|
+
# a copy counts as settled only when the generation didn't move and no
|
|
32
|
+
# rollback journal was present at either edge. Otherwise the copy is
|
|
33
|
+
# discarded and retried after a backoff.
|
|
34
|
+
#
|
|
35
|
+
# When the tries run out, the older settled copy keeps serving, so a
|
|
36
|
+
# query during a big sync answers one poll stale. Only with no older copy
|
|
37
|
+
# does {#refresh} raise {TornError}, whose message is the model-facing
|
|
38
|
+
# "Thunderbird is busy" text. Gloda holds its +-journal+ about 11 ms per
|
|
39
|
+
# new message (4 s for 400), so no budget outlasts every poll, and a
|
|
40
|
+
# crash leaves a hot +-journal+ until Thunderbird next starts.
|
|
27
41
|
#
|
|
28
|
-
# The
|
|
29
|
-
#
|
|
30
|
-
# source against {#generation}, and {#refresh} re-copies only when they
|
|
42
|
+
# The generation doubles as the freshness stamp: {#current?} compares the
|
|
43
|
+
# live one against the copy's, and {#refresh} re-copies only when they
|
|
31
44
|
# differ. One signal drives both consistency and freshness.
|
|
32
45
|
#
|
|
33
46
|
# == Implementation details
|
|
34
47
|
#
|
|
48
|
+
# The generation is the header change counter *plus* the WAL's salt and
|
|
49
|
+
# size. In WAL mode SQLite bumps the counter only on a checkpoint, and
|
|
50
|
+
# Thunderbird can leave a few new rows in the +-wal+ until it quits, so
|
|
51
|
+
# the counter alone would call a stale copy current.
|
|
52
|
+
#
|
|
53
|
+
# The WAL is copied *after* the main file. A checkpoint between the two
|
|
54
|
+
# leaves frames already in main, and re-applying them is idempotent; a
|
|
55
|
+
# torn WAL tail fails its frame checksum, so SQLite stops at the last
|
|
56
|
+
# whole commit. The one unsafe case — a WAL restart between the copies,
|
|
57
|
+
# new frames over the old main — changes the salt, which the bracket
|
|
58
|
+
# catches. The target slot's +-wal+/+-shm+ are deleted before each copy:
|
|
59
|
+
# a stale WAL would be replayed over the new main.
|
|
60
|
+
#
|
|
61
|
+
# Copies alternate between two slots, +a/+ and +b/+, so a failed attempt
|
|
62
|
+
# never overwrites the copy a caller still reads. That is also why the
|
|
63
|
+
# caller must close its connection after each successful {#refresh}: the
|
|
64
|
+
# next one copies into the slot that connection holds.
|
|
65
|
+
#
|
|
35
66
|
# On-disk rather than an +:memory:+ load: the page cache keeps idle RAM ≈ 0
|
|
36
67
|
# for a multi-hundred-MB Gloda, which a memory copy would pin for the life
|
|
37
|
-
# of the process.
|
|
68
|
+
# of the process. Nor is there an +:memory:+ knob for a host that must
|
|
69
|
+
# leave nothing on disk: the source already sits in the same profile on
|
|
70
|
+
# the same filesystem, so the copy exposes nothing the profile doesn't.
|
|
38
71
|
#
|
|
39
72
|
# The copy lands in a caller-owned 0700 dir on the *same filesystem* as
|
|
40
73
|
# the source (so reflink's copy-on-write extent share is available;
|
|
41
74
|
# +--reflink=auto+ degrades to a full copy elsewhere). It is
|
|
42
75
|
# {Pikuri::Finalizers}-reaped, so a normal exit removes it; a crash can
|
|
43
|
-
# strand it (re-derivable data).
|
|
44
|
-
# WAL store that yields an as-of-last-checkpoint view (Thunderbird
|
|
45
|
-
# checkpoints aggressively), which callers open with +?immutable=1+.
|
|
76
|
+
# strand it (re-derivable data).
|
|
46
77
|
class DatabaseSnapshot
|
|
47
78
|
LOGGER = Pikuri.logger_for('Thunderbird::DatabaseSnapshot')
|
|
48
79
|
|
|
49
|
-
# Raised when
|
|
50
|
-
#
|
|
51
|
-
#
|
|
80
|
+
# Raised when no settled copy can be taken and there is no older one to
|
|
81
|
+
# serve. Its message is written for the model; a tool answers it as
|
|
82
|
+
# +Tool::Result.error(e.message)+.
|
|
52
83
|
class TornError < StandardError; end
|
|
53
84
|
|
|
54
|
-
# @return [String]
|
|
55
|
-
|
|
85
|
+
# @return [String] what {TornError} says.
|
|
86
|
+
BUSY = 'Thunderbird is busy writing to its database; please wait for Thunderbird to settle down ' \
|
|
87
|
+
'and try again, or restart Thunderbird if this is taking too much time.'
|
|
56
88
|
|
|
57
|
-
# @return [
|
|
58
|
-
#
|
|
59
|
-
attr_reader :
|
|
89
|
+
# @return [String] absolute path of the current snapshot copy. It moves
|
|
90
|
+
# to the other slot on each successful {#refresh}.
|
|
91
|
+
attr_reader :path
|
|
60
92
|
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
#
|
|
64
|
-
#
|
|
93
|
+
# The source's change stamp: any commit moves it, a read never does.
|
|
94
|
+
#
|
|
95
|
+
# DatabaseSnapshot.generation('cache.sqlite')
|
|
96
|
+
# # => [412, "\xC2$\x93\x16ig\x84\xA0", 2_400_312] # WAL store
|
|
97
|
+
# # => [87, nil, nil] # no -wal
|
|
65
98
|
#
|
|
66
99
|
# @param db_path [String]
|
|
67
|
-
# @return [Integer, nil]
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
100
|
+
# @return [Array(Integer, String, Integer), nil] the header change
|
|
101
|
+
# counter (bytes 24–27), the +-wal+ salt (its header bytes 16–23) and
|
|
102
|
+
# the +-wal+ size; each WAL part is +nil+ without a +-wal+. +nil+
|
|
103
|
+
# overall when the file has no header yet.
|
|
104
|
+
def self.generation(db_path)
|
|
105
|
+
counter = read_bytes(db_path, 24, 4)&.unpack1('N')
|
|
106
|
+
return nil unless counter
|
|
107
|
+
|
|
108
|
+
wal = "#{db_path}-wal"
|
|
109
|
+
[counter, read_bytes(wal, 16, 8), File.size?(wal)].freeze
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# @return [String, nil] exactly +length+ bytes, or +nil+ if the file is
|
|
113
|
+
# missing or too short.
|
|
114
|
+
def self.read_bytes(path, offset, length)
|
|
115
|
+
bytes = File.binread(path, length, offset)
|
|
116
|
+
bytes if bytes&.bytesize == length
|
|
71
117
|
rescue Errno::ENOENT, EOFError
|
|
72
118
|
nil
|
|
73
119
|
end
|
|
120
|
+
private_class_method :read_bytes
|
|
74
121
|
|
|
75
122
|
# @param source [String] path to the live Thunderbird DB.
|
|
76
123
|
# @param dir [String] private dir to hold the copy (created 0700 if
|
|
@@ -79,76 +126,95 @@ module Pikuri
|
|
|
79
126
|
def initialize(source:, dir:)
|
|
80
127
|
@source = source
|
|
81
128
|
@dir = dir
|
|
82
|
-
@path =
|
|
129
|
+
@path = slot_path('a')
|
|
83
130
|
@generation = nil
|
|
84
131
|
FileUtils.mkdir_p(@dir, mode: 0o700)
|
|
132
|
+
%w[a b].each { FileUtils.mkdir_p(File.join(@dir, _1), mode: 0o700) }
|
|
85
133
|
# Fire-and-forget reap of the whole private dir (block form).
|
|
86
134
|
Pikuri::Finalizers.register { FileUtils.rm_rf(@dir) }
|
|
87
135
|
end
|
|
88
136
|
|
|
89
|
-
#
|
|
90
|
-
#
|
|
137
|
+
# @return [Boolean] whether the copy exists and the source hasn't
|
|
138
|
+
# changed since it was taken.
|
|
139
|
+
def current?
|
|
140
|
+
!@generation.nil? && File.file?(@path) && self.class.generation(@source) == @generation
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# (Re)take the snapshot unless {#current?}. On +true+, {#path} names the
|
|
144
|
+
# new copy: close the connection to the older one and open it.
|
|
91
145
|
#
|
|
92
|
-
# @param attempts [Integer] bracket
|
|
93
|
-
# @param backoff [Float] seconds between
|
|
94
|
-
#
|
|
95
|
-
# @
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
146
|
+
# @param attempts [Integer] bracket tries before giving up.
|
|
147
|
+
# @param backoff [Float] seconds between tries; Thunderbird's sync is slow,
|
|
148
|
+
# so there is no point hammering the file.
|
|
149
|
+
# @return [Boolean] +true+ if a fresh copy was taken this call; +false+
|
|
150
|
+
# when the copy is current, or when Thunderbird kept writing through
|
|
151
|
+
# every try and the older copy keeps serving.
|
|
152
|
+
# @raise [TornError] if no settled copy could be taken and none exists yet.
|
|
153
|
+
def refresh(attempts: 4, backoff: 0.25)
|
|
154
|
+
return false if current?
|
|
155
|
+
|
|
156
|
+
detail = copy_bracketed(attempts, backoff)
|
|
157
|
+
return true unless detail
|
|
158
|
+
|
|
159
|
+
LOGGER.warn("no settled snapshot of #{@source} after #{attempts} tries (#{detail})" \
|
|
160
|
+
"#{', serving the older copy' if @generation}")
|
|
161
|
+
raise TornError, BUSY unless @generation
|
|
162
|
+
|
|
163
|
+
false
|
|
102
164
|
end
|
|
103
165
|
|
|
104
166
|
private
|
|
105
167
|
|
|
106
|
-
# @return [
|
|
107
|
-
#
|
|
168
|
+
# @return [String, nil] why the last try failed, or +nil+ once a
|
|
169
|
+
# settled copy is in place.
|
|
108
170
|
def copy_bracketed(attempts, backoff)
|
|
171
|
+
target = slot_path(@generation && @path == slot_path('a') ? 'b' : 'a')
|
|
109
172
|
detail = nil
|
|
110
|
-
attempts.times do
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
173
|
+
attempts.times do |i|
|
|
174
|
+
sleep(backoff) if i.positive?
|
|
175
|
+
before = self.class.generation(@source)
|
|
176
|
+
before_journal = journal?
|
|
177
|
+
copy_error = copy_files(target)
|
|
178
|
+
after = self.class.generation(@source)
|
|
179
|
+
after_journal = journal?
|
|
180
|
+
|
|
181
|
+
if copy_error.nil? && !before.nil? && before == after && !before_journal && !after_journal
|
|
182
|
+
@path = target
|
|
183
|
+
@generation = after
|
|
184
|
+
return nil
|
|
120
185
|
end
|
|
121
186
|
|
|
122
|
-
detail = "
|
|
123
|
-
|
|
124
|
-
sleep(backoff)
|
|
187
|
+
detail = copy_error || "generation #{before.inspect}->#{after.inspect}, " \
|
|
188
|
+
"journal #{before_journal}/#{after_journal}"
|
|
125
189
|
end
|
|
126
|
-
|
|
190
|
+
detail
|
|
127
191
|
end
|
|
128
192
|
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
|
|
132
|
-
# @return [Boolean]
|
|
133
|
-
def settled?(before_counter, after_counter, before_journal, after_journal)
|
|
134
|
-
!before_counter.nil? && before_counter == after_counter &&
|
|
135
|
-
!before_journal && !after_journal
|
|
136
|
-
end
|
|
193
|
+
# @param slot [String] +"a"+ or +"b"+.
|
|
194
|
+
# @return [String]
|
|
195
|
+
def slot_path(slot) = File.join(@dir, slot, File.basename(@source))
|
|
137
196
|
|
|
138
197
|
# @return [Boolean] whether a +<source>-journal+ (write in flight) exists.
|
|
139
|
-
def journal?
|
|
198
|
+
def journal? = File.exist?("#{@source}-journal")
|
|
199
|
+
|
|
200
|
+
# Copy the main file, then its +-wal+ if one exists.
|
|
201
|
+
#
|
|
202
|
+
# @param target [String] the slot's copy path.
|
|
203
|
+
# @return [String, nil] +cp+'s complaint, or +nil+ on success.
|
|
204
|
+
def copy_files(target)
|
|
205
|
+
FileUtils.rm_f(["#{target}-wal", "#{target}-shm"])
|
|
206
|
+
error = cp(@source, target)
|
|
207
|
+
wal = "#{@source}-wal"
|
|
208
|
+
error ||= cp(wal, "#{target}-wal") if File.exist?(wal)
|
|
209
|
+
error
|
|
210
|
+
end
|
|
140
211
|
|
|
141
212
|
# reflink-when-possible copy through the Subprocess chokepoint.
|
|
142
213
|
#
|
|
143
|
-
# @return [
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
result
|
|
147
|
-
'cp', '--reflink=auto', '-f', @source, @path, chdir: '/'
|
|
148
|
-
).wait
|
|
149
|
-
return if result.status.success?
|
|
150
|
-
|
|
151
|
-
raise TornError, "cp failed copying #{@source}: #{result.output.strip}"
|
|
214
|
+
# @return [String, nil] +cp+'s complaint, or +nil+ on success.
|
|
215
|
+
def cp(from, to)
|
|
216
|
+
result = Pikuri::Subprocess.spawn('cp', '--reflink=auto', '-f', from, to, chdir: '/').wait
|
|
217
|
+
result.status.success? ? nil : "cp failed copying #{from}: #{result.output.strip}"
|
|
152
218
|
end
|
|
153
219
|
end
|
|
154
220
|
end
|