12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: fs:: { read_dir, DirEntry , OpenOptions } ;
15
+ use std:: fs:: { metadata , read_dir, DirEntry , OpenOptions } ;
16
16
use std:: io:: { self , copy, Write } ;
17
17
use std:: os:: unix:: ffi:: OsStringExt ;
18
18
use std:: os:: unix:: fs:: { MetadataExt , PermissionsExt } ;
@@ -108,22 +108,20 @@ fn try_read_key_file(
108
108
bail ! ( "{} is a dotfile, ignoring" , ent. path( ) . display( ) ) ;
109
109
}
110
110
111
- // check file permissions
111
+ // check file type and permissions
112
+ if !metadata ( ent. path ( ) )
113
+ . chain_err ( || format ! ( "couldn't stat {}" , ent. path( ) . display( ) ) ) ?
114
+ . is_file ( )
115
+ {
116
+ bail ! ( "{} is not a file, ignoring" , ent. path( ) . display( ) ) ;
117
+ }
112
118
ensure_safe_permissions ( & ent. path ( ) ) ?;
113
119
114
120
// open file
115
121
let mut file = OpenOptions :: new ( )
116
122
. read ( true )
117
123
. open ( ent. path ( ) )
118
124
. chain_err ( || format ! ( "opening {}" , ent. path( ) . display( ) ) ) ?;
119
- // avoid writing out the path comment if we have a directory
120
- if !file
121
- . metadata ( )
122
- . chain_err ( || format ! ( "getting metadata for {}" , ent. path( ) . display( ) ) ) ?
123
- . is_file ( )
124
- {
125
- bail ! ( "{} is not a file, ignoring" , ent. path( ) . display( ) ) ;
126
- }
127
125
128
126
// write comment with source path
129
127
let safe_path = ent. path ( ) . to_string_lossy ( ) . replace ( "\n " , "\u{fffd} " ) ;
@@ -150,6 +148,8 @@ mod tests {
150
148
use std:: io:: Cursor ;
151
149
use std:: os:: unix:: fs:: symlink;
152
150
151
+ use nix:: sys:: stat;
152
+ use nix:: unistd:: mkfifo;
153
153
use tempfile:: { Builder , TempDir } ;
154
154
155
155
fn make_file ( path : & Path , contents : & str ) -> Result < ( ) > {
@@ -195,6 +195,8 @@ mod tests {
195
195
symlink ( & keydir. join ( "d" ) , & keydir. join ( "sd" ) ) ?;
196
196
// dangling symlink
197
197
symlink ( & keydir. join ( "nx" ) , & keydir. join ( "snx" ) ) ?;
198
+ // fifo
199
+ mkfifo ( & keydir. join ( "fifo" ) , stat:: Mode :: S_IRUSR ) ?;
198
200
199
201
Ok ( dir)
200
202
}
@@ -255,8 +257,9 @@ Caused by: Permission denied (os error 13)
255
257
256
258
Error: {dir}/d is not a file, ignoring
257
259
258
- Error: opening {dir}/dnp
259
- Caused by: Permission denied (os error 13)
260
+ Error: {dir}/dnp is not a file, ignoring
261
+
262
+ Error: {dir}/fifo is not a file, ignoring
260
263
261
264
Error: {dir}/sd is not a file, ignoring
262
265
0 commit comments