@@ -275,49 +275,48 @@ def hash_timestamp(afile):
275
275
return md5hex
276
276
277
277
278
- def _generate_cifs_table ():
279
- """Construct a reverse-length-ordered list of mount points that
280
- fall under a CIFS mount.
278
+ def _parse_mount_table (exit_code , output ):
279
+ """Parses the output of ``mount`` to produce (path, fs_type) pairs
281
280
282
- This precomputation allows efficient checking for whether a given path
283
- would be on a CIFS filesystem.
284
-
285
- On systems without a ``mount`` command, or with no CIFS mounts, returns an
286
- empty list.
281
+ Separated from _generate_cifs_table to enable testing logic with real
282
+ outputs
287
283
"""
288
- exit_code , output = sp .getstatusoutput ("mount" )
289
284
# Not POSIX
290
285
if exit_code != 0 :
291
286
return []
292
287
288
+ # Linux mount example: sysfs on /sys type sysfs (rw,nosuid,nodev,noexec)
289
+ # <PATH>^^^^ ^^^^^<FSTYPE>
290
+ # OSX mount example: /dev/disk2 on / (hfs, local, journaled)
291
+ # <PATH>^ ^^^<FSTYPE>
292
+ pattern = re .compile (r'.*? on (/.*?) (?:type |\()([^\s,]+)(?:, |\)| )' )
293
+
293
294
# (path, fstype) tuples, sorted by path length (longest first)
294
- mount_info = sorted (
295
- (line .split ()[2 :5 :2 ] for line in output .splitlines ()),
296
- key = lambda x : len (x [0 ]),
297
- reverse = True )
298
-
299
- # find which mount points are CIFS
300
- # init to empty list
301
- cifs_paths = []
302
-
303
- try :
304
- for path_and_fstype in mount_info :
305
- # need to check for tables that have only path and no fstype
306
- if len (path_and_fstype ) == 2 :
307
- # if this entry is cifs, add it to list
308
- if path_and_fstype [1 ] == 'cifs' :
309
- cifs_paths .append (path_and_fstype [0 ])
310
- else :
311
- fmlogger .debug ('mount file system types not described by fstype' )
312
- except :
313
- fmlogger .debug ('mount file system type check for CIFS error' )
314
- return []
295
+ mount_info = sorted ((pattern .match (l ).groups () for l in output .splitlines ()),
296
+ key = lambda x : len (x [0 ]), reverse = True )
297
+ cifs_paths = [path for path , fstype in mount_info
298
+ if fstype .lower () == 'cifs' ]
299
+
315
300
return [
316
301
mount for mount in mount_info
317
302
if any (mount [0 ].startswith (path ) for path in cifs_paths )
318
303
]
319
304
320
305
306
+ def _generate_cifs_table ():
307
+ """Construct a reverse-length-ordered list of mount points that
308
+ fall under a CIFS mount.
309
+
310
+ This precomputation allows efficient checking for whether a given path
311
+ would be on a CIFS filesystem.
312
+
313
+ On systems without a ``mount`` command, or with no CIFS mounts, returns an
314
+ empty list.
315
+ """
316
+ exit_code , output = sp .getstatusoutput ("mount" )
317
+ return _parse_mount_table (exit_code , output )
318
+
319
+
321
320
_cifs_table = _generate_cifs_table ()
322
321
323
322
0 commit comments