-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsync.rs
More file actions
97 lines (84 loc) · 3.05 KB
/
sync.rs
File metadata and controls
97 lines (84 loc) · 3.05 KB
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
use crate::{client::build_client, config::Config, pantry_db};
use async_compression::tokio::bufread::XzDecoder;
use fs2::FileExt;
use futures::TryStreamExt;
use rusqlite::Connection;
use std::{error::Error, fs::OpenOptions, path::PathBuf};
use tokio_tar::ArchiveBuilder;
use tokio_util::compat::FuturesAsyncReadCompatExt;
#[allow(clippy::all)]
pub fn should(config: &Config) -> Result<bool, Box<dyn Error>> {
if !config.pantry_dir.join("projects").is_dir() {
Ok(true)
} else {
// the file always exists because we create the connection
// but will be 0 bytes if we need to fill it
Ok(std::fs::metadata(&config.pantry_db_file)?.len() == 0)
}
}
// doesn’t replace pantry clone, will build db
// essential for working in a local pantry clone with PKGX_PANTRY_DIR set
pub async fn ensure(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
if !config.pantry_dir.join("projects").is_dir() {
replace(config, conn).await
} else {
let lockfile = lock(config)?;
pantry_db::cache(config, conn)?;
FileExt::unlock(&lockfile)?;
Ok(())
}
}
pub async fn update(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
if std::env::var("PKGX_PANTRY_DIR").is_ok() {
if config.pantry_dir.join("projects").is_dir() {
let lockfile = lock(config)?;
pantry_db::cache(config, conn)?;
FileExt::unlock(&lockfile)?;
return Ok(());
} else {
return Err(
"PKGX_PANTRY_DIR is set but does not contain a pantry (missing projects/)",
)?;
}
}
replace(config, conn).await
}
async fn replace(config: &Config, conn: &mut Connection) -> Result<(), Box<dyn Error>> {
let url = format!(
"{}/{}",
config.dist_url,
env!("PKGX_PANTRY_TARBALL_FILENAME")
);
let lockfile = lock(config)?;
download_and_extract_pantry(&url, &config.pantry_dir).await?;
pantry_db::cache(config, conn)?;
FileExt::unlock(&lockfile)?;
Ok(())
}
async fn download_and_extract_pantry(url: &str, dest: &PathBuf) -> Result<(), Box<dyn Error>> {
let rsp = build_client()?.get(url).send().await?.error_for_status()?;
let stream = rsp.bytes_stream();
let stream = stream.map_err(futures::io::Error::other).into_async_read();
let stream = stream.compat();
let decoder = XzDecoder::new(stream);
// Step 3: Extract the tar archive
let mut archive = ArchiveBuilder::new(decoder)
.set_preserve_permissions(true)
.build();
archive.unpack(dest).await?;
Ok(())
}
fn lock(config: &Config) -> Result<std::fs::File, Box<dyn Error>> {
std::fs::create_dir_all(&config.pantry_dir)?;
#[cfg(not(windows))]
let lockfile = OpenOptions::new().read(true).open(&config.pantry_dir)?;
#[cfg(windows)]
let lockfile = OpenOptions::new()
.read(true)
.create(true)
.truncate(true)
.write(true)
.open(config.pantry_dir.join("lockfile"))?;
lockfile.lock_exclusive()?;
Ok(lockfile)
}