asched-core 0.2.0

Reusable local project routine scheduler, daemon, persistence, and typed client
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
//! Generic project registry.
//! ref: README.md#architecture

use crate::routine::store::{atomic_toml, read_text_limited};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs::{self, File, OpenOptions};
use std::os::fd::AsRawFd;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, OnceLock};

const REGISTRY_VERSION: u32 = 1;
static REGISTRY_WRITE_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Project {
    pub name: String,
    pub working_dir: PathBuf,
}

impl Project {
    pub fn validated(mut self) -> Result<Self, RegistryError> {
        self.name = self.name.trim().to_string();
        validate_name(&self.name)?;
        self.working_dir = fs::canonicalize(&self.working_dir).map_err(|error| {
            RegistryError::Validation(format!(
                "cannot resolve working directory {}: {error}",
                self.working_dir.display()
            ))
        })?;
        if !self.working_dir.is_dir() {
            return Err(RegistryError::Validation(format!(
                "{} is not a directory",
                self.working_dir.display()
            )));
        }
        Ok(self)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
// ^ [[Project Registry and Scheduling Allowlist]]
pub struct ProjectRegistry {
    pub version: u32,
    pub revision: u64,
    #[serde(default)]
    pub projects: Vec<Project>,
}

impl Default for ProjectRegistry {
    fn default() -> Self {
        Self {
            version: REGISTRY_VERSION,
            revision: 0,
            projects: Vec::new(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct RegistryStore {
    root: PathBuf,
}

impl RegistryStore {
    pub fn new(root: PathBuf) -> Self {
        Self { root }
    }

    pub fn default_root() -> Result<PathBuf, RegistryError> {
        if let Some(path) = std::env::var_os("ASCHED_ROOT") {
            return Ok(PathBuf::from(path));
        }
        dirs::config_dir()
            .map(|path| path.join("asched"))
            .ok_or_else(|| RegistryError::Unavailable("no config directory".into()))
    }

    pub fn root(&self) -> &Path {
        &self.root
    }

    pub fn path(&self) -> PathBuf {
        self.root.join("projects.toml")
    }

    pub fn load(&self) -> Result<ProjectRegistry, RegistryError> {
        let path = self.path();
        if !path.exists() {
            return Ok(ProjectRegistry::default());
        }
        let text = read_text_limited(&path)?;
        let registry: ProjectRegistry = toml::from_str(&text)
            .map_err(|error| RegistryError::Corrupt(format!("{}: {error}", path.display())))?;
        validate_registry(registry)
    }

    pub fn add(
        &self,
        expected_revision: u64,
        project: Project,
    ) -> Result<ProjectRegistry, RegistryError> {
        self.modify(expected_revision, |registry| {
            let project = project.validated()?;
            if registry
                .projects
                .iter()
                .any(|item| item.name == project.name)
            {
                return Err(RegistryError::Duplicate(project.name));
            }
            if let Some(existing) = registry
                .projects
                .iter()
                .find(|item| item.working_dir == project.working_dir)
            {
                return Err(RegistryError::Validation(format!(
                    "{} is already registered as project '{}'",
                    project.working_dir.display(),
                    existing.name
                )));
            }
            registry.projects.push(project);
            registry
                .projects
                .sort_by(|left, right| left.name.cmp(&right.name));
            Ok(())
        })
    }

    pub fn remove(
        &self,
        expected_revision: u64,
        name: &str,
    ) -> Result<ProjectRegistry, RegistryError> {
        self.modify(expected_revision, |registry| {
            let before = registry.projects.len();
            registry.projects.retain(|project| project.name != name);
            if registry.projects.len() == before {
                return Err(RegistryError::NotFound(name.to_string()));
            }
            Ok(())
        })
    }

    pub fn merge(
        &self,
        expected_revision: u64,
        projects: Vec<Project>,
    ) -> Result<ProjectRegistry, RegistryError> {
        self.modify(expected_revision, |registry| merge_into(registry, projects))
    }

    pub fn select(
        &self,
        names: &[String],
        filter: Option<&str>,
    ) -> Result<Vec<Project>, RegistryError> {
        let registry = self.load()?;
        let requested = names.iter().collect::<HashSet<_>>();
        let missing = names
            .iter()
            .filter(|name| {
                !registry
                    .projects
                    .iter()
                    .any(|project| &project.name == *name)
            })
            .cloned()
            .collect::<Vec<_>>();
        if !missing.is_empty() {
            return Err(RegistryError::NotFound(missing.join(", ")));
        }
        let filter = filter
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(str::to_lowercase);
        Ok(registry
            .projects
            .into_iter()
            .filter(|project| requested.is_empty() || requested.contains(&project.name))
            .filter(|project| {
                filter.as_deref().is_none_or(|value| {
                    project.name.to_lowercase().contains(value)
                        || project
                            .working_dir
                            .to_string_lossy()
                            .to_lowercase()
                            .contains(value)
                })
            })
            .collect())
    }

    fn modify(
        &self,
        expected_revision: u64,
        update: impl FnOnce(&mut ProjectRegistry) -> Result<(), RegistryError>,
    ) -> Result<ProjectRegistry, RegistryError> {
        // ^ Registry writes bypass the daemon; keep file locking, revision checks, and atomic_toml together.
        let _guard = self.exclusive_lock()?;
        self.modify_locked(expected_revision, update)
    }

    pub(crate) fn merge_locked(
        &self,
        expected_revision: u64,
        projects: Vec<Project>,
    ) -> Result<ProjectRegistry, RegistryError> {
        self.modify_locked(expected_revision, |registry| merge_into(registry, projects))
    }

    pub(crate) fn exclusive_lock(&self) -> Result<RegistryWriteGuard, RegistryError> {
        let lock = REGISTRY_WRITE_LOCK.get_or_init(|| Mutex::new(()));
        let process = lock
            .lock()
            .map_err(|_| RegistryError::Io("project registry lock poisoned".into()))?;
        let file = RegistryFileLock::acquire(&self.root)?;
        Ok(RegistryWriteGuard {
            _process: process,
            _file: file,
        })
    }

    fn modify_locked(
        &self,
        expected_revision: u64,
        update: impl FnOnce(&mut ProjectRegistry) -> Result<(), RegistryError>,
    ) -> Result<ProjectRegistry, RegistryError> {
        let mut registry = self.load()?;
        if registry.revision != expected_revision {
            return Err(RegistryError::Conflict {
                expected: expected_revision,
                actual: registry.revision,
            });
        }
        update(&mut registry)?;
        registry.revision = registry
            .revision
            .checked_add(1)
            .ok_or_else(|| RegistryError::Corrupt("registry revision overflow".into()))?;
        atomic_toml(&self.path(), &registry)
            .map_err(|error| RegistryError::Io(error.to_string()))?;
        Ok(registry)
    }
}

fn merge_into(registry: &mut ProjectRegistry, projects: Vec<Project>) -> Result<(), RegistryError> {
    for project in projects {
        let project = project.validated()?;
        if let Some(existing) = registry
            .projects
            .iter()
            .find(|item| item.name == project.name || item.working_dir == project.working_dir)
        {
            if existing != &project {
                return Err(RegistryError::Validation(format!(
                    "project '{}' conflicts with existing project '{}' ({})",
                    project.name,
                    existing.name,
                    existing.working_dir.display()
                )));
            }
            continue;
        }
        registry.projects.push(project);
    }
    registry
        .projects
        .sort_by(|left, right| left.name.cmp(&right.name));
    Ok(())
}

fn validate_registry(registry: ProjectRegistry) -> Result<ProjectRegistry, RegistryError> {
    if registry.version != REGISTRY_VERSION {
        return Err(RegistryError::Corrupt(format!(
            "unsupported project registry schema {}",
            registry.version
        )));
    }
    let mut names = HashSet::new();
    let mut paths = HashSet::new();
    for project in &registry.projects {
        validate_name(&project.name).map_err(|error| RegistryError::Corrupt(error.to_string()))?;
        if project.name.trim() != project.name {
            return Err(RegistryError::Corrupt(
                "stored project name must be normalized".into(),
            ));
        }
        if !project.working_dir.is_absolute() {
            return Err(RegistryError::Corrupt(format!(
                "stored working directory must be absolute: {}",
                project.working_dir.display()
            )));
        }
        if project.working_dir.exists() {
            if !project.working_dir.is_dir() {
                return Err(RegistryError::Corrupt(format!(
                    "stored working directory is not a directory: {}",
                    project.working_dir.display()
                )));
            }
            let canonical = fs::canonicalize(&project.working_dir)?;
            if canonical != project.working_dir {
                return Err(RegistryError::Corrupt(format!(
                    "stored working directory must be canonical: {}",
                    project.working_dir.display()
                )));
            }
        }
        if !names.insert(&project.name) {
            return Err(RegistryError::Duplicate(project.name.clone()));
        }
        if !paths.insert(&project.working_dir) {
            return Err(RegistryError::Corrupt(format!(
                "working directory registered more than once: {}",
                project.working_dir.display()
            )));
        }
    }
    Ok(registry)
}

fn validate_name(name: &str) -> Result<(), RegistryError> {
    if name.is_empty() {
        return Err(RegistryError::Validation(
            "project name must not be empty".into(),
        ));
    }
    if name.contains('/') || name.chars().any(char::is_control) {
        return Err(RegistryError::Validation(
            "project name must not contain '/' or control characters".into(),
        ));
    }
    Ok(())
}

struct RegistryFileLock(File);

pub(crate) struct RegistryWriteGuard {
    _process: MutexGuard<'static, ()>,
    _file: RegistryFileLock,
}

impl RegistryFileLock {
    fn acquire(root: &Path) -> Result<Self, RegistryError> {
        fs::create_dir_all(root)?;
        fs::set_permissions(root, fs::Permissions::from_mode(0o700))?;
        let path = root.join("projects.lock");
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(&path)
            .map_err(|error| RegistryError::Io(format!("opening {}: {error}", path.display())))?;
        file.set_permissions(fs::Permissions::from_mode(0o600))?;
        let result = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX) };
        if result != 0 {
            return Err(RegistryError::Io(format!(
                "locking {}: {}",
                path.display(),
                std::io::Error::last_os_error()
            )));
        }
        Ok(Self(file))
    }
}

impl Drop for RegistryFileLock {
    fn drop(&mut self) {
        unsafe {
            libc::flock(self.0.as_raw_fd(), libc::LOCK_UN);
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum RegistryError {
    #[error("invalid project: {0}")]
    Validation(String),
    #[error("project '{0}' already exists")]
    Duplicate(String),
    #[error("project '{0}' not found")]
    NotFound(String),
    #[error("stale registry revision: expected {expected}, actual {actual}")]
    Conflict { expected: u64, actual: u64 },
    #[error("project registry unavailable: {0}")]
    Unavailable(String),
    #[error("project registry I/O error: {0}")]
    Io(String),
    #[error("invalid project registry: {0}")]
    Corrupt(String),
}

impl From<std::io::Error> for RegistryError {
    fn from(value: std::io::Error) -> Self {
        Self::Io(value.to_string())
    }
}

#[cfg(test)]
#[path = "registry_contract_tests.rs"]
mod registry_contract_tests;