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
use crate::{Spool, SpoolEntry, SpoolId};
use anyhow::Context;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use flume::Sender;
use std::fs::File;
use std::io::Write;
use std::os::fd::AsRawFd;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tempfile::NamedTempFile;
use tokio::runtime::Handle;

pub struct LocalDiskSpool {
    path: PathBuf,
    flush: bool,
    _pid_file: File,
    runtime: Handle,
}

impl LocalDiskSpool {
    pub fn new(path: &Path, flush: bool, runtime: Handle) -> anyhow::Result<Self> {
        let pid_file_path = path.join("lock");
        let _pid_file = lock_pid_file(pid_file_path)?;

        Self::create_dir_structure(path)?;

        Ok(Self {
            path: path.to_path_buf(),
            flush,
            _pid_file,
            runtime,
        })
    }

    fn create_dir_structure(path: &Path) -> anyhow::Result<()> {
        std::fs::create_dir_all(path.join("new"))?;
        std::fs::create_dir_all(path.join("data"))?;
        Ok(())
    }

    fn compute_path(&self, id: SpoolId) -> PathBuf {
        id.compute_path(&self.path.join("data"))
    }

    fn cleanup_dirs(path: &Path) {
        let new_dir = path.join("new");
        for entry in jwalk::WalkDir::new(new_dir) {
            if let Ok(entry) = entry {
                if !entry.file_type().is_file() {
                    continue;
                }
                let path = entry.path();
                if let Err(err) = std::fs::remove_file(&path) {
                    eprintln!("Failed to remove {path:?}: {err:#}");
                }
            }
        }

        let data_dir = path.join("data");
        Self::cleanup_data(&data_dir);
    }

    fn cleanup_data(data_dir: &Path) {
        for entry in jwalk::WalkDir::new(data_dir) {
            if let Ok(entry) = entry {
                if !entry.file_type().is_dir() {
                    continue;
                }
                let path = entry.path();
                // Speculatively try removing the directory; it will
                // only succeed if it is empty. We don't need to check
                // for that first, and we don't care if it fails.
                std::fs::remove_dir(&path).ok();
            }
        }
    }
}

#[async_trait]
impl Spool for LocalDiskSpool {
    async fn load(&self, id: SpoolId) -> anyhow::Result<Vec<u8>> {
        let path = self.compute_path(id);
        tokio::fs::read(&path)
            .await
            .with_context(|| format!("failed to load {id} from {path:?}"))
    }

    async fn remove(&self, id: SpoolId) -> anyhow::Result<()> {
        let path = self.compute_path(id);
        tokio::fs::remove_file(&path)
            .await
            .with_context(|| format!("failed to remove {id} from {path:?}"))
    }

    async fn store(
        &self,
        id: SpoolId,
        data: Arc<Box<[u8]>>,
        force_sync: bool,
    ) -> anyhow::Result<()> {
        let path = self.compute_path(id);
        let new_dir = self.path.join("new");
        let flush = force_sync || self.flush;
        tokio::task::Builder::new()
            .name("LocalDiskSpool store")
            .spawn_blocking_on(
                move || {
                    let mut temp = NamedTempFile::new_in(new_dir).with_context(|| {
                        format!("failed to create a temporary file to store {id}")
                    })?;

                    temp.write_all(&data)
                        .with_context(|| format!("failed to write data for {id}"))?;

                    if flush {
                        temp.as_file_mut()
                            .sync_data()
                            .with_context(|| format!("failed to sync data for {id}"))?;
                    }

                    std::fs::create_dir_all(path.parent().unwrap()).with_context(|| {
                        format!("failed to create dir structure for {id} {path:?}")
                    })?;

                    temp.persist(&path).with_context(|| {
                        format!("failed to move temp file for {id} to {path:?}")
                    })?;
                    Ok(())
                },
                &self.runtime,
            )?
            .await?
    }

    fn enumerate(
        &self,
        sender: Sender<SpoolEntry>,
        start_time: DateTime<Utc>,
    ) -> anyhow::Result<()> {
        let path = self.path.clone();
        tokio::task::Builder::new()
            .name("LocalDiskSpool enumerate")
            .spawn_blocking_on(
                move || -> anyhow::Result<()> {
                    Self::cleanup_dirs(&path);

                    for entry in jwalk::WalkDir::new(path.join("data")) {
                        if let Ok(entry) = entry {
                            if !entry.file_type().is_file() {
                                continue;
                            }
                            let path = entry.path();
                            if let Some(id) = SpoolId::from_path(&path) {
                                if id.created() >= start_time {
                                    // Entries created since we started must have
                                    // landed there after we started and are thus
                                    // not eligible for discovery via enumeration
                                    continue;
                                }
                                match std::fs::read(&path) {
                                    Ok(data) => sender
                                        .send(SpoolEntry::Item { id, data })
                                        .map_err(|err| {
                                            anyhow::anyhow!("failed to send data for {id}: {err:#}")
                                        })?,
                                    Err(err) => sender
                                        .send(SpoolEntry::Corrupt {
                                            id,
                                            error: format!("{err:#}"),
                                        })
                                        .map_err(|err| {
                                            anyhow::anyhow!(
                                                "failed to send SpoolEntry for {id}: {err:#}"
                                            )
                                        })?,
                                };
                            } else {
                                eprintln!("{} is not a spool id", path.display());
                            }
                        }
                    }
                    anyhow::Result::Ok(())
                },
                &self.runtime,
            )?;
        Ok(())
    }

    async fn cleanup(&self) -> anyhow::Result<()> {
        let data_dir = self.path.join("data");
        Ok(tokio::task::Builder::new()
            .name("LocalDiskSpool cleanup")
            .spawn_blocking_on(
                move || {
                    Self::cleanup_data(&data_dir);
                },
                &self.runtime,
            )?
            .await?)
    }

    async fn shutdown(&self) -> anyhow::Result<()> {
        Ok(())
    }

    async fn advise_low_memory(&self) -> anyhow::Result<isize> {
        Ok(0)
    }
}

/// Set the sticky bit on path.
/// This prevents tmpwatch from removing the lock file.
pub fn set_sticky_bit(path: &Path) {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Ok(metadata) = path.metadata() {
            let mut perms = metadata.permissions();
            let mode = perms.mode();
            perms.set_mode(mode | libc::S_ISVTX as u32);
            let _ = std::fs::set_permissions(&path, perms);
        }
    }

    #[cfg(windows)]
    {
        let _ = path;
    }
}

fn lock_pid_file(pid_file: PathBuf) -> anyhow::Result<std::fs::File> {
    let pid_file_dir = pid_file
        .parent()
        .ok_or_else(|| anyhow::anyhow!("{} has no parent?", pid_file.display()))?;
    std::fs::create_dir_all(&pid_file_dir).with_context(|| {
        format!(
            "while creating directory structure: {}",
            pid_file_dir.display()
        )
    })?;
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .write(true)
        .open(&pid_file)
        .with_context(|| format!("opening pid file {}", pid_file.display()))?;
    set_sticky_bit(&pid_file);
    let res = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
    if res != 0 {
        let err = std::io::Error::last_os_error();

        let owner = match std::fs::read_to_string(&pid_file) {
            Ok(pid) => format!(". Owned by pid {}.", pid.trim()),
            Err(_) => "".to_string(),
        };

        anyhow::bail!(
            "unable to lock pid file {}: {}{owner}",
            pid_file.display(),
            err
        );
    }

    unsafe { libc::ftruncate(file.as_raw_fd(), 0) };
    writeln!(file, "{}", unsafe { libc::getpid() }).ok();

    Ok(file)
}

#[cfg(test)]
mod test {
    use super::*;

    #[tokio::test]
    async fn basic_spool() -> anyhow::Result<()> {
        let location = tempfile::tempdir()?;
        let spool = LocalDiskSpool::new(&location.path(), false, Handle::current())?;
        let data_dir = location.path().join("data");

        {
            let id1 = SpoolId::new();
            let id1_path = id1.compute_path(&data_dir).display().to_string();

            // Can't load an entry that doesn't exist
            assert_eq!(
                format!("{:#}", spool.load(id1).await.unwrap_err()),
                format!(
                    "failed to load {id1} from \"{id1_path}\": \
                    No such file or directory (os error 2)"
                )
            );
        }

        // Insert some entries
        let mut ids = vec![];
        for i in 0..100 {
            let id = SpoolId::new();
            spool
                .store(
                    id,
                    Arc::new(format!("I am {i}").as_bytes().to_vec().into_boxed_slice()),
                    false,
                )
                .await?;
            ids.push(id);
        }

        // Verify that we can load those entries
        for (i, &id) in ids.iter().enumerate() {
            let data = spool.load(id).await?;
            let text = String::from_utf8(data)?;
            assert_eq!(text, format!("I am {i}"));
        }

        {
            // Verify that we can enumerate them
            let (tx, rx) = flume::bounded(32);
            spool.enumerate(tx, Utc::now())?;
            let mut count = 0;

            while let Ok(item) = rx.recv_async().await {
                match item {
                    SpoolEntry::Item { id, data } => {
                        let i = ids
                            .iter()
                            .position(|&item| item == id)
                            .ok_or_else(|| anyhow::anyhow!("{id} not found in ids!"))?;

                        let text = String::from_utf8(data)?;
                        assert_eq!(text, format!("I am {i}"));

                        spool.remove(id).await?;
                        // Can't load an entry that we just removed
                        let id_path = id.compute_path(&data_dir).display().to_string();
                        assert_eq!(
                            format!("{:#}", spool.load(id).await.unwrap_err()),
                            format!(
                                "failed to load {id} from \"{id_path}\": \
                                No such file or directory (os error 2)"
                            )
                        );
                        count += 1;
                    }
                    SpoolEntry::Corrupt { id, error } => {
                        anyhow::bail!("Corrupt: {id}: {error}");
                    }
                }
            }

            assert_eq!(count, 100);
        }

        // Now that we've removed the files, try enumerating again.
        // We expect to receive no entries.
        // Do it a couple of times to verify that none of the cleanup
        // stuff that happens in enumerate breaks the directory
        // structure
        for _ in 0..2 {
            // Verify that we can enumerate them
            let (tx, rx) = flume::bounded(32);
            spool.enumerate(tx, Utc::now())?;
            let mut unexpected = vec![];

            while let Ok(item) = rx.recv_async().await {
                match item {
                    SpoolEntry::Item { id, .. } | SpoolEntry::Corrupt { id, .. } => {
                        unexpected.push(id)
                    }
                }
            }

            assert_eq!(unexpected.len(), 0);
        }

        Ok(())
    }
}