| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | | pub mod archive; |
| 9 | | pub mod cache; |
| 10 | | pub mod edits; |
| 11 | | pub mod functions; |
| 12 | | pub mod names; |
| 13 | | pub mod proposals; |
| 14 | | pub mod search; |
| 15 | | pub mod view; |
| 16 | |
|
| 17 | | pub use cache::{Loaded, StateCache}; |
| 18 | | pub use names::RepoName; |
| 19 | |
|
| 20 | | use oxy_core::repo::Repo; |
| 21 | | use std::collections::HashMap; |
| 22 | | use std::path::{Path, PathBuf}; |
| 23 | | use std::sync::{Arc, Mutex}; |
| 24 | | use tokio::sync::{OwnedMutexGuard, OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock}; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | | #[derive(Default)] |
| 32 | | struct Locks { |
| 33 | | life: Arc<RwLock<()>>, |
| 34 | | writer: Arc<tokio::sync::Mutex<()>>, |
| 35 | | } |
| 36 | |
|
| 37 | |
|
| 38 | | pub type ReadGuard = OwnedRwLockReadGuard<()>; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | | pub struct WriteGuard { |
| 43 | | _writer: OwnedMutexGuard<()>, |
| 44 | | _life: OwnedRwLockReadGuard<()>, |
| 45 | | } |
| 46 | |
|
| 47 | |
|
| 48 | | pub type ExclusiveGuard = OwnedRwLockWriteGuard<()>; |
| 49 | |
|
| 50 | |
|
| 51 | | pub struct Repos { |
| 52 | | root: PathBuf, |
| 53 | | locks: Mutex<HashMap<String, Arc<Locks>>>, |
| 54 | | cache: Arc<StateCache>, |
| 55 | | pub searches: search::SearchCache, |
| 56 | | } |
| 57 | |
|
| 58 | | #[derive(Debug, thiserror::Error)] |
| 59 | | pub enum RepoError { |
| 60 | | #[error("no such repository")] |
| 61 | | NotFound, |
| 62 | | #[error("{0} already exists")] |
| 63 | | Exists(String), |
| 64 | | #[error("{0}")] |
| 65 | | Oxy(#[from] oxy_core::repo::RepoError), |
| 66 | | #[error("{0}")] |
| 67 | | Store(#[from] oxy_core::store::StoreError), |
| 68 | | #[error("{0}")] |
| 69 | | Io(#[from] std::io::Error), |
| 70 | | } |
| 71 | |
|
| 72 | | impl Repos { |
| 73 | 386× | pub fn new(root: PathBuf) -> Repos { |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | 386× | let briefs = root.parent().unwrap_or(&root).join("cache").join("summaries").join(root.file_name().unwrap_or_default()); |
| 78 | 386× | let cache = Arc::new(StateCache::new(256).with_briefs(briefs)); |
| 79 | 386× | Repos { root, locks: Mutex::new(HashMap::new()), cache, searches: search::SearchCache::default() } |
| 80 | 386× | } |
| 81 | |
|
| 82 | 9× | pub fn cache(&self) -> &Arc<StateCache> { |
| 83 | 9× | &self.cache |
| 84 | 9× | } |
| 85 | |
|
| 86 | 297× | pub fn root(&self) -> &Path { |
| 87 | 297× | &self.root |
| 88 | 297× | } |
| 89 | |
|
| 90 | | |
| 91 | 10809× | pub fn dir(&self, name: &RepoName) -> PathBuf { |
| 92 | 10809× | self.root.join(&name.owner).join(&name.name) |
| 93 | 10809× | } |
| 94 | |
|
| 95 | 5351× | pub fn exists(&self, name: &RepoName) -> bool { |
| 96 | 5351× | self.dir(name).join(oxy_core::repo::OXY_DIR).is_dir() |
| 97 | 5351× | } |
| 98 | |
|
| 99 | | |
| 100 | 1834× | pub fn open(&self, name: &RepoName) -> Result<Repo, RepoError> { |
| 101 | 1834× | if !self.exists(name) { |
| 102 | 1× | return Err(RepoError::NotFound); |
| 103 | 1833× | } |
| 104 | 1833× | Ok(Repo::open(&self.dir(name))?) |
| 105 | 1834× | } |
| 106 | |
|
| 107 | | |
| 108 | | |
| 109 | | |
| 110 | 193× | pub fn create(&self, name: &RepoName, id: Option<[u8; 16]>) -> Result<Repo, RepoError> { |
| 111 | 193× | let dir = self.dir(name); |
| 112 | 193× | if dir.exists() { |
| 113 | 0× | return Err(RepoError::Exists(name.to_string())); |
| 114 | 193× | } |
| 115 | 193× | Ok(match id { |
| 116 | 8× | Some(id) => Repo::init_bare_with_id(&dir, id)?, |
| 117 | 185× | None => Repo::init_bare_unbound(&dir)?, |
| 118 | | }) |
| 119 | 193× | } |
| 120 | |
|
| 121 | | |
| 122 | | |
| 123 | | |
| 124 | 0× | pub fn create_on(&self, name: &RepoName, url: &str) -> Result<Repo, RepoError> { |
| 125 | 0× | let dir = self.dir(name); |
| 126 | 0× | if dir.exists() { |
| 127 | 0× | return Err(RepoError::Exists(name.to_string())); |
| 128 | 0× | } |
| 129 | 0× | let made = (|| -> Result<Repo, RepoError> { |
| 130 | 0× | drop(Repo::init_bare_on(&dir, [0; 16], Some(url))?); |
| 131 | | |
| 132 | | |
| 133 | 0× | let oxy = dir.join(oxy_core::repo::OXY_DIR); |
| 134 | 0× | std::fs::remove_file(oxy.join("id"))?; |
| 135 | 0× | std::fs::write(oxy.join("unbound"), b"")?; |
| 136 | 0× | Ok(Repo::open(&dir)?) |
| 137 | | })(); |
| 138 | 0× | if made.is_err() { |
| 139 | 0× | let _ = std::fs::remove_dir_all(&dir); |
| 140 | 0× | } |
| 141 | 0× | made |
| 142 | 0× | } |
| 143 | |
|
| 144 | | |
| 145 | | |
| 146 | | |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | 2× | pub async fn fork(&self, src: &RepoName, dest: &RepoName) -> Result<(), RepoError> { |
| 151 | 2× | let guard = self.write_guard(src).await; |
| 152 | 2× | let (from, to) = (self.dir(src), self.dir(dest)); |
| 153 | 2× | if !self.exists(src) { |
| 154 | 0× | return Err(RepoError::NotFound); |
| 155 | 2× | } |
| 156 | 2× | if to.exists() { |
| 157 | 0× | return Err(RepoError::Exists(dest.to_string())); |
| 158 | 2× | } |
| 159 | 2× | if Repo::open(&from).is_ok_and(|r| r.config.storage_url.is_some()) { |
| 160 | 0× | return Err(RepoError::Io(std::io::Error::other( |
| 161 | 0× | "a repository kept in a bucket cannot be forked yet: clone it and push the clone to a new repository", |
| 162 | 0× | ))); |
| 163 | 2× | } |
| 164 | 2× | tokio::task::spawn_blocking(move || { |
| 165 | 2× | let _guard = guard; |
| 166 | 2× | let made = (|| -> Result<(), RepoError> { |
| 167 | 2× | drop(Repo::init_bare(&to)?); |
| 168 | 4× | let objects = |d: &Path| d.join(oxy_core::repo::OXY_DIR).join("objects"); |
| 169 | 2× | link_tree(&objects(&from), &objects(&to))?; |
| 170 | 2× | let source = Repo::open(&from)?; |
| 171 | 2× | let fork = Repo::open(&to)?; |
| 172 | 2× | let existing: Vec<String> = fork.bookmarks()?.into_iter().map(|b| b.name).collect(); |
| 173 | 2× | for b in source.bookmarks()? { |
| 174 | 2× | if existing.contains(&b.name) { |
| 175 | 2× | fork.bookmark_set(&b.name, &b.heads)?; |
| 176 | | } else { |
| 177 | 0× | fork.bookmark_create(&b.name, Some(&b.heads))?; |
| 178 | | } |
| 179 | | } |
| 180 | 2× | Ok(()) |
| 181 | | })(); |
| 182 | 2× | if made.is_err() { |
| 183 | 0× | let _ = std::fs::remove_dir_all(&to); |
| 184 | 2× | } |
| 185 | 2× | made |
| 186 | 2× | }) |
| 187 | 2× | .await |
| 188 | 2× | .map_err(|e| RepoError::Io(std::io::Error::other(e.to_string())))? |
| 189 | 2× | } |
| 190 | |
|
| 191 | | |
| 192 | | |
| 193 | 1× | pub fn rename(&self, from: &RepoName, to: &RepoName) -> Result<(), RepoError> { |
| 194 | 1× | let (src, dest) = (self.dir(from), self.dir(to)); |
| 195 | 1× | if !src.exists() { |
| 196 | 0× | return Err(RepoError::NotFound); |
| 197 | 1× | } |
| 198 | 1× | if dest.exists() { |
| 199 | 0× | return Err(RepoError::Exists(to.to_string())); |
| 200 | 1× | } |
| 201 | 1× | if let Some(parent) = dest.parent() { |
| 202 | 1× | std::fs::create_dir_all(parent)?; |
| 203 | 0× | } |
| 204 | 1× | std::fs::rename(&src, &dest)?; |
| 205 | 1× | let mut locks = self.locks.lock().unwrap(); |
| 206 | 1× | if let Some(l) = locks.remove(&from.to_string()) { |
| 207 | 1× | locks.insert(to.to_string(), l); |
| 208 | 1× | } |
| 209 | 1× | Ok(()) |
| 210 | 1× | } |
| 211 | |
|
| 212 | | |
| 213 | 6× | pub fn trash(&self, name: &RepoName) -> Result<PathBuf, RepoError> { |
| 214 | 6× | let dir = self.dir(name); |
| 215 | 6× | if !dir.exists() { |
| 216 | 0× | return Err(RepoError::NotFound); |
| 217 | 6× | } |
| 218 | 6× | let trash = self.root.parent().unwrap_or(&self.root).join("trash").join(&name.owner); |
| 219 | | |
| 220 | 6× | let dest = trash.join(format!("{}-{}", name.name, crate::db::now_ms())); |
| 221 | 6× | std::fs::create_dir_all(dest.parent().unwrap_or(&trash))?; |
| 222 | 6× | std::fs::rename(&dir, &dest)?; |
| 223 | 6× | Ok(dest) |
| 224 | 6× | } |
| 225 | |
|
| 226 | 3956× | fn lock(&self, name: &RepoName) -> Arc<Locks> { |
| 227 | 3956× | self.locks.lock().unwrap().entry(name.to_string()).or_default().clone() |
| 228 | 3956× | } |
| 229 | |
|
| 230 | | |
| 231 | | |
| 232 | 2745× | pub async fn read<T: Send + 'static>( |
| 233 | 2745× | &self, |
| 234 | 2745× | name: &RepoName, |
| 235 | 2745× | f: impl FnOnce(&Repo, &StateCache) -> Result<T, RepoError> + Send + 'static, |
| 236 | 2745× | ) -> Result<T, RepoError> { |
| 237 | 2745× | let guard = self.read_guard(name).await; |
| 238 | 2745× | let dir = self.dir(name); |
| 239 | 2745× | let exists = self.exists(name); |
| 240 | 2745× | let cache = self.cache.clone(); |
| 241 | 2745× | tokio::task::spawn_blocking(move || { |
| 242 | 2743× | let _guard = guard; |
| 243 | 2743× | if !exists { |
| 244 | 0× | return Err(RepoError::NotFound); |
| 245 | 2743× | } |
| 246 | 2743× | let repo = Repo::open(&dir)?; |
| 247 | 2742× | f(&repo, &cache) |
| 248 | 2743× | }) |
| 249 | 2745× | .await |
| 250 | 2744× | .map_err(|e| RepoError::Io(std::io::Error::other(e)))? |
| 251 | 2744× | } |
| 252 | |
|
| 253 | | |
| 254 | 100× | pub async fn write<T: Send + 'static>( |
| 255 | 100× | &self, |
| 256 | 100× | name: &RepoName, |
| 257 | 100× | f: impl FnOnce(&Repo, &StateCache) -> Result<T, RepoError> + Send + 'static, |
| 258 | 100× | ) -> Result<T, RepoError> { |
| 259 | 100× | let guard = self.write_guard(name).await; |
| 260 | 100× | let dir = self.dir(name); |
| 261 | 100× | let exists = self.exists(name); |
| 262 | 100× | let cache = self.cache.clone(); |
| 263 | 100× | tokio::task::spawn_blocking(move || { |
| 264 | 100× | let _guard = guard; |
| 265 | 100× | if !exists { |
| 266 | 0× | return Err(RepoError::NotFound); |
| 267 | 100× | } |
| 268 | 100× | let repo = Repo::open(&dir)?; |
| 269 | 100× | f(&repo, &cache) |
| 270 | 100× | }) |
| 271 | 100× | .await |
| 272 | 100× | .map_err(|e| RepoError::Io(std::io::Error::other(e)))? |
| 273 | 100× | } |
| 274 | |
|
| 275 | | |
| 276 | | |
| 277 | | |
| 278 | | |
| 279 | 384× | pub async fn write_guard(&self, name: &RepoName) -> WriteGuard { |
| 280 | 384× | let locks = self.lock(name); |
| 281 | 384× | let writer = locks.writer.clone().lock_owned().await; |
| 282 | 383× | WriteGuard { _writer: writer, _life: locks.life.clone().read_owned().await } |
| 283 | 383× | } |
| 284 | |
|
| 285 | 3563× | pub async fn read_guard(&self, name: &RepoName) -> ReadGuard { |
| 286 | 3563× | self.lock(name).life.clone().read_owned().await |
| 287 | 3562× | } |
| 288 | |
|
| 289 | | |
| 290 | | |
| 291 | 9× | pub async fn exclusive_guard(&self, name: &RepoName) -> ExclusiveGuard { |
| 292 | 9× | self.lock(name).life.clone().write_owned().await |
| 293 | 8× | } |
| 294 | | } |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | 22× | fn link_tree(from: &Path, to: &Path) -> std::io::Result<()> { |
| 300 | 22× | std::fs::create_dir_all(to)?; |
| 301 | 37× | for e in std::fs::read_dir(from)? { |
| 302 | 37× | let e = e?; |
| 303 | 37× | let name = e.file_name(); |
| 304 | 37× | let n = name.to_string_lossy(); |
| 305 | 37× | if n.starts_with("tmp-") || n.starts_with("sigs-tmp-") || n.contains(".tmp") { |
| 306 | 0× | continue; |
| 307 | 37× | } |
| 308 | 37× | let (src, dst) = (e.path(), to.join(&name)); |
| 309 | 37× | if e.file_type()?.is_dir() { |
| 310 | 20× | link_tree(&src, &dst)?; |
| 311 | 17× | } else if !dst.exists() && std::fs::hard_link(&src, &dst).is_err() { |
| 312 | 0× | std::fs::copy(&src, &dst)?; |
| 313 | 17× | } |
| 314 | | } |
| 315 | 22× | Ok(()) |
| 316 | 22× | } |