Skip to content

feat(core): read and write .sql files from anywhere on disk - #808

Merged
DokaDev merged 9 commits into
mainfrom
files-048/3-file-core
Dec 6, 2026
Merged

DokaDev merged 9 commits into
mainfrom
files-048/3-file-core

Conversation

@DokaDev

@DokaDev DokaDev commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Related issue: #311
Related issue: #812

Third part of the 0.48.0 "files" stack, on top of #797 (files-048/2-new-tab). This is the core of part B of #311 (.sql files from disk): reading and writing text files, the unsaved-text store and the workspace's file tab. No UI here: the next part (files-048/4-open-save) opens and saves file tabs with it. The only change users see is in saved queries, which now read and write through the same code.

What changes

  • datarig-core::textfile (new):
    • read(path, rules): follows a link, takes a regular file only (looked at before it is opened, so a pipe never blocks), decodes strictly and never lossily: UTF-8 with or without a byte order mark, UTF-16 LE or BE behind its byte order mark (an unpaired surrogate or an odd length is refused), a UTF-32 byte order mark refused, anything else that is not UTF-8 refused. The byte order mark is taken off and remembered. Line endings: all LF (or none) as is; all CRLF edited as LF and remembered; mixed or a lone CR kept byte for byte. Rules::DISK wants a UTF-8 path and at most MAX_LEN (20 MiB; exactly at the cap opens, one byte over is refused with both sizes); Rules::SAVED_QUERY has neither rule.
    • TextFile { text, format, seen }: Format is the encoding and line ending; Seen is the Stamp (mtime and length, moved here from scripts) and an FNV-1a 64 hash of the bytes (the fnv crate, already in the tree; pinned to the published test values, as the hash is kept in workspace.toml).
    • write(path, text, format, expect): encodes back exactly (line endings, byte order mark, UTF-16), checks Expect::Seen first (Conflict when the stamp or the hash differs, so a change of the same size within the same second is seen; Missing when the file or a link's target is gone), Expect::New { replace } answers Exists for another file there unless the caller replaces (without replace the file is put in place by fsutil::create_new, so a file made there meanwhile is never replaced either), Expect::Any writes regardless. A file without write permission (ReadOnly), a folder that cannot be written (FolderReadOnly) and a missing folder (NoFolder) are refused before anything is written, and permissions are never changed. The write goes through fsutil::atomic_write: through a link to its target (the link stays) and keeping the Unix mode.
    • check(path, seen): Same, Changed or Gone, for the check on tab enter and at restore.
    • Errors (FileError) carry the path and the problem (an io kind for the rest), never the text.
    • textfile::path: expand (~, ./, ../, absolute, against the home folder and the launch folder; . and .. by name), is_path (/, ~, ./, ../; on Windows also C:\, C:/, \\, .\, ..\), display (~ for the home folder), FileId (device and inode on Unix, the final path on Windows) and home (HOME, else USERPROFILE, through the app's env lookup; App::home uses it).
  • Saved queries read and write through it, uncapped: a byte order mark is never sent to the server again (the PostgreSQL and MySQL lexers did not skip it) and stays in the file; CRLF files are edited as lines and written back as CRLF; UTF-16 files with a byte order mark open instead of being refused. Autosave, save as and overwrite write each file back in its own form (Doc.format), and a save checks the hash too (Doc.seen). Saved queries keep their write rules otherwise.
  • Unsaved text of file tabs: workspace::{write,read,remove}_unsaved, <state>/files/<id>.sql, a 0700 folder (made private by fsutil::create_private_dir, moved here from the result spill, which uses it as before) of 0600 files (atomic_write_private).
  • workspace.toml: the tab kind file within version 3, with path (absolute), mtime, len, hash and unsaved; tab_key finds a file tab by its path. A file tab without an absolute path is left out; of two with one id, the later one gets an id of its own and no unsaved text (they would share it). An older datarig keeps the kind verbatim. The TUI does not open file tabs yet: a restored one is kept as its text (workspace::tab_text, the form unknown tabs are kept in) and written back as it was, with the keys this build does not read.
  • Read-only saved queries (Do not save over a saved query whose file was made read-only #812): a saved query whose file was made read-only outside datarig is no longer saved over. Autosave, Ctrl+S and an overwrite after a conflict get SaveError::ReadOnly (checked by textfile::read_only, after the version check, so a change on disk is still a conflict first); the tab shows * and "Not saved: the file is read-only · Space s a saves a copy" (the save-as key from the key map), an asked save says so every time, and the permissions are never changed. Once the file may be written again, the next autosave writes it. Docs: the editor guide; changes/fixed/read-only-saved-query.md.
  • crates/bench/budgets.toml: a note of what the text file core adds to the binary: 32.991 -> 33.013 MiB on the CI's Linux x86_64, measured at e0fc73d2 (with Do not save over a saved query whose file was made read-only #812) before the rebase onto 0.47.3. 0.47.3 had already raised the budget to 34 MiB, so this PR no longer changes it. On the new base, CI's performance job measures 33.057 MiB at 88ca8906, against the 34 MiB budget.
  • Docs: docs/architecture.md (textfile, the saved queries' form and version, the unsaved store and the file kind) and the editor guide (encodings and line endings of saved queries).
  • changes/fixed/saved-query-encoding.md.

Failing first

On the stack base c356e925 (files-048/2-new-tab), the saved-query flow tests (cargo test -p datarig-tui --test it -- a_saved_query_with_a_byte_order_mark a_crlf_saved_query): 2 failed:

a_saved_query_with_a_byte_order_mark_is_sent_without_it_and_keeps_it:
  left: (Some("bom.sql"), "\u{feff}select 1;\n")      the mark is in the editor, so a run sends it
 right: (Some("bom.sql"), "select 1;\n")
a_crlf_saved_query_is_edited_as_lines_and_written_back_byte_for_byte: no CR in the lines
  left: "select 1;\r\nselect 2;\r\n"
 right: "select 1;\nselect 2;\n"

From the review bot's first finding (fixed in 68c37744), on 1c71d47c, a_file_tab_this_build_does_not_open_stays_in_the_workspace: the tab is gone from workspace.toml after a launch and a quit:

id = "f1": version = 3
active = 0
[explorer]
...

The bot's second finding (a file made at a save-as path between the check and the rename was replaced) is fixed in e9bc3ce4 by publishing through fsutil::create_new; that race is not reproducible in a test, and create_new is covered by its own tests. CI's clippy asked for is_multiple_of (1c71d47c), its performance job for the larger binary budget (5074f05a), and its Windows tests for times in 100 ns steps in a workspace test (c4c30718; Windows keeps file times so).

For #812 (e0fc73d2), on c4c30718, cargo test -p datarig-tui --test it -- a_saved_query_made_read_only_outside_is_never_saved_over: 1 failed, the autosave wrote over the 0444 file:

assertion `left == right` failed: the autosave left the file as it was
  left: b"select 'kept'select 'not written';;"   (bytes on disk after the autosave)
 right: b"select 'kept';"

The core tests are new with the module they test (textfile, and the file kind and unsaved store of workspace): on the base they do not build (textfile does not exist; TabKind::File, FileTab, write_unsaved are not there), and every one passes here.

Tests

  • textfile (core): round_trips_are_byte_exact (LF, CRLF, mixed, a lone CR, UTF-8 BOM with LF and CRLF, UTF-16 LE with a surrogate pair and CRLF, UTF-16 BE without a final newline, a UTF-16 BOM alone, an empty file, no final newline; pure and on disk), an_edit_is_written_in_the_files_own_form, the_hash_is_fnv_1a_64_bits (the published test values), a_stamp_is_kept_as_nanoseconds_and_a_length (also before the epoch), text_that_cannot_be_read_exactly_is_refused (Windows-1252, a BOM then Latin-1, an unpaired high and low surrogate, an odd length, UTF-32 LE and BE, a folder, a missing file), files_from_disk_are_capped_and_saved_queries_are_not (exactly 20 MiB opens, one byte over is refused with both sizes, uncapped for saved queries), a_path_that_is_not_utf8_is_refused (Unix), a_change_on_disk_is_a_conflict_and_a_file_gone_is_missing (the same length with the mtime set back by File::set_modified, another length, gone, written again only when asked), save_as_onto_another_file_answers_exists_unless_told_to_replace (also a folder, and a missing folder is never made), a_write_goes_through_a_link_which_stays_a_link (Unix; a dangling link is missing, and something there for a save as), a_read_only_file_and_a_folder_that_cannot_be_written_are_refused (Unix: 0444 file under every expectation, mode untouched; 0555 folder gets no new or replaced file; a 0640 file keeps its mode), errors_never_hold_the_text (a marker in the file never in an error's text, debug or io form).
  • textfile::path (core): the_home_folder_is_home_else_userprofile, an_argument_is_a_path_by_how_it_starts (both rule sets on every system), a_typed_path_is_expanded_from_home_and_the_launch_folder (Unix), a_typed_path_is_expanded_by_the_rules_of_windows (Windows), a_path_under_the_home_folder_is_shown_with_a_tilde (Unix), a_file_is_told_by_the_file_not_by_its_path (another spelling, a link, another case where the file system ignores it).
  • workspace (core): a_file_tab_comes_back_with_its_file (with a key a later version adds), a_file_tab_that_cannot_be_read_back_is_left_out_or_loses_only_what_cannot_be, an_older_datarig_keeps_a_file_tab_as_it_was (the tab as an older reader keeps it, written back and read again), unsaved_text_is_kept_private_and_removed_when_done (0700 folder, 0600 file on Unix, an opened-up folder made private again, ids that are not file names refused).
  • scripts (core): a_saved_query_is_edited_without_its_byte_order_mark_and_written_back_as_it_was (BOM with CRLF, CRLF, a lone CR, UTF-16; save as keeps the form), and saves_check_the_stamp_and_never_overwrite_a_changed_file now also checks a change of the same length with the mtime set back.
  • Do not save over a saved query whose file was made read-only #812: a_saved_query_made_read_only_outside_is_never_saved_over (flow: autosave and Ctrl+S twice leave the bytes and the 0444 mode; * and the words; writable again, the next autosave writes) and a_saved_query_without_write_permission_is_never_saved_over (core: autosave and overwrite refused, a change on disk is a conflict first, the mode kept).
  • Flows (TUI): the two above, and a_file_tab_this_build_does_not_open_stays_in_the_workspace (review).
  • workspace also: a_tab_kept_as_its_text_is_written_back_as_it_was (review).
  • Updated for the new field and API: the TabState literals of the bench and the PostgreSQL and MySQL integration tests (file: None), crash_safety (the save's format), the spill test's import.

Not in this part

  • The TUI for files (open, save, save as, the close and conflict questions, the check on tab enter, restore with its rules) comes in files-048/4-open-save.

@DokaDev
DokaDev added this pull request to stack #798 December 4, 2026 08:29
@DokaDev DokaDev changed the title files 048/3 file core feat(core): read and write .sql files from anywhere on disk Dec 4, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T08:31:50.738226Z 4ab1f08 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ab1f08398

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/tui/src/app/persist.rs Resolved
Comment thread crates/core/src/textfile.rs Resolved
A new module, textfile, reads a text file whole and never lossily:
UTF-8 with or without a byte order mark, or UTF-16 (LE or BE) behind
its byte order mark, decoded strictly; a UTF-32 byte order mark,
invalid UTF-16 and text that is not UTF-8 are refused. The byte order
mark is taken off for editing and written back; all-CRLF text is edited
as LF and written back as CRLF; mixed line endings and lone CRs are kept
byte for byte. A file from disk needs a UTF-8 path and a regular file
(a link followed) of at most 20 MiB.

A version of a file is its stamp (mtime, length) and an FNV-1a hash of
its bytes, so a change of the same length within the same second is
seen. A write checks the version last seen first (conflict, missing,
exists for a save as unless it replaces), refuses a file without write
permission and a folder that is not there or cannot be written, never
changes permissions, and goes through atomic_write (through a link to
its target, keeping the Unix mode). Errors carry the path and the
problem, never the text. Path helpers expand ~, ./ and ../ against the
home and launch folders, tell a path argument from a saved query's
name, show ~ for home and tell files apart by device and inode (the
final path on Windows).

The unsaved text of file tabs goes to <state>/files/<id>.sql, a folder
private to the user (the private-folder rule moves from the result
spill to fsutil) of 0600 files. workspace.toml gets the tab kind file
within version 3: its absolute path, the version of its file and
whether unsaved text is kept; an older datarig keeps it verbatim. The
app opens no file tabs yet: a restored one is left out.

Related issue: #311
Saved queries now read and write through textfile, with no size cap:
a byte order mark is taken off for editing, so it is never sent to the
server again (the PostgreSQL and MySQL lexers did not skip it), and is
written back; a CRLF file is edited as lines and written back with
CRLF; a UTF-16 file with a byte order mark opens instead of being
refused. Autosave, save as and overwrite write each file back in the
form it was read in, and a save checks the hash of the file too, so a
change of the same length within the same second is a conflict.

Related issue: #311
Clippy (manual_is_multiple_of) asks for the method over the remainder.

Related issue: #311
A workspace.toml with a file tab lost it at the first save: this build
opens no file tabs and left the tab out, where an older build keeps it
as a tab of a kind it does not know. The tab is now kept as its text
(workspace::tab_text, the form unknown tabs are kept in) and written
back as it was, with the keys this build does not read. Its unsaved
text stays in the state directory.

Related issue: #311
A save as that must not replace a file checked the path, then renamed
the new file over it, so a file made there in between was replaced. It
now goes through fsutil::create_new, which puts the file in place only
where nothing is, and answers Exists otherwise.

Related issue: #311
Reading and writing text files from disk (strict UTF-8 and UTF-16
decoding, the line endings, the file hash) brought the CI's Linux x86_64
binary from 32.991 to 33.013 MiB before the 0.47 fixes, within the
34 MiB budget that 0.47.3 set.

Related issue: #311
Windows keeps a time in steps of 100 ns, so a stamp of 1 ns precision
came back rounded there. A file's own time is always in such steps; the
test now uses them too.

Related issue: #311
Autosave, Ctrl+S and an overwrite now leave a saved query's file alone when
it has no write permission, as a file tab from disk does. The tab shows that
the text is not saved and why, Space s a saves it as another query, and the
file's permissions are never changed. Once the file may be written again,
the next autosave writes it.

Related issue: #812
#784 added a saved table tab to the SQLite TUI tests. On this branch
TabState has a file field, so that tab needs it too, or the test does
not compile.

Related issue: #311
@DokaDev
DokaDev force-pushed the files-048/3-file-core branch from 88ca890 to fedc98b Compare December 6, 2026 08:37
@DokaDev
DokaDev merged commit f1642a9 into main Dec 6, 2026
25 checks passed
@DokaDev
DokaDev deleted the files-048/3-file-core branch December 6, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant