Skip to content

Commit 17bf644

Browse files
authored
doc(builder): clarify permissions (#372)
fixes #313
1 parent c7423f1 commit 17bf644

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/lib.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ const NUM_RETRIES: u32 = 65536;
202202
const NUM_RAND_CHARS: usize = 6;
203203

204204
use std::ffi::OsStr;
205-
use std::fs::OpenOptions;
205+
use std::fs::{OpenOptions, Permissions};
206206
use std::io;
207207
use std::path::Path;
208208

@@ -227,7 +227,7 @@ pub struct Builder<'a, 'b> {
227227
prefix: &'a OsStr,
228228
suffix: &'b OsStr,
229229
append: bool,
230-
permissions: Option<std::fs::Permissions>,
230+
permissions: Option<Permissions>,
231231
disable_cleanup: bool,
232232
}
233233

@@ -374,7 +374,7 @@ impl<'a, 'b> Builder<'a, 'b> {
374374
self
375375
}
376376

377-
/// Set the file to be opened in append mode.
377+
/// Configure the file to be opened in append-only mode.
378378
///
379379
/// Default: `false`.
380380
///
@@ -393,31 +393,32 @@ impl<'a, 'b> Builder<'a, 'b> {
393393
self
394394
}
395395

396-
/// The permissions to create the tempfile or [tempdir](Self::tempdir) with.
396+
/// Set the permissions for the new temporary file/directory.
397397
///
398-
/// # Security
398+
/// # Platform Notes
399399
///
400-
/// By default, the permissions of tempfiles on Unix are set for it to be
401-
/// readable and writable by the owner only, yielding the greatest amount
402-
/// of security.
403-
/// As this method allows to widen the permissions, security would be
404-
/// reduced in such cases.
400+
/// ## Windows
401+
///
402+
/// This setting is only fully-supported on unix-like platforms. On Windows, if this method is
403+
/// called with a [`Permissions`] object where `permissions.readonly` returns true, creating
404+
/// temporary files and directories will fail with an error.
405405
///
406-
/// # Platform Notes
407406
/// ## Unix
408407
///
409-
/// The actual permission bits set on the tempfile or tempdir will be affected by the `umask`
410-
/// applied by the underlying syscall. The actual permission bits are calculated via
411-
/// `permissions & !umask`.
408+
/// On unix-like systems, the actual permission bits set on the tempfile or tempdir will be
409+
/// affected by the `umask` applied by the underlying syscall. The actual permission bits are
410+
/// calculated via `permissions & !umask`. In other words, depending on your umask, the
411+
/// permissions of the created file may be more restrictive (but never more permissive) than the
412+
/// ones you specified.
412413
///
413414
/// Permissions default to `0o600` for tempfiles and `0o777` for tempdirs. Note, this doesn't
414415
/// include effects of the current `umask`. For example, combined with the standard umask
415416
/// `0o022`, the defaults yield `0o600` for tempfiles and `0o755` for tempdirs.
416417
///
417-
/// ## Windows and others
418+
/// ## WASI
418419
///
419-
/// This setting is unsupported and trying to set a file or directory read-only
420-
/// will return an error.
420+
/// While custom permissions are allowed on WASI, they will be ignored as the platform has no
421+
/// concept of permissions or file modes (or multiple users for that matter).
421422
///
422423
/// # Examples
423424
///
@@ -431,12 +432,15 @@ impl<'a, 'b> Builder<'a, 'b> {
431432
///
432433
/// let all_read_write = std::fs::Permissions::from_mode(0o666);
433434
/// let tempfile = Builder::new().permissions(all_read_write).tempfile()?;
435+
///
436+
/// // Check that this worked and that the file is world-readable.
437+
/// //
438+
/// // NOTE: the file likely won't actually be created with 0o666 permissions because it's
439+
/// // restricted by the user's umask.
440+
/// //
441+
/// // NOTE: This test will fail if the user's umask is, e.g., 0o066.
434442
/// let actual_permissions = tempfile.path().metadata()?.permissions();
435-
/// assert_ne!(
436-
/// actual_permissions.mode() & !0o170000,
437-
/// 0o600,
438-
/// "we get broader permissions than the default despite umask"
439-
/// );
443+
/// assert_eq!(actual_permissions.mode() & 0o044, 0o044);
440444
/// # }
441445
/// # Ok::<(), std::io::Error>(())
442446
/// ```
@@ -460,7 +464,7 @@ impl<'a, 'b> Builder<'a, 'b> {
460464
/// # }
461465
/// # Ok::<(), std::io::Error>(())
462466
/// ```
463-
pub fn permissions(&mut self, permissions: std::fs::Permissions) -> &mut Self {
467+
pub fn permissions(&mut self, permissions: Permissions) -> &mut Self {
464468
self.permissions = Some(permissions);
465469
self
466470
}

0 commit comments

Comments
 (0)