Skip to content

Commit ffa6449

Browse files
committed
sync: build for wasm
1 parent 406e5a8 commit ffa6449

4 files changed

Lines changed: 42 additions & 23 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ feat_Tier1 = [
210210
## (primary platforms) feature sets
211211
# "feat_wasm" == set of utilities which can be built for wasm target
212212
# We don't need to support all of wasm targets. So the ambiguous name is used at here
213-
feat_wasm = ["feat_common_core", "nproc"]
213+
feat_wasm = ["feat_common_core", "nproc", "sync"]
214214
# "feat_os_unix" == set of utilities which can be built/run on modern/usual *nix platforms.
215215
feat_os_unix = [
216216
"feat_Tier1",

‎src/uu/sync/locales/en-US.ftl‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ sync-about = Synchronize cached writes to persistent storage
22
sync-usage = sync [OPTION]... FILE...
33
44
# Help messages
5-
sync-help-file-system = sync the file systems that contain the files (Linux and Windows only)
6-
sync-help-data = sync only file data, no unneeded metadata (Linux only)
5+
sync-help-file-system = sync the file systems that contain the files
6+
sync-help-files = sync files (due to platform's limitation)
7+
sync-help-data = sync only file data, no unneeded metadata
78
89
# Error messages
910
sync-error-data-needs-argument = --data needs at least one argument

‎src/uu/sync/locales/fr-FR.ftl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ sync-about = Synchroniser les écritures en cache vers le stockage persistant
22
sync-usage = sync [OPTION]... FICHIER...
33
44
# Messages d'aide
5-
sync-help-file-system = synchroniser les systèmes de fichiers qui contiennent les fichiers (Linux et Windows uniquement)
6-
sync-help-data = synchroniser seulement les données des fichiers, pas les métadonnées inutiles (Linux uniquement)
5+
sync-help-file-system = synchroniser les systèmes de fichiers qui contiennent les fichiers
6+
sync-help-data = synchroniser seulement les données des fichiers, pas les métadonnées inutiles
77
88
# Messages d'erreur
99
sync-error-data-needs-argument = --data nécessite au moins un argument

‎src/uu/sync/src/sync.rs‎

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod options {
1818

1919
static ARG_FILES: &str = "files";
2020

21-
#[cfg(unix)]
21+
#[cfg(not(windows))]
2222
mod platform {
2323
#[cfg(any(target_os = "linux", target_os = "android"))]
2424
use std::fs::{File, OpenOptions};
@@ -38,6 +38,8 @@ mod platform {
3838
reason = "fn sig must match on all platforms"
3939
)]
4040
pub fn do_sync() -> UResult<()> {
41+
// maximize compatibility of scripts by noop on targets global sync is impossible
42+
#[cfg(unix)]
4143
rustix::fs::sync();
4244
Ok(())
4345
}
@@ -76,16 +78,6 @@ mod platform {
7678
}
7779
Ok(())
7880
}
79-
80-
#[cfg(any(target_os = "linux", target_os = "android"))]
81-
pub fn do_syncfs(files: &[String]) -> UResult<()> {
82-
do_sync_with(files, rustix::fs::syncfs)
83-
}
84-
85-
#[cfg(any(target_os = "linux", target_os = "android"))]
86-
pub fn do_fdatasync(files: &[String]) -> UResult<()> {
87-
do_sync_with(files, rustix::fs::fdatasync)
88-
}
8981
}
9082

9183
#[cfg(windows)]
@@ -248,11 +240,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
248240
if files.is_empty() {
249241
sync()?;
250242
} else {
251-
#[cfg(any(target_os = "linux", target_os = "android", windows))]
252243
syncfs(&files)?;
253244
}
254245
} else if matches.get_flag(options::DATA) {
255-
#[cfg(any(target_os = "linux", target_os = "android"))]
256246
fdatasync(&files)?;
257247
} else {
258248
sync()?;
@@ -261,6 +251,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
261251
}
262252

263253
pub fn uu_app() -> Command {
254+
#[cfg(any(target_os = "linux", target_os = "android", windows))]
255+
let syncfs = translate!("sync-help-file-system");
256+
#[cfg(not(any(target_os = "linux", target_os = "android", windows)))]
257+
let syncfs = translate!("sync-help-files");
264258
Command::new("sync")
265259
.version(uucore::crate_version!())
266260
.help_template(uucore::localized_help_template("sync"))
@@ -272,7 +266,7 @@ pub fn uu_app() -> Command {
272266
.short('f')
273267
.long(options::FILE_SYSTEM)
274268
.conflicts_with(options::DATA)
275-
.help(translate!("sync-help-file-system"))
269+
.help(syncfs)
276270
.action(ArgAction::SetTrue),
277271
)
278272
.arg(
@@ -294,12 +288,36 @@ fn sync() -> UResult<()> {
294288
platform::do_sync()
295289
}
296290

297-
#[cfg(any(target_os = "linux", target_os = "android", windows))]
291+
#[cfg(any(target_os = "linux", target_os = "android"))]
298292
fn syncfs(files: &[String]) -> UResult<()> {
299-
platform::do_syncfs(files)
293+
platform::do_sync_with(files, rustix::fs::syncfs)
300294
}
301295

302-
#[cfg(any(target_os = "linux", target_os = "android"))]
296+
// compromise: sync files only, not FS containing the files
297+
#[cfg(not(any(target_os = "linux", target_os = "android", windows)))]
298+
fn syncfs(files: &[String]) -> UResult<()> {
299+
do_sync_with_regular_file(files, |f| f.sync_all())
300+
}
301+
302+
#[cfg(any(target_os = "linux", target_os = "android", windows))]
303+
fn fdatasync(files: &[String]) -> UResult<()> {
304+
platform::do_sync_with(files, rustix::fs::fdatasync)
305+
}
306+
307+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
303308
fn fdatasync(files: &[String]) -> UResult<()> {
304-
platform::do_fdatasync(files)
309+
do_sync_with_regular_file(files, |f| f.sync_data())
310+
}
311+
312+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
313+
fn do_sync_with_regular_file<F>(files: &[String], op: F) -> UResult<()>
314+
where
315+
F: Fn(std::fs::File) -> std::io::Result<()>,
316+
{
317+
use uucore::error::FromIo;
318+
for path in files {
319+
let f = std::fs::OpenOptions::new().read(true).open(path)?;
320+
op(f).map_err_context(|| translate!("sync-error-syncing-file", "file" => path.quote()))?;
321+
}
322+
Ok(())
305323
}

0 commit comments

Comments
 (0)