Repository navigation
Expand file tree
/
Copy pathsession.odin
More file actions
530 lines (450 loc) · 15.1 KB
/
Copy pathsession.odin
File metadata and controls
530 lines (450 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package diff_view
import "base:intrinsics"
import "base:runtime"
import "core:fmt"
import "core:log"
import "core:mem"
import "core:mem/virtual"
import "core:os"
import "core:path/filepath"
import "core:strings"
import "core:sync/chan"
import "core:time"
Session_Error :: enum {
None,
Arena_Init_Failed,
Snapshot_Arena_Init_Failed,
Watcher_Init_Failed,
Scan_Failed,
}
Session_Mode :: enum {
Live,
View,
}
DEBOUNCE_INTERVAL :: 50 * time.Millisecond
COALESCE_WINDOW :: 200 * time.Millisecond
MAX_FILE_SIZE :: 10 * mem.Megabyte
File_Entry :: struct {
path: string,
first_snap_idx: int,
latest_snap_idx: int,
snapshot_count: int,
snap_indices: [dynamic]int,
is_deleted: bool,
last_debounce: time.Time,
pending_delete: bool,
delete_timestamp: time.Time,
last_store_seq: int,
last_store_kind: Snapshot_Kind,
}
Session :: struct {
mode: Session_Mode,
root_path: string,
session_name: string,
store_path: string,
arena: virtual.Arena,
snapshot_arena: virtual.Arena,
allocator: mem.Allocator,
snap_allocator: mem.Allocator,
watcher: Watcher,
ignore_rules: Ignore_Rules,
timeline: Timeline,
file_entries: map[string]^File_Entry,
snapshot_seq: int,
running: bool,
event_channel: chan.Chan(Watcher_Event),
known_paths: map[string]bool,
store_entries: []Store_Entry,
}
session_create :: proc(root_path: string, session_name: string) -> (session: ^Session, err: Session_Error) {
bootstrap_err: mem.Allocator_Error
session, bootstrap_err = virtual.arena_growing_bootstrap_new_by_name(Session, "arena")
if bootstrap_err != nil {
return nil, .Arena_Init_Failed
}
session.allocator = virtual.arena_allocator(&session.arena)
context.allocator = session.allocator
session.mode = .Live
snap_err := virtual.arena_init_growing(&session.snapshot_arena)
if snap_err != nil {
return nil, .Snapshot_Arena_Init_Failed
}
session.snap_allocator = virtual.arena_allocator(&session.snapshot_arena)
abs_root: string
if filepath.is_abs(root_path) {
abs_root = strings.clone(root_path, session.allocator)
} else {
cwd := os.get_current_directory(context.temp_allocator)
abs_root = strings.clone(filepath.join({cwd, root_path}, context.temp_allocator), session.allocator)
}
session.root_path = abs_root
if len(session_name) > 0 {
session.session_name = strings.clone(session_name, session.allocator)
} else {
now := time.now()
y, mon, d := time.date(now)
h, min, s := time.clock(now)
generated := fmt.tprintf("session_%04d%02d%02d_%02d%02d%02d", y, int(mon), d, h, min, s)
session.session_name = strings.clone(generated, session.allocator)
}
session.file_entries = make(map[string]^File_Entry, allocator = session.allocator)
timeline_init(&session.timeline, session.allocator)
seq, store_path, store_ok := store_init(session.root_path, session.session_name)
if !store_ok {
log.warnf("Failed to initialize store — snapshots will not be persisted")
}
session.snapshot_seq = seq
session.store_path = strings.clone(store_path, session.allocator)
session.ignore_rules = ignore_rules_load(session.root_path, session.allocator)
chan_alloc_err: mem.Allocator_Error
session.event_channel, chan_alloc_err = chan.create(chan.Chan(Watcher_Event), 256, context.allocator)
if chan_alloc_err != nil {
return nil, .Arena_Init_Failed
}
watcher_err := watcher_create(&session.watcher, session.root_path, &session.event_channel, &session.ignore_rules, session.allocator)
if watcher_err != nil {
log.errorf("Watcher error: %v", watcher_err)
return nil, .Watcher_Init_Failed
}
if session.snapshot_seq > 1 {
entries, load_ok := store_load_entries(session.store_path, session.allocator)
if load_ok {
session.known_paths = make(map[string]bool, len(entries), session.allocator)
for se in entries {
abs_path := strings.clone(
filepath.join({session.root_path, se.rel_path}, context.temp_allocator),
session.allocator,
)
session.known_paths[abs_path] = true
fe: ^File_Entry
if existing, found := session.file_entries[abs_path]; found {
fe = existing
} else {
fe = new(File_Entry, session.allocator)
fe.path = abs_path
fe.snap_indices = make([dynamic]int, 0, 4, session.allocator)
session.file_entries[abs_path] = fe
}
fe.snapshot_count += 1
fe.is_deleted = (se.kind == .Deleted)
fe.last_store_seq = se.seq
fe.last_store_kind = se.kind
}
log.infof("Loaded %d existing entries from store", len(entries))
}
}
return session, nil
}
session_load :: proc(root_path: string, session_name: string) -> (session: ^Session, err: Session_Error) {
bootstrap_err: mem.Allocator_Error
session, bootstrap_err = virtual.arena_growing_bootstrap_new_by_name(Session, "arena")
if bootstrap_err != nil {
return nil, .Arena_Init_Failed
}
session.allocator = virtual.arena_allocator(&session.arena)
context.allocator = session.allocator
session.mode = .View
abs_root: string
if filepath.is_abs(root_path) {
abs_root = strings.clone(root_path, session.allocator)
} else {
cwd := os.get_current_directory(context.temp_allocator)
abs_root = strings.clone(filepath.join({cwd, root_path}, context.temp_allocator), session.allocator)
}
session.root_path = abs_root
session.session_name = strings.clone(session_name, session.allocator)
base_path := filepath.join({abs_root, STORE_DIR}, context.temp_allocator)
store_path := filepath.join({base_path, session_name}, context.temp_allocator)
session.store_path = strings.clone(store_path, session.allocator)
entries, load_ok := store_load_entries(session.store_path, session.allocator)
if !load_ok {
return nil, .Scan_Failed
}
session.store_entries = entries
session.file_entries = make(map[string]^File_Entry, allocator = session.allocator)
for se, idx in entries {
abs_path := strings.clone(
filepath.join({session.root_path, se.rel_path}, context.temp_allocator),
session.allocator,
)
fe: ^File_Entry
if existing, found := session.file_entries[abs_path]; found {
fe = existing
} else {
fe = new(File_Entry, session.allocator)
fe.path = abs_path
fe.snap_indices = make([dynamic]int, 0, 4, session.allocator)
session.file_entries[abs_path] = fe
}
fe.snapshot_count += 1
append(&fe.snap_indices, idx) // index into store_entries
fe.latest_snap_idx = idx
if fe.snapshot_count == 1 {
fe.first_snap_idx = idx
}
fe.is_deleted = (se.kind == .Deleted)
}
log.infof("Loaded session '%s': %d entries, %d files", session_name, len(entries), len(session.file_entries))
return session, nil
}
session_run :: proc(session: ^Session) {
context.allocator = session.allocator
lanes_run(_session_lane_entry, session, session.allocator)
}
_session_lane_entry :: proc(data: rawptr) {
session := (^Session)(data)
context.allocator = session.allocator
paths_count: u64 = 0
work_raw: rawptr
if lane_idx() == 0 {
file_paths := make([dynamic]string, 0, 256, context.temp_allocator)
ctx := Collect_Context{session = session, file_paths = &file_paths}
filepath.walk(session.root_path, _collect_walk_proc, &ctx)
if len(file_paths) > 0 {
skip := make([]bool, len(file_paths), context.temp_allocator)
if session.known_paths != nil {
for path, i in file_paths {
if path in session.known_paths {
skip[i] = true
}
}
}
work := new(Scan_Work, context.temp_allocator)
work.paths = file_paths[:]
work.snapshots = make([]Snapshot, len(file_paths), context.temp_allocator)
work.errors = make([]Snapshot_Error, len(file_paths), context.temp_allocator)
work.snap_allocator = session.snap_allocator
work.skip_flags = skip
work_raw = rawptr(work)
paths_count = u64(len(file_paths))
}
}
lane_sync_u64(&paths_count, 0)
lane_sync_ptr(&work_raw, 0)
if paths_count > 0 {
work := (^Scan_Work)(work_raw)
lo, hi := lane_range(int(paths_count))
for i in lo ..< hi {
if work.skip_flags != nil && work.skip_flags[i] do continue
snap, snap_err := snapshot_capture(work.snap_allocator, work.paths[i], .Initial)
work.snapshots[i] = snap
work.errors[i] = snap_err
}
}
lane_sync()
if lane_idx() == 0 {
if paths_count > 0 {
work := (^Scan_Work)(work_raw)
for i in 0 ..< int(paths_count) {
if work.skip_flags != nil && work.skip_flags[i] {
interned_path := strings.clone(work.paths[i], session.allocator)
watcher_add_path(&session.watcher, interned_path, false)
continue
}
if work.errors[i] != nil {
if work.errors[i] != .File_Too_Large {
rel := path_relative_to(work.paths[i], session.root_path)
log.warnf("Failed to snapshot %s: %v", rel, work.errors[i])
}
continue
}
interned_path := strings.clone(work.paths[i], session.allocator)
work.snapshots[i].file_path = interned_path
idx := timeline_append(&session.timeline, work.snapshots[i])
entry := new(File_Entry, session.allocator)
entry.path = interned_path
entry.first_snap_idx = idx
entry.latest_snap_idx = idx
entry.snapshot_count = 1
entry.snap_indices = make([dynamic]int, 0, 4, session.allocator)
append(&entry.snap_indices, idx)
session.file_entries[interned_path] = entry
store_persist(session.store_path, session.root_path, timeline_get(&session.timeline, idx), &session.snapshot_seq)
watcher_add_path(&session.watcher, interned_path, false)
}
}
log.infof("Initial scan: %d files, %d snapshots", len(session.file_entries), timeline_len(&session.timeline))
log.infof("Arena used: %v bytes, Snapshot arena used: %v bytes", session.arena.total_used, session.snapshot_arena.total_used)
log.infof("Watching %d fds", len(session.watcher.platform.fd_to_path))
session.known_paths = nil
watcher_start(&session.watcher)
session.running = true
log.info("Watching for changes... (Ctrl+C to stop)")
}
lane_sync()
for {
if lane_idx() == 0 {
for {
event, ok := chan.try_recv(session.event_channel)
if !ok do break
session_process_event(session, event)
}
_session_flush_pending_deletes(session)
time.sleep(10 * time.Millisecond)
}
running_u64: u64 = 1
if lane_idx() == 0 {
running_u64 = intrinsics.atomic_load(&session.running) ? 1 : 0
}
lane_sync_u64(&running_u64, 0)
if running_u64 == 0 do break
}
}
session_poll_events :: proc(session: ^Session) {
context.allocator = session.allocator
for {
event, ok := chan.try_recv(session.event_channel)
if !ok do break
session_process_event(session, event)
}
_session_flush_pending_deletes(session)
}
session_process_event :: proc(session: ^Session, event: Watcher_Event) {
context.allocator = session.allocator
path := event.path
defer delete(path, runtime.heap_allocator())
rel_path := path_relative_to(path, session.root_path)
if ignore_should_skip(&session.ignore_rules, rel_path, false) {
return
}
kind: Snapshot_Kind
switch event.kind {
case .Created: kind = .Created
case .Modified: kind = .Modified
case .Deleted: kind = .Deleted
case .Renamed: kind = .Renamed
case .Attr_Changed: return // ignore attribute-only changes
}
now := time.now()
entry := session.file_entries[path]
if kind == .Deleted {
if entry != nil {
entry.pending_delete = true
entry.delete_timestamp = now
} else {
interned_path := strings.clone(path, session.allocator)
new_entry := new(File_Entry, session.allocator)
new_entry.path = interned_path
new_entry.pending_delete = true
new_entry.delete_timestamp = now
session.file_entries[interned_path] = new_entry
}
return
}
skip_debounce := false
if entry != nil && entry.pending_delete {
entry.pending_delete = false
kind = .Modified
skip_debounce = true
}
if !skip_debounce && entry != nil {
if time.diff(entry.last_debounce, now) < DEBOUNCE_INTERVAL {
return
}
}
_session_record_snapshot(session, path, kind)
}
_session_record_snapshot :: proc(session: ^Session, path: string, kind: Snapshot_Kind) {
context.allocator = session.allocator
rel_path := path_relative_to(path, session.root_path)
snap, snap_err := snapshot_capture(session.snap_allocator, path, kind)
if snap_err != nil {
if snap_err != .File_Too_Large {
log.warnf("Failed to capture snapshot for %s: %v", rel_path, snap_err)
}
return
}
interned_path := path
entry: ^File_Entry
is_new_entry := false
if existing, found := session.file_entries[path]; found {
entry = existing
entry.snapshot_count += 1
entry.is_deleted = (kind == .Deleted)
interned_path = entry.path
} else {
interned_path = strings.clone(path, session.allocator)
entry = new(File_Entry, session.allocator)
entry.path = interned_path
entry.snapshot_count = 1
entry.is_deleted = (kind == .Deleted)
entry.snap_indices = make([dynamic]int, 0, 4, session.allocator)
session.file_entries[interned_path] = entry
is_new_entry = true
}
now := time.now()
snap.file_path = interned_path
idx := timeline_append(&session.timeline, snap)
entry.latest_snap_idx = idx
entry.last_debounce = now
append(&entry.snap_indices, idx)
if is_new_entry {
entry.first_snap_idx = idx
}
store_persist(session.store_path, session.root_path, timeline_get(&session.timeline, idx), &session.snapshot_seq)
if kind != .Initial {
if len(entry.snap_indices) >= 2 {
prev_snap_idx := entry.snap_indices[len(entry.snap_indices) - 2]
diff_result, diff_ok := diff_compute(session, prev_snap_idx, idx)
if diff_ok {
diff_result.file_path = interned_path
store_persist_diff(session.store_path, session.root_path, &diff_result, session.snapshot_seq - 1, kind)
diff_display_destroy(&diff_result)
}
} else if entry.last_store_seq > 0 {
rel := path_relative_to(interned_path, session.root_path)
old_content, old_ok := store_load_content(
session.store_path, rel,
entry.last_store_seq, entry.last_store_kind,
context.temp_allocator,
)
new_snap := timeline_get(&session.timeline, idx)
if old_ok && new_snap != nil {
diff_result, diff_ok := diff_compute_from_content(
old_content, new_snap.content, interned_path,
context.temp_allocator,
)
if diff_ok {
store_persist_diff(session.store_path, session.root_path, &diff_result, session.snapshot_seq - 1, kind)
}
}
}
}
entry.last_store_seq = session.snapshot_seq - 1
entry.last_store_kind = kind
log.infof("[%s] %s", kind, rel_path)
}
_session_flush_pending_deletes :: proc(session: ^Session) {
context.allocator = session.allocator
now := time.now()
expired_buf: [64]string
expired_count := 0
for _, entry in session.file_entries {
if entry.pending_delete && time.diff(entry.delete_timestamp, now) >= COALESCE_WINDOW {
if expired_count < len(expired_buf) {
expired_buf[expired_count] = entry.path
expired_count += 1
}
}
}
for path in expired_buf[:expired_count] {
if entry, found := session.file_entries[path]; found {
entry.pending_delete = false
}
_session_record_snapshot(session, path, .Deleted)
}
}
session_destroy :: proc(session: ^Session) {
if session == nil do return
if session.mode == .Live {
watcher_stop(&session.watcher)
watcher_destroy(&session.watcher)
chan.destroy(&session.event_channel)
}
snapshot_arena := session.snapshot_arena
session_arena := session.arena
if session.mode == .Live {
virtual.arena_destroy(&snapshot_arena)
}
virtual.arena_destroy(&session_arena)
}