# Data file upload (csdfu) :id=top

------

> _Doc Ver:_ 2.5  
_App Ver:_ 2.6.6.53  
_Date:_ 09/11/2026

------

> **csdfu** (CASSTK Data File Upload) **ships in the SA1 pack**. The **service is installed and stays up** (`/var/sa/bin/csdfu`, `csdfu.service` / `sa1-supervise`). **Whether files are sent** is `enable` in `/var/sa/conf/csdfu.yaml` and Web 03 **Allow upload / Stop upload** — not a second install, not `cspkg install csdfu`, not a copy in `/usr/local/bin`.

This page used to describe **dtfu**. That tool is withdrawn. sa-di no longer curls uploads. Do not run a second uploader on the same data directory.

Three help surfaces must stay equally complete:

1. Device CLI: `/var/sa/bin/csdfu help` / `--help` ([§10](#cli) is the exact Chinese text the binary prints)
2. The page Help overlay and FACEPLATE Help tab (`:8787`, signed-in `/csdfu`)
3. This handbook (device Help menu and the online portal)

```bash
/var/sa/bin/csdfu help
/var/sa/bin/csdfu --help
/var/sa/bin/csdfu version
```

The binary `--help` is **Chinese**. English below is the same facts. The dump in [§10](#cli) matches the binary.

------

## 1. Open  :id=link

  * Menu > **Setting** > **System** > **Base** > **Data save**, group **2 csdfu**;
  * Signed-in `/csdfu` → this host **:8787** (not the SA page on port 80);
  * Save files on the device first ([Data save](data_save.md)). Usual sources: `/var/sa/data/wl`, `pv`, `raw`.

Factory YAML: `enable: false` (process + page up, no send). Fill a destination and tap **Allow upload** (writes YAML, **no restart**). Pause lookup only stops scanning in memory.

Do **not** set source `max_files` on SA1 — retention stays on the Data save page.

------

## 2. Three switches  :id=enable

All three must be on before anything is sent:

| Layer | Factory | How to change | If off |
|-------|---------|----------------|--------|
| Binary + service | Installed, started with the stack | Do not curl-install; do not cspkg csdfu | `:8787` will not open |
| Master `enable` | Off (`false`) | YAML **or** Web 03 **Allow upload** | Process and page stay up; no send |
| Each dest `enable` | `remote` off | YAML or dest form On + **Save** | Master on still skips that dest |

Web **Pause lookup** is not persisted. After reboot, YAML `enable` wins.

Restart the process only if you edit YAML by hand and did not Save on the page:

```
sudo systemctl restart csdfu
# BusyBox:
sudo /etc/init.d/sa1 restart
```

------

## 3. First time (same four steps as the page)  :id=first

1. Sources → Add → absolute watch dir (e.g. `/var/sa/data/wl`) → Save. Factory already has wl / pv / raw.
2. Destinations → Add (or tap `remote`) → SFTP, host, user, password or key (`/var/sa/conf/csdfu_sftp` is common), remote dir e.g. `/data/csdfu` → turn this dest **On** → Save.
3. 03 **Allow upload**. Master on with dest still off sends nothing.
4. **Scan now**. OK goes to PROC; fail goes to ALARM.

------

## 4. Factory YAML  :id=yaml

Path: `/var/sa/conf/csdfu.yaml` (persist; upgrades do not JSON-merge it). Listen `:8787`. Data dir `/var/sa/data/csdfu` (**not** `/tmp`). Sources `wl` / `pv` / `raw`. One dest `remote` (sftp, off).

Keys omitted in the factory file still have program defaults: `send_timeout_sec=180`, `fail_skip=3`, `dest_break_after=3`, `log_max_kb=256`, `log_keep=2`, `retry=3`, `scan_interval_sec=10`, `min_file_age_sec=8`.

Full fields, protocols, and Web API: [§10](#cli) (`csdfu help`).

------

## 5. Web  :id=web

Page on **:8787** (SA1 signed-in `/csdfu`). Desktop fills the viewport; phone stacks the same panels. Save writes YAML immediately.

| Zone | Content |
|------|---------|
| Mast / KPI | Mode, version, clock, listen; upload/lookup/src/dst/ledger/fail |
| 01 Sources | Watch directories (click a row to edit) |
| 02 Destinations | Upload targets |
| 03 Commands | Allow/stop upload, pause lookup, scan now, trim extras |
| 04 Journal | Recent transfers |
| 05 Faceplate | Dest / source / settings / help |
| 06 Events | Alarm, process, system, operator (ISA-18.2) |

Do not treat 03 as two identical toggle pairs:

| Control | Writes YAML? | Effect |
|---------|--------------|--------|
| Allow upload / Stop upload | Yes (`enable`) | Master switch |
| Pause lookup / Resume | No | Stops auto-scan in memory; scan resumes after restart |
| Scan now | No | One scan immediately; works while paused; clears fail-skip / dest cooldown |
| Trim extras | No | Per-source `max_files`; only files already in every enabled dest ledger |

Settings: scan interval, settle seconds, retries, send timeout, fail-skip, dest circuit, log lines, page password (min 8 chars, never logged). Listen and config path are read-only.

Non-empty YAML `auth_token`: Cookie, or `Authorization: Bearer`, or `?token=`. Empty token: anyone on the LAN can change config.

------

## 6. Scan, ledger, bad 4G  :id=net

  * Skip subdirs, `.temp`, `.part`. Files younger than settle seconds are not sent.
  * Each round sends **newest mtime first**.
  * Ledger key = source path + size + mtime. Staged compress path is not the key. **Mark only after success**.
  * Send timeout default 180 s; a timeout skips remaining in-round retries.
  * After N file failures (default 3) the file backs off (30 s doubling, cap 5 min).
  * After N dest failures (default 3) the dest cools down (60 s doubling, cap 5 min). If every enabled dest is cooling, the round does not scan files.

------

## 7. Protocols  :id=proto

| Protocol | Required |
|----------|----------|
| SFTP / SCP | Host + user + (password or `key_path`) + remote dir |
| FTP / FTPS | Host, user, password; or a full URL. FTPS is explicit TLS |
| HTTP / HTTPS | Full upload URL (multipart `filename=`) |
| Local dir | `path` is an absolute directory on this box |
| S3 / MinIO | Endpoint + bucket + keys |
| WebDAV | URL + user/password |
| SMB | Host + share + user |
| rclone | rclone binary on the box; `remote` like `gdrive:folder`; `rclone_bin` must be exactly `rclone` |

Saved secrets: leave the field empty or `***` to keep.

------

## 8. Disk and logs  :id=disk

Small ARM / BusyBox has **no** logrotate / journald vacuum. csdfu cleans itself under `data_dir` (SA1: `/var/sa/data/csdfu`):

  * In-page log: `log_lines` (default 500)
  * Disk log: `csdfu.log`, default 256 KB × 2 rotated copies
  * Ledger JSON: drop records when the source file is gone; deleting a dest removes `ledger_*.json`
  * Compress stage: delete after send; leftovers older than 2 h are swept

Do not point `data_dir` at `/tmp`.

------

## 9. Troubleshooting  :id=trouble

No data on the page / nothing sent:

1. Master off: 03 **Allow upload** (process up ≠ sending)
2. Dest off, or on but host/dir/user/secret empty
3. No closed files yet, or settle seconds not reached (`.temp` is skipped)
4. Send failed: journal + ALARM (bad password, host down, missing remote dir)
5. Dest circuit cooling: log `skip dest cooldown`. **Scan now** clears the cooldown and tries again.
6. Do not also run sa-di upload, old dtfu, or a second csdfu on the same dir
7. `sa-doctor` `csdfu_count` should be 1 (missing is warn, not fail)

------

## 10. CLI help (same as `csdfu help`)  :id=cli

On the device: `/var/sa/bin/csdfu help` / `--help`. The block below is **exactly** csdfu **0.2.6** `csdfu help` (Chinese). When flags / YAML / Web API change, update the binary `usage()`, the embedded page Help, and this section together.

```
csdfu 0.2.6 — CASSTK 数据文件上传（单二进制，内嵌一页工业运行台）

角色:
  监视本机已经写完的数据文件 → 可选压缩 → 按每目标账本送到一台或多台服务器。
  不嵌解调进程，不抢 80 / 8190 / 7090。核心路径纯 Go，不依赖 rclone。
  SA1 场站随业务 tar 落地 /var/sa/bin/csdfu（csdfu.service 或 BusyBox sa1-supervise）。
  不要单独 cspkg install csdfu，也不要再装一份到 /usr/local/bin。
  服务默认常驻。是否外发看 YAML enable 和网页 03「允许上传 / 停止上传」，不是「没装服务」。

无参数即常驻：扫目录、按账本上传、打开 Web。YAML 与命令行可叠，命令行覆盖文件。
有 YAML 时源/目标可先空着，在网页里新增。无配置文件时必须同时给 -src 和 -dst。

用法:
  csdfu [flags]
  csdfu serve [flags]
  csdfu once [flags]          扫一轮后退出，stdout JSON 状态，无 Web
  csdfu status [flags]        只打印状态，不传输
  csdfu version | -version | --version
  csdfu help | -h | --help

例子:
  csdfu -src /data -dst ./inbox
  csdfu -src /var/sa/data/wl -dst sftp://debian@192.168.3.10/data -password '***' -listen :8787
  csdfu -c /var/sa/conf/csdfu.yaml
  csdfu once -src /data -dst ./inbox
  csdfu status -c /var/sa/conf/csdfu.yaml
  csdfu version

环境变量:
  CSDFU_CONFIG        同 -c / -config

Flags:
  -c, -config FILE    YAML。省略时依次试 ./csdfu.yaml、/etc/csdfu/csdfu.yaml
  -src DIR            监视目录（单源；多源写 YAML sources[]）
  -dst SPEC           目标：本地路径，或 http(s):// ftp(s):// sftp:// scp://
                      s3://bucket/prefix webdav:// smb://host/share/dir local:/path
  -listen ADDR        Web 地址，默认 127.0.0.1:8787（局域网用 :8787）
  -user NAME          覆盖目标用户
  -password PASS      覆盖目标口令（勿写进 shell 历史，优先 YAML）
  -key PATH           SSH 私钥（sftp/scp）
  -pattern GLOB       文件 glob，默认 * ；多个用逗号
  -compress MODE      none|gzip|xz|zstd|tar.gz，默认 none
  -interval SEC       扫描间隔秒，默认 10
  -min-age SEC        文件至少这么老才传，默认 0（SA1 出厂 8）
  -max-files N        源目录配额；只删已送到所有已开目标的旧文件；0=不删
  -retry N            本轮失败再试次数，默认 3。发送超时则本轮不再连打
  -send-timeout SEC   单次发送超时秒，默认 180（差网防卡死）
  -fail-skip N        同一文件连续失败几轮后让路，默认 3
  -dest-break N       目标连续失败几次后短时停连，默认 3
  -data-dir DIR       账本/滚动日志/压缩暂存，默认 ./data（勿用 /tmp、tmpfs）
  -port N             覆盖目标端口
  -endpoint URL       S3/MinIO endpoint
  -bucket NAME        S3 bucket
  -share NAME         SMB 共享名

YAML 顶层:
  enable              总开关。false=进程和网页仍在，不外发。省略=开。SA1 出厂 false
  listen              Web 地址。SA1 出厂 :8787
  data_dir            账本、csdfu.log、压缩暂存。SA1：/var/sa/data/csdfu。不要指到 /tmp
  auth_token          网页口令。空=同网段可改配置。至少 8 位。不进日志
  scan_interval_sec   默认 10
  min_file_age_sec    成熟秒，默认 0；未到龄不传，避免半截文件
  log_lines           网页内存日志行数，默认 500
  retry               默认 3
  send_timeout_sec    默认 180
  fail_skip           默认 3。退避 30s 起倍增，封顶 5 分钟
  dest_break_after    默认 3。冷却 60s 起倍增，封顶 5 分钟。全部已开目标都在冷却时本轮不再扫文件
  log_max_kb          磁盘日志单文件上限 KB，默认 256；<0 不写文件（仍打 stderr）。上限 8192
  log_keep            滚档份数，默认 2（约 768KB 封顶）。进程内滚动，不依赖 logrotate / journald
  sources[] destinations[]

扫描与账本:
  忽略子目录、后缀 .temp / .part。patterns 默认 *。每轮按 mtime 新文件先发。
  账本键 = 源路径 + size + mtime（压缩暂存路径不进账本）。成功才记账。同名覆盖。
  已是 .gz/.xz/.zip/.zst 的不再套 gzip/xz/zstd（tar.gz 仍可打包）。
  口令、密钥、SFTP 私钥内容不打印到日志；网页里显示 ***。

sources[]:
  id enable product path patterns[] compress max_files
  enable 省略=开。网页保存时 path 必须是绝对路径。
  compress: none|gzip|xz|zstd|tar.gz
  max_files: 只删已经在所有已开目标账本里的旧文件；未发出去的不删；0=不删。
  SA1 不要给源设 max_files——设备本地配额走数据保存页。

destinations[]:
  id enable protocol url host port user password key_path path
  share domain endpoint bucket region access_key secret_key
  insecure_tls remote rclone_bin rclone_args[]
  protocol: local|http|https|ftp|ftps|sftp|scp|s3|webdav|smb|rclone
  enable 省略=开。每个目标独立账本。网页删除目标会删对应 ledger_*.json。

协议怎么填:
  sftp / scp     主机 + 用户 +（口令或 key_path）+ 远程目录。SA1 常用 key_path=/var/sa/conf/csdfu_sftp
  ftp / ftps     主机、用户、口令；或完整 url。ftps 为 explicit TLS
  http / https   完整上传 URL（multipart filename=，兼容旧 SA1 curl 上传）
  local          path 为本机绝对目录，文件拷过去
  s3             endpoint + bucket + 密钥；局域网 IP 走 path-style
  webdav         url + 用户/口令
  smb / cifs     host + share + user；path 为共享内子目录。domain 可选
  rclone         机上须另装 rclone；remote 如 gdrive:folder；rclone_bin 只填 rclone（不要填路径）

自身维护（ARM 小盘 / BusyBox 无 logrotate）:
  磁盘日志 data_dir/csdfu.log 写满自动滚（log_max_kb + log_keep）。
  账本 JSON：源文件从盘上消失后删记录。
  压缩暂存 data_dir/stage/：传完即删，残留超过 2 小时清掉。
  *.tmp 半成品在扫描时清。崩溃留下的 ledger_*.json.tmp 启动时回收。

Web（listen 地址；电脑铺满视口，手机纵向叠同样面板）:
  现场以网页配置为主，点保存即写回 YAML，不必重启进程。
  右上「帮助」与操作盘「帮助」是完整说明（第一次、协议、差网、磁盘、排障）。
  03 三块（不要当成两对相同的开关）:
    允许上传 / 停止上传 = YAML enable（总开关，刷新网页仍有效）
    暂停查找 / 恢复查找 = 只停内存自动扫，不写文件；进程重启后又会扫
    现在扫一遍 = 马上找一次，暂停时也能用；并清掉文件让路/目标熔断冷却再试一轮；总开关关着则不会送文件
    删除超额旧文件 = 按源 max_files，只删已送到所有已开目标的文件；配额 0 按钮禁用
  底栏事件按 ISA-18.2 / IEC 62682：报警 ALARM、过程 PROC、系统 SYS、操作 OPER。
  扫描周期不进报警。失败/重试进报警，成功送达进过程，允许/停止上传进操作。
  网页口令非空则要登录（Cookie）；也可 Authorization: Bearer 或 ?token=
  未设口令时局域网可改配置，可在操作盘「设置」里加上（至少 8 位）。
  GET  /api/status  /api/logs  /api/session
  GET  /api/logs  lines[] 与 entries[]（class: alarm|warn|proc|sys|oper）
  POST /api/login {"token":"..."}   POST /api/logout
  POST /api/enable  {"enable":true|false}
  POST /api/dest    新增或更新目标。口令 "***" 或空=保持
  POST /api/dest/delete  {"id":"..."}
  POST /api/source  新增或更新源   POST /api/source/delete {"id":"..."}
  POST /api/settings 扫描间隔/成熟时间/重试/发送超时/失败让路/目标熔断/日志行数/auth_token
  POST /api/pause /api/resume /api/scan /api/retain

排障（网页没数据 / 不外发）:
  1. 总开关停：03 点「允许上传」（进程在、网页在，不等于在传）
  2. 目标关，或已开但没填主机/目录/用户/口令或私钥
  3. 源目录没有已经写完的文件，或未到成熟秒（.temp 不传）
  4. 传输失败：看传输记录和底栏「报警」（口令、主机不通、目录不存在最常见）
  5. 目标熔断冷却中：日志 skip dest cooldown。点「现在扫一遍」会清冷却再试一轮
  6. 同一数据目录不要再跑 sa-di 上传、旧 dtfu、或第二个 csdfu
  7. 只在手工改 YAML 且没用网页保存时才需要重启:
       sudo systemctl restart csdfu
       BusyBox: sudo /etc/init.d/sa1 restart

退出码:
  0   成功（含 -h）
  1   运行/配置错误
  2   用法错误

linux armv7l：GOARM=7 静态编译。核心路径不依赖 rclone。
```

------

## Changelog :id=changelog

|Version|Date|Description|
|:--:|----|----------|
|2.5|09/11/2026|1. Scan now clears cooldown; zones 01/02. SA1 **2.6.6.30** bundles csdfu 0.2.6.|
|2.4|09/11/2026|1. Align with csdfu 0.2.6 `csdfu help` and the Web Help: 03 labels, bad-4G, self-capped logs, protocol fields, troubleshooting, full Web API.|
|2.3|09/09/2026|1. SA1 pack only — do not manage csdfu with a separate cspkg product. Device CLI is `/var/sa/bin/csdfu`.|
|2.2|09/09/2026|1. CLI/Web docs. Service installed; sending is YAML `enable` and the page (no restart).|
|2.1|09/09/2026|1. Ships in the SA1 tar with sa-di / sa-web supervise.|
|2.0|09/09/2026|1. Replaced dtfu with csdfu. sa-di upload retired. SA1 reserved `/csdfu`.|
|1.4|04/30/2023|1. Historical dtfu supervisor fix.|

------

[Top](#top)
