feat: v0.8.0 edit and save files with conflict detection
build-deb / Lint, test, package (push) Successful in 1m57s
build-deb / Deploy to CT 126 (push) Successful in 3s
build-deb / Notify on failure (push) Skipped

- devdesk attach --fs-write SESSION PATH BYTES BASESHA|new: streamed like
  uploads, temp file in the same folder, sha re-check (conflict reply instead
  of overwriting), mode/owner kept, atomic mv; new files with folders, never
  outside the session folder or through a symlink out of it.
- Editor: editable CodeMirror for UTF-8 text, dirty tracking, Ctrl+S,
  Save/Revert, conflict dialog with a disk-vs-yours diff, changed-on-disk
  banner, drafts in localStorage (flushed on pagehide), close/leave guards,
  New file.
- Upload streaming factored into streamIn(), shared by uploads and saves.

Closes #20

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 20:10:45 -04:00
co-authored by Claude Opus 5.5
parent 927920c162
commit 66321f4748
12 changed files with 573 additions and 35 deletions
+75
View File
@@ -190,6 +190,81 @@ cmd_git_show() {
rm -f "$tmp"
}
# --fs-write SESSION REL BYTES BASE (devdesk#20): save a file from the editor.
# The content arrives as base64 on stdin after the READY line (like uploads).
# BASE is the sha256 the editor loaded: if the file changed since, nothing is
# written and the reply is {conflict: true, sha: <current>}. BASE "new" creates
# a file (and missing folders) and refuses to overwrite one. Writes go to a
# temp file in the same folder, keep the mode and owner, and land with mv.
cmd_fs_write() {
[ $# -eq 4 ] || dd_die "usage: devdesk attach --fs-write SESSION PATH BYTES BASESHA|new"
local root rel=$2 size=$3 base=$4 target dir name tmp b64len actual current sha mtime
root=$(dd_session_root "$1")
if ! [[ $size =~ ^[0-9]+$ ]] || [ "$size" -gt "$DEVDESK_FS_MAX" ]; then
dd_die "too large: the editor saves files up to $((DEVDESK_FS_MAX / 1024)) KiB"
fi
[[ $base =~ ^([0-9a-f]{64}|new)$ ]] || dd_die "bad base: $base"
case $rel in /* | *$'\n'* | *$'\r'* | '' | */ | . | ..) dd_die "bad path: $rel" ;; esac
name=${rel##*/}
if [ "$name" = . ] || [ "$name" = .. ]; then dd_die "bad path: $rel"; fi
if [ "$base" = new ]; then
if [ -e "$root/$rel" ] || [ -L "$root/$rel" ]; then dd_die "already exists: $rel"; fi
case $rel in */*) dir=$root/${rel%/*} ;; *) dir=$root ;; esac
# Missing folders are created, but never through a symlink out of the
# root: check the deepest folder that exists before creating any.
local existing=$dir
while [ ! -e "$existing" ]; do existing=${existing%/*}; done
existing=${existing#"$root"}
dd_fs_path "$root" "${existing#/}" >/dev/null
mkdir -p -- "$dir" || dd_die "cannot create folder for $rel"
dir=${dir#"$root"}
dir=$(dd_fs_path "$root" "${dir#/}")
target="$dir/$name"
else
target=$(dd_fs_path "$root" "$rel")
[ -f "$target" ] || dd_die "not a file: $rel"
dir=${target%/*}
fi
tmp=$(mktemp --tmpdir="$dir" ".${name}.devdesk-XXXXXX") || dd_die "cannot write in ${dir#"$root"/}"
trap 'rm -f "$tmp"' HUP INT TERM
# shellcheck disable=SC2017 # integer ceil(size/3)*4: the base64 length, on purpose
b64len=$(((size + 2) / 3 * 4))
stty raw -echo 2>/dev/null || true
printf 'DEVDESK-WRITE-READY\r\n'
if [ "$b64len" -gt 0 ]; then
head -c "$b64len" | base64 -d >"$tmp" 2>/dev/null || true
fi
stty sane 2>/dev/null || true
actual=$(stat -c %s "$tmp" 2>/dev/null || echo 0)
if [ "$actual" != "$size" ]; then
rm -f "$tmp"
dd_die "save incomplete ($actual of $size bytes)"
fi
if [ "$base" = new ]; then
if [ -e "$target" ]; then
rm -f "$tmp"
dd_die "already exists: $rel"
fi
chmod 0644 "$tmp"
else
current=$(sha256sum -- "$target" | cut -d' ' -f1)
if [ "$current" != "$base" ]; then
rm -f "$tmp"
trap - HUP INT TERM
jq -cMn --arg p "$rel" --arg h "$current" '{ok: false, conflict: true, path: $p, sha: $h}'
return
fi
chmod --reference="$target" "$tmp" 2>/dev/null || true
chown --reference="$target" "$tmp" 2>/dev/null || true
fi
mv -f -- "$tmp" "$target" || { rm -f "$tmp"; dd_die "cannot replace $rel"; }
trap - HUP INT TERM
sha=$(sha256sum -- "$target" | cut -d' ' -f1)
mtime=$(stat -c %Y -- "$target")
jq -cMn --arg p "${target#"$root"/}" --arg h "$sha" --argjson s "$size" --argjson m "$mtime" \
'{ok: true, path: $p, sha: $h, size: $s, mtime: $m}'
}
# --asset NAME: a lazily loaded part of the web UI (the file viewer's editor),
# shipped in $DEVDESK_ASSETS and cached by the browser.
cmd_asset() {