Skip to content

Commit 9c56918

Browse files
committed
Addressed comments
1 parent 1230a08 commit 9c56918

File tree

5 files changed

+75
-60
lines changed

5 files changed

+75
-60
lines changed

src/libstd/fs.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ impl OpenOptions {
414414
/// This option, when true, will indicate that the file should be
415415
/// `write`-able if opened.
416416
///
417-
/// If a file already exist, the contents of that file get overwritten, but it is
418-
/// not truncated.
417+
/// If a file already exist, any write calls on the file will overwrite its
418+
/// contents, without truncating it.
419419
///
420420
/// # Examples
421421
///
@@ -436,19 +436,20 @@ impl OpenOptions {
436436
/// Note that setting `.write(true).append(true)` has the same effect as
437437
/// setting only `.append(true)`.
438438
///
439-
/// For most filesystems the operating system guarantees all writes are atomic:
440-
/// no writes get mangled because another process writes at the same time.
439+
/// For most filesystems the operating system guarantees all writes are
440+
/// atomic: no writes get mangled because another process writes at the same
441+
/// time.
441442
///
442-
/// One maybe obvious note when using append-mode: make sure that all data that
443-
/// belongs together, is written the the file in one operation. This can be done
444-
/// by concatenating strings before passing them to `write()`, or using a buffered
445-
/// writer (with a more than adequately sized buffer) and calling `flush()` when the
446-
/// message is complete.
443+
/// One maybe obvious note when using append-mode: make sure that all data
444+
/// that belongs together, is written the the file in one operation. This
445+
/// can be done by concatenating strings before passing them to `write()`,
446+
/// or using a buffered writer (with a more than adequately sized buffer)
447+
/// and calling `flush()` when the message is complete.
447448
///
448-
/// If a file is opened with both read and append access, beware that after opening
449-
/// and after every write the position for reading may be set at the end of the file.
450-
/// So before writing save the current position (using `seek(SeekFrom::Current(0))`,
451-
/// and restore it before the next read.
449+
/// If a file is opened with both read and append access, beware that after
450+
/// opening and after every write the position for reading may be set at the
451+
/// end of the file. So before writing save the current position (using
452+
/// `seek(SeekFrom::Current(0))`, and restore it before the next read.
452453
///
453454
/// # Examples
454455
///
@@ -507,7 +508,12 @@ impl OpenOptions {
507508
/// No file is allowed to exist at the target location, also no (dangling)
508509
/// symlink.
509510
///
510-
/// if `.create_new(true)` is set, `.create()` and `.truncate()` are ignored.
511+
/// This option is usefull because it as atomic. Otherwise between checking
512+
/// whether a file exists and creating a new one, the file may have been
513+
/// created by another process (a TOCTOU race condition / attack).
514+
///
515+
/// If `.create_new(true)` is set, `.create()` and `.truncate()` are
516+
/// ignored.
511517
///
512518
/// The file must be opened with write or append access in order to create
513519
/// a new file.
@@ -518,7 +524,9 @@ impl OpenOptions {
518524
/// #![feature(expand_open_options)]
519525
/// use std::fs::OpenOptions;
520526
///
521-
/// let file = OpenOptions::new().write(true).create_new(true).open("foo.txt");
527+
/// let file = OpenOptions::new().write(true)
528+
/// .create_new(true)
529+
/// .open("foo.txt");
522530
/// ```
523531
#[unstable(feature = "expand_open_options",
524532
reason = "recently added",
@@ -534,7 +542,8 @@ impl OpenOptions {
534542
/// This function will return an error under a number of different
535543
/// circumstances, to include but not limited to:
536544
///
537-
/// * Opening a file that does not exist without setting `create` or `create_new`.
545+
/// * Opening a file that does not exist without setting `create` or
546+
/// `create_new`.
538547
/// * Attempting to open a file with access that the user lacks
539548
/// permissions for
540549
/// * Filesystem-level errors (full disk, etc)

src/libstd/sys/unix/ext/fs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ pub trait OpenOptionsExt {
107107

108108
/// Pass custom flags to the `flags` agument of `open`.
109109
///
110-
/// The bits that define the access mode are masked out with `O_ACCMODE`, to ensure
111-
/// they do not interfere with the access mode set by Rusts options.
110+
/// The bits that define the access mode are masked out with `O_ACCMODE`, to
111+
/// ensure they do not interfere with the access mode set by Rusts options.
112112
///
113113
/// Custom flags can only set flags, not remove flags set by Rusts options.
114+
/// This options overwrites any previously set custom flags.
114115
///
115116
/// # Examples
116117
///
@@ -121,7 +122,9 @@ pub trait OpenOptionsExt {
121122
///
122123
/// let mut options = OpenOptions::new();
123124
/// options.write(true);
124-
/// if cfg!(unix) { options.custom_flags(libc::O_NOFOLLOW); }
125+
/// if cfg!(unix) {
126+
/// options.custom_flags(libc::O_NOFOLLOW);
127+
/// }
125128
/// let file = options.open("foo.txt");
126129
/// ```
127130
#[unstable(feature = "expand_open_options",

src/libstd/sys/unix/fs.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,15 @@ impl OpenOptions {
275275

276276
fn get_creation_mode(&self) -> io::Result<c_int> {
277277
match (self.write, self.append) {
278-
(true, false) => {}
279-
(false, false) => if self.truncate || self.create || self.create_new {
280-
return Err(Error::from_raw_os_error(libc::EINVAL));
281-
},
282-
(_, true) => if self.truncate && !self.create_new {
283-
return Err(Error::from_raw_os_error(libc::EINVAL));
284-
},
278+
(true, false) => {}
279+
(false, false) =>
280+
if self.truncate || self.create || self.create_new {
281+
return Err(Error::from_raw_os_error(libc::EINVAL));
282+
},
283+
(_, true) =>
284+
if self.truncate && !self.create_new {
285+
return Err(Error::from_raw_os_error(libc::EINVAL));
286+
},
285287
}
286288

287289
Ok(match (self.create, self.truncate, self.create_new) {

src/libstd/sys/windows/ext/fs.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub trait OpenOptionsExt {
2727
/// with the specified value.
2828
///
2929
/// This will override the `read`, `write`, and `append` flags on the
30-
/// `OpenOptions` structure. This method provides fine-grained control
31-
/// over the permissions to read, write and append data, attributes
32-
/// (like hidden and system) and extended attributes.
30+
/// `OpenOptions` structure. This method provides fine-grained control over
31+
/// the permissions to read, write and append data, attributes (like hidden
32+
/// and system) and extended attributes.
3333
///
3434
/// # Examples
3535
///
@@ -38,18 +38,19 @@ pub trait OpenOptionsExt {
3838
/// use std::fs::OpenOptions;
3939
/// use std::os::windows::fs::OpenOptionsExt;
4040
///
41-
/// // Open without read and write permission, for example if you only need to call `stat()`
42-
/// // on the file
41+
/// // Open without read and write permission, for example if you only need
42+
/// // to call `stat()` on the file
4343
/// let file = OpenOptions::new().access_mode(0).open("foo.txt");
4444
/// ```
4545
fn access_mode(&mut self, access: u32) -> &mut Self;
4646

4747
/// Overrides the `dwShareMode` argument to the call to `CreateFile` with
4848
/// the specified value.
4949
///
50-
/// By default `share_mode` is set to `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`.
51-
/// Specifying less permissions denies others to read from, write to and/or
52-
/// delete the file while it is open.
50+
/// By default `share_mode` is set to
51+
/// `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`. Specifying
52+
/// less permissions denies others to read from, write to and/or delete the
53+
/// file while it is open.
5354
///
5455
/// # Examples
5556
///
@@ -58,17 +59,20 @@ pub trait OpenOptionsExt {
5859
/// use std::fs::OpenOptions;
5960
/// use std::os::windows::fs::OpenOptionsExt;
6061
///
62+
/// // Do not allow others to read or modify this file while we have it open
63+
/// // for writing
6164
/// let file = OpenOptions::new().write(true)
62-
/// .share_mode(0) // Do not allow others to read or modify
65+
/// .share_mode(0)
6366
/// .open("foo.txt");
6467
/// ```
6568
fn share_mode(&mut self, val: u32) -> &mut Self;
6669

67-
/// Sets extra flags for the `dwFileFlags` argument to the call to `CreateFile2`
68-
/// (or combines it with `attributes` and `security_qos_flags` to set the
69-
/// `dwFlagsAndAttributes` for `CreateFile`).
70+
/// Sets extra flags for the `dwFileFlags` argument to the call to
71+
/// `CreateFile2` (or combines it with `attributes` and `security_qos_flags`
72+
/// to set the `dwFlagsAndAttributes` for `CreateFile`).
7073
///
7174
/// Custom flags can only set flags, not remove flags set by Rusts options.
75+
/// This options overwrites any previously set custom flags.
7276
///
7377
/// # Examples
7478
///
@@ -79,7 +83,9 @@ pub trait OpenOptionsExt {
7983
///
8084
/// let mut options = OpenOptions::new();
8185
/// options.create(true).write(true);
82-
/// if cfg!(windows) { options.custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE); }
86+
/// if cfg!(windows) {
87+
/// options.custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE);
88+
/// }
8389
/// let file = options.open("foo.txt");
8490
/// ```
8591
#[unstable(feature = "expand_open_options",
@@ -89,15 +95,16 @@ pub trait OpenOptionsExt {
8995

9096
/// Sets the `dwFileAttributes` argument to the call to `CreateFile2` to
9197
/// the specified value (or combines it with `custom_flags` and
92-
/// `security_qos_flags` to set the `dwFlagsAndAttributes` for `CreateFile`).
98+
/// `security_qos_flags` to set the `dwFlagsAndAttributes` for
99+
/// `CreateFile`).
93100
///
94-
/// If a _new_ file is created because it does not yet exist and `.create(true)` or
95-
/// `.create_new(true)` are specified, the new file is given the attributes declared
96-
/// with `.attributes()`.
101+
/// If a _new_ file is created because it does not yet exist and
102+
///`.create(true)` or `.create_new(true)` are specified, the new file is
103+
/// given the attributes declared with `.attributes()`.
97104
///
98105
/// If an _existing_ file is opened with `.create(true).truncate(true)`, its
99-
/// existing attributes are preserved and combined with the ones declared with
100-
/// `.attributes()`.
106+
/// existing attributes are preserved and combined with the ones declared
107+
/// with `.attributes()`.
101108
///
102109
/// In all other cases the attributes get ignored.
103110
///
@@ -119,10 +126,6 @@ pub trait OpenOptionsExt {
119126
/// the specified value (or combines it with `custom_flags` and `attributes`
120127
/// to set the `dwFlagsAndAttributes` for `CreateFile`).
121128
fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions;
122-
123-
/// Sets the `lpSecurityAttributes` argument to the call to `CreateFile` to
124-
/// the specified value.
125-
fn security_attributes(&mut self, attrs: sys::c::LPSECURITY_ATTRIBUTES) -> &mut OpenOptions;
126129
}
127130

128131
#[unstable(feature = "open_options_ext",
@@ -148,10 +151,6 @@ impl OpenOptionsExt for OpenOptions {
148151
fn security_qos_flags(&mut self, flags: u32) -> &mut OpenOptions {
149152
self.as_inner_mut().security_qos_flags(flags); self
150153
}
151-
152-
fn security_attributes(&mut self, attrs: sys::c::LPSECURITY_ATTRIBUTES) -> &mut OpenOptions {
153-
self.as_inner_mut().security_attributes(attrs); self
154-
}
155154
}
156155

157156
/// Extension methods for `fs::Metadata` to access the raw fields contained

src/libstd/sys/windows/fs.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,15 @@ impl OpenOptions {
209209
const ERROR_INVALID_PARAMETER: i32 = 87;
210210

211211
match (self.write, self.append) {
212-
(true, false) => {}
213-
(false, false) => if self.truncate || self.create || self.create_new {
214-
return Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
215-
},
216-
(_, true) => if self.truncate && !self.create_new {
217-
return Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
218-
},
212+
(true, false) => {}
213+
(false, false) =>
214+
if self.truncate || self.create || self.create_new {
215+
return Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
216+
},
217+
(_, true) =>
218+
if self.truncate && !self.create_new {
219+
return Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
220+
},
219221
}
220222

221223
Ok(match (self.create, self.truncate, self.create_new) {

0 commit comments

Comments
 (0)