Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc700477

Browse files
committed
mknod: set cli specified file mode
libc's mknod function applies the process' umask in the usual way:the permissions of the created node are `mode & ~umask`.the umask of uu-mknod is currently only set to 0 (= no mask)when the supplied mode is not 0o666:```rustlet set_umask_0 = config.mode & MODE_RW_UGO != MODE_RW_UGO;```because `MODE_RW_UGO == 0o666` is also the default `--mode` argument value.but when `--mode 0666` is given explicitly, the permissions should be setexactly to the requested bits, and the umask should not be applied.this patch fixes the issue by tracking if a custom mode was supplied by argument.interestingly, GNU coreutils issue a call to `fchmodat2` after `mknod` was called,even though they also set the umask to 0 if a custom mode was specified via `--mode`.this patch doesn't add that, but it may be necessary for other cases in the future.fixes#8342
1 parent213389c commitc700477

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

‎src/uu/mknod/src/mknod.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ impl FileType {
5050
}
5151
}
5252

53-
/// Configuration fordirectory creation.
53+
/// Configuration forspecial inode creation.
5454
pubstructConfig<'a>{
55+
/// bitmask of inode mode (permissions and file type)
5556
pubmode:mode_t,
5657

58+
/// when false, the exact mode bits will be set
59+
pubuse_umask:bool,
60+
5761
pubdev:dev_t,
5862

5963
/// Set `SELinux` security context.
@@ -66,18 +70,15 @@ pub struct Config<'a> {
6670
fnmknod(file_name:&str,config:Config) ->i32{
6771
let c_str =CString::new(file_name).expect("Failed to convert to CString");
6872

69-
// the user supplied a mode
70-
let set_umask = config.mode&MODE_RW_UGO !=MODE_RW_UGO;
71-
7273
unsafe{
73-
// storeprev umask
74-
letlast_umask =ifset_umask{libc::umask(0)}else{0};
74+
//set umask to 0 andstoreprevious umask
75+
letprev_umask =ifconfig.use_umask{0}else{libc::umask(0)};
7576

7677
let errno = libc::mknod(c_str.as_ptr(), config.mode, config.dev);
7778

7879
// set umask back to original value
79-
ifset_umask{
80-
libc::umask(last_umask);
80+
if!config.use_umask{
81+
libc::umask(prev_umask);
8182
}
8283

8384
if errno == -1{
@@ -110,8 +111,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
110111
let matches =uu_app().try_get_matches_from(args)?;
111112

112113
let file_type = matches.get_one::<FileType>("type").unwrap();
113-
let mode =get_mode(matches.get_one::<String>("mode")).map_err(|e|USimpleError::new(1, e))?
114-
| file_type.as_mode();
114+
115+
letmut use_umask =true;
116+
let mode_permissions =match matches.get_one::<String>("mode"){
117+
None =>MODE_RW_UGO,
118+
Some(str_mode) =>{
119+
use_umask =false;
120+
parse_mode(str_mode).map_err(|e|USimpleError::new(1, e))?
121+
}
122+
};
123+
let mode = mode_permissions | file_type.as_mode();
115124

116125
let file_name = matches
117126
.get_one::<String>("name")
@@ -144,6 +153,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
144153

145154
let config =Config{
146155
mode,
156+
use_umask,
147157
dev,
148158
set_selinux_context: set_selinux_context || context.is_some(),
149159
context,
@@ -211,24 +221,21 @@ pub fn uu_app() -> Command {
211221
)
212222
}
213223

214-
fnget_mode(str_mode:Option<&String>) ->Result<mode_t,String>{
215-
match str_mode{
216-
None =>Ok(MODE_RW_UGO),
217-
Some(str_mode) => uucore::mode::parse_mode(str_mode)
218-
.map_err(|e|{
219-
get_message_with_args(
220-
"mknod-error-invalid-mode",
221-
HashMap::from([("error".to_string(), e.to_string())]),
222-
)
223-
})
224-
.and_then(|mode|{
225-
if mode >0o777{
226-
Err(get_message("mknod-error-mode-permission-bits-only"))
227-
}else{
228-
Ok(mode)
229-
}
230-
}),
231-
}
224+
fnparse_mode(str_mode:&str) ->Result<mode_t,String>{
225+
uucore::mode::parse_mode(str_mode)
226+
.map_err(|e|{
227+
get_message_with_args(
228+
"mknod-error-invalid-mode",
229+
HashMap::from([("error".to_string(), e.to_string())]),
230+
)
231+
})
232+
.and_then(|mode|{
233+
if mode >0o777{
234+
Err(get_message("mknod-error-mode-permission-bits-only"))
235+
}else{
236+
Ok(mode)
237+
}
238+
})
232239
}
233240

234241
fnparse_type(tpe:&str) ->Result<FileType,String>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp