stash-cli 0.8.1

A local store for pipeline output and ad hoc file snapshots
Documentation
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
use crate::preview::build_preview_data;
use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
use serde_json::Value;
use std::collections::BTreeMap;
use std::error::Error as StdError;
use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

mod attr;
mod cache;
mod push;

pub use push::{push_from_reader, tee_from_reader_partial};

pub const SHORT_ID_LEN: usize = 8;
pub const MIN_ID_LEN: usize = 6;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub struct PartialSavedError {
    pub id: String,
    pub cause: std::io::Error,
    pub signal: Option<i32>,
}

impl std::fmt::Display for PartialSavedError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "partial entry saved as \"{}\": {}",
            self.id,
            self.cause
        )
    }
}

impl StdError for PartialSavedError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        Some(&self.cause)
    }
}

pub struct UtcDateTime {
    pub year: i32,
    pub month: u32,
    pub day: u32,
    pub hour: u32,
    pub min: u32,
    pub sec: u32,
}

#[derive(
    Clone,
    Debug,
    SerdeSerialize,
    SerdeDeserialize,
    rkyv::Archive,
    rkyv::Serialize,
    rkyv::Deserialize,
)]
pub struct Meta {
    pub id: String,
    pub ts: String,
    pub size: i64,
    pub preview: String,
    pub attrs: BTreeMap<String, String>,
}

impl Meta {
    // IDs are always stored lowercase (created via new_ulid which calls to_ascii_lowercase).
    pub fn short_id(&self) -> &str {
        &self.id[self.id.len().saturating_sub(SHORT_ID_LEN)..]
    }

    pub fn display_id(&self) -> &str {
        &self.id
    }

    pub fn to_json_value(&self, include_preview: bool) -> Value {
        let capacity =
            3 + self.attrs.len() + usize::from(include_preview && !self.preview.is_empty());
        let mut map = serde_json::Map::with_capacity(capacity);
        map.insert("id".into(), Value::String(self.id.clone()));
        map.insert("ts".into(), Value::String(self.ts.clone()));
        map.insert("size".into(), Value::Number(self.size.into()));
        for (k, v) in &self.attrs {
            map.insert(k.clone(), Value::String(v.clone()));
        }
        if include_preview && !self.preview.is_empty() {
            map.insert("preview".into(), Value::String(self.preview.clone()));
        }
        Value::Object(map)
    }
}

// ---------------------------------------------------------------------------
// Entry selection / filtering
// ---------------------------------------------------------------------------

#[derive(Clone, Debug, Default)]
pub struct MetaSelection {
    pub show_all: bool,
    pub display_tags: Vec<String>,
    pub filter_tags: Vec<String>,
    pub filter_tag_values: Vec<(String, String)>,
}

pub fn parse_meta_selection(values: &[String], show_all: bool) -> io::Result<MetaSelection> {
    let mut out = MetaSelection {
        show_all,
        display_tags: Vec::with_capacity(values.len()),
        filter_tags: Vec::with_capacity(values.len()),
        filter_tag_values: Vec::with_capacity(values.len()),
    };
    let mut seen_display = std::collections::HashSet::with_capacity(values.len());
    let mut seen_filter = std::collections::HashSet::with_capacity(values.len());
    let mut seen_filter_values = std::collections::HashSet::with_capacity(values.len());
    for value in values {
        if value.contains(',') || value.trim().is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "--attr accepts name, name=value, +name, ++name, or ++name=value and is repeatable",
            ));
        }
        if let Some(key) = value.strip_prefix("++") {
            if key.is_empty() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "--attr filter+display must be ++name",
                ));
            }
            if let Some((filter_key, filter_value)) = key.split_once('=') {
                if filter_key.is_empty() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        "--attr filter+display value form must be ++name=value",
                    ));
                }
                if seen_display.insert(filter_key.to_string()) {
                    out.display_tags.push(filter_key.to_string());
                }
                let pair = (filter_key.to_string(), filter_value.to_string());
                if seen_filter_values.insert(pair.clone()) {
                    out.filter_tag_values.push(pair);
                }
                continue;
            }
            if seen_display.insert(key.to_string()) {
                out.display_tags.push(key.to_string());
            }
            if seen_filter.insert(key.to_string()) {
                out.filter_tags.push(key.to_string());
            }
        } else if let Some(key) = value.strip_prefix('+') {
            if key.is_empty() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "--attr display must be +name",
                ));
            }
            if seen_display.insert(key.to_string()) {
                out.display_tags.push(key.to_string());
            }
        } else if let Some((key, attr_value)) = value.split_once('=') {
            if key.is_empty() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "--attr value filter must be name=value",
                ));
            }
            let pair = (key.to_string(), attr_value.to_string());
            if seen_filter_values.insert(pair.clone()) {
                out.filter_tag_values.push(pair);
            }
        } else if seen_filter.insert(value.to_string()) {
            out.filter_tags.push(value.clone());
        }
    }
    Ok(out)
}

pub fn matches_meta(attrs: &BTreeMap<String, String>, sel: &MetaSelection) -> bool {
    sel.filter_tags.iter().all(|tag| attrs.contains_key(tag))
        && sel
            .filter_tag_values
            .iter()
            .all(|(key, value)| attrs.get(key) == Some(value))
}

// ---------------------------------------------------------------------------
// Directory / path helpers
// ---------------------------------------------------------------------------

fn cached_base_dir() -> &'static PathBuf {
    use std::sync::OnceLock;
    static BASE: OnceLock<PathBuf> = OnceLock::new();
    BASE.get_or_init(|| match std::env::var("STASH_DIR") {
        Ok(dir) if !dir.trim().is_empty() => PathBuf::from(dir),
        _ => {
            let home = std::env::var("HOME")
                .map(PathBuf::from)
                .expect("HOME not set");
            home.join(".stash")
        }
    })
}

pub fn base_dir() -> io::Result<PathBuf> {
    Ok(cached_base_dir().clone())
}

pub fn data_dir() -> io::Result<PathBuf> {
    Ok(cached_base_dir().join("data"))
}

pub fn attr_dir() -> io::Result<PathBuf> {
    Ok(cached_base_dir().join("attr"))
}

fn cache_dir() -> io::Result<PathBuf> {
    Ok(cached_base_dir().join("cache"))
}

fn list_cache_path() -> io::Result<PathBuf> {
    Ok(cache_dir()?.join("list.cache"))
}

pub fn entry_dir(id: &str) -> io::Result<PathBuf> {
    Ok(cached_base_dir().join(id))
}

pub fn entry_data_path(id: &str) -> io::Result<PathBuf> {
    Ok(data_dir()?.join(id.to_ascii_lowercase()))
}

pub fn entry_attr_path(id: &str) -> io::Result<PathBuf> {
    Ok(attr_dir()?.join(id.to_ascii_lowercase()))
}

fn tmp_dir() -> io::Result<PathBuf> {
    Ok(cached_base_dir().join("tmp"))
}

pub fn init() -> io::Result<()> {
    let base = cached_base_dir();
    fs::create_dir_all(base.join("data"))?;
    fs::create_dir_all(base.join("attr"))?;
    fs::create_dir_all(base.join("tmp"))?;
    fs::create_dir_all(base.join("cache"))?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Query API
// ---------------------------------------------------------------------------

pub fn list_entry_ids() -> io::Result<Vec<String>> {
    let attrs = attr_dir()?;
    let read_dir = match fs::read_dir(&attrs) {
        Ok(rd) => rd,
        Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(err) => return Err(err),
    };
    let mut ids: Vec<String> = read_dir
        .filter_map(|item| item.ok())
        .map(|item| item.file_name().to_string_lossy().into_owned())
        .collect();
    ids.sort_unstable();
    ids.reverse();
    Ok(ids)
}

pub fn list() -> io::Result<Vec<Meta>> {
    if let Ok(items) = cache::read_list_cache() {
        return Ok(items);
    }
    let entry_ids = list_entry_ids()?;
    let mut out = Vec::with_capacity(entry_ids.len());
    for id in entry_ids {
        if let Ok(meta) = get_meta(&id) {
            out.push(meta);
        }
    }
    cache::write_list_cache(&out)?;
    Ok(out)
}

pub fn all_attr_keys() -> io::Result<Vec<(String, usize)>> {
    if let Ok(keys) = cache::read_attr_keys() {
        return Ok(keys);
    }
    let items = list()?;
    // Fall back to rebuilding from the list. write_list_cache already stored
    // the attr key index, so the next call will hit the cache.
    cache::write_list_cache(&items)?;
    cache::read_attr_keys()
}

pub fn newest() -> io::Result<Meta> {
    list()?
        .into_iter()
        .next()
        .ok_or_else(|| io::Error::other("stash is empty"))
}

pub fn nth_newest(n: usize) -> io::Result<Meta> {
    if n == 0 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "n must be >= 1",
        ));
    }
    let items = list()?;
    items
        .into_iter()
        .nth(n - 1)
        .ok_or_else(|| io::Error::other("entry index out of range"))
}

pub fn older_than_ids(id: &str) -> io::Result<Vec<String>> {
    let items = list()?;
    for (idx, item) in items.iter().enumerate() {
        if item.id == id {
            return Ok(items[idx + 1..].iter().map(|m| m.id.clone()).collect());
        }
    }
    Err(io::Error::new(io::ErrorKind::NotFound, "entry not found"))
}

pub fn newer_than_ids(id: &str) -> io::Result<Vec<String>> {
    let items = list()?;
    for (idx, item) in items.iter().enumerate() {
        if item.id == id {
            return Ok(items[..idx].iter().map(|m| m.id.clone()).collect());
        }
    }
    Err(io::Error::new(io::ErrorKind::NotFound, "entry not found"))
}

pub fn resolve(input: &str) -> io::Result<String> {
    let raw = input.trim();
    if raw.is_empty() {
        return newest().map(|m| m.id);
    }
    if let Some(rest) = raw.strip_prefix('@') {
        let n = rest
            .parse::<usize>()
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid stack ref"))?;
        return nth_newest(n).map(|m| m.id);
    }
    let lower = raw.to_ascii_lowercase();
    if lower.bytes().all(|c| c.is_ascii_digit()) {
        let n = lower
            .parse::<usize>()
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid index"))?;
        return nth_newest(n).map(|m| m.id);
    }
    if lower.len() < MIN_ID_LEN {
        return Err(io::Error::new(io::ErrorKind::InvalidInput, "id too short"));
    }
    let ids = list_entry_ids()?;
    if ids.is_empty() {
        return Err(io::Error::new(io::ErrorKind::NotFound, "stash is empty"));
    }
    if let Some(id) = ids.iter().find(|id| **id == lower) {
        return Ok(id.clone());
    }
    let mut prefix_match: Option<&String> = None;
    let mut suffix_match: Option<&String> = None;
    let mut prefix_ambig = false;
    let mut suffix_ambig = false;
    for id in &ids {
        if id.starts_with(&lower) {
            if prefix_match.is_some() {
                prefix_ambig = true;
            } else {
                prefix_match = Some(id);
            }
        }
        if id.ends_with(&lower) {
            if suffix_match.is_some() {
                suffix_ambig = true;
            } else {
                suffix_match = Some(id);
            }
        }
    }
    if let Some(id) = prefix_match {
        if !prefix_ambig {
            return Ok(id.clone());
        }
        return Err(io::Error::other("ambiguous id"));
    }
    if let Some(id) = suffix_match {
        if !suffix_ambig {
            return Ok(id.clone());
        }
        return Err(io::Error::other("ambiguous id"));
    }
    Err(io::Error::new(io::ErrorKind::NotFound, "entry not found"))
}

pub fn get_meta(id: &str) -> io::Result<Meta> {
    let path = entry_attr_path(id)?;
    let data = fs::read_to_string(path)?;
    attr::parse_attr_file(&data).map_err(io::Error::other)
}

pub fn write_meta(id: &str, meta: &Meta) -> io::Result<()> {
    let result = fs::write(entry_attr_path(id)?, attr::encode_attr(meta));
    if result.is_ok() {
        cache::invalidate_list_cache();
    }
    result
}

pub fn set_attrs(id: &str, attrs: &BTreeMap<String, String>) -> io::Result<()> {
    let mut meta = get_meta(id)?;
    for (k, v) in attrs {
        meta.attrs.insert(k.clone(), v.clone());
    }
    write_meta(id, &meta)
}

pub fn unset_attrs(id: &str, keys: &[String]) -> io::Result<()> {
    let mut meta = get_meta(id)?;
    for key in keys {
        meta.attrs.remove(key);
    }
    write_meta(id, &meta)
}

pub fn cat_to_writer<W: Write>(id: &str, mut writer: W) -> io::Result<()> {
    let file = File::open(entry_data_path(id)?)?;
    let mut reader = io::BufReader::with_capacity(65536, file);
    io::copy(&mut reader, &mut writer)?;
    Ok(())
}

pub fn remove(id: &str) -> io::Result<()> {
    let data_result = fs::remove_file(entry_data_path(id)?);
    if let Err(ref e) = data_result {
        if e.kind() != io::ErrorKind::NotFound {
            return data_result;
        }
    }
    let attr_result = fs::remove_file(entry_attr_path(id)?);
    if let Err(ref e) = attr_result {
        if e.kind() != io::ErrorKind::NotFound {
            return attr_result;
        }
    }
    cache::invalidate_list_cache();
    Ok(())
}

// Called by io::run_read_loop and io::save_or_abort_partial via super::
fn finalize_saved_entry(
    id: String,
    data_path: PathBuf,
    sample: &[u8],
    total: i64,
    attrs: BTreeMap<String, String>,
) -> io::Result<String> {
    let meta = Meta {
        id: id.clone(),
        ts: now_rfc3339ish()?,
        size: total,
        preview: build_preview_data(sample, sample.len()),
        attrs,
    };
    let tmp = tmp_dir()?;
    let attr_path = tmp.join(format!("{id}.attr"));
    fs::write(&attr_path, attr::encode_attr(&meta))?;
    fs::rename(&data_path, entry_data_path(&id)?)?;
    fs::rename(&attr_path, entry_attr_path(&id)?)?;
    cache::invalidate_list_cache();
    Ok(id)
}

// ---------------------------------------------------------------------------
// Utilities
// ---------------------------------------------------------------------------

pub fn human_size(n: i64) -> String {
    match n {
        n if n < 1024 => format!("{n}B"),
        n if n < 1024 * 1024 => format!("{:.1}K", n as f64 / 1024.0),
        n if n < 1024 * 1024 * 1024 => format!("{:.1}M", n as f64 / (1024.0 * 1024.0)),
        n => format!("{:.1}G", n as f64 / (1024.0 * 1024.0 * 1024.0)),
    }
}

fn now_rfc3339ish() -> io::Result<String> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(io::Error::other)?;
    let secs = now.as_secs() as i64;
    let nanos = now.subsec_nanos();
    let dt = unix_to_utc(secs);
    Ok(format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{nanos:09}Z",
        dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec,
    ))
}

pub fn unix_to_utc(secs: i64) -> UtcDateTime {
    let days = secs.div_euclid(86_400);
    let rem = secs.rem_euclid(86_400);
    let (year, month, day) = civil_from_days(days);
    UtcDateTime {
        year,
        month,
        day,
        hour: (rem / 3600) as u32,
        min: ((rem % 3600) / 60) as u32,
        sec: (rem % 60) as u32,
    }
}

fn civil_from_days(days: i64) -> (i32, u32, u32) {
    let z = days + 719468;
    let era = if z >= 0 { z } else { z - 146096 } / 146097;
    let doe = z - era * 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = mp + if mp < 10 { 3 } else { -9 };
    let year = y + if m <= 2 { 1 } else { 0 };
    (year as i32, m as u32, d as u32)
}

fn new_ulid() -> io::Result<String> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(io::Error::other)?
        .as_millis() as u64;
    let mut bytes = [0u8; 16];
    for (i, byte) in bytes.iter_mut().enumerate().take(6) {
        *byte = ((now >> (8 * (5 - i))) & 0xff) as u8;
    }
    let mut rand = File::open("/dev/urandom")?;
    rand.read_exact(&mut bytes[6..])?;
    Ok(encode_ulid(bytes).to_ascii_lowercase())
}

fn encode_ulid(bytes: [u8; 16]) -> String {
    const ALPHABET: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
    let mut value = 0u128;
    for byte in bytes {
        value = (value << 8) | byte as u128;
    }
    let mut out = [0u8; 26];
    for i in (0..26).rev() {
        out[i] = ALPHABET[(value & 0x1f) as usize];
        value >>= 5;
    }
    // SAFETY: out contains only bytes from ALPHABET, which is ASCII-only
    unsafe { String::from_utf8_unchecked(out.to_vec()) }
}

pub fn add_filename_attr(path: &Path, attrs: &mut BTreeMap<String, String>) {
    if let Some(name) = path.file_name().and_then(|s| s.to_str()) {
        attrs.insert("filename".into(), name.into());
    }
}