Revision 2dbc6857
b/lib/utils/io.py | ||
---|---|---|
38 | 38 |
#: Path generating random UUID |
39 | 39 |
_RANDOM_UUID_FILE = "/proc/sys/kernel/random/uuid" |
40 | 40 |
|
41 |
#: Directory used by fsck(8) to store recovered data, usually at a file |
|
42 |
#: system's root directory |
|
43 |
_LOST_AND_FOUND = "lost+found" |
|
44 |
|
|
41 | 45 |
|
42 | 46 |
def ReadFile(file_name, size=-1, preread=None): |
43 | 47 |
"""Reads a file. |
... | ... | |
467 | 471 |
return backup_name |
468 | 472 |
|
469 | 473 |
|
470 |
def ListVisibleFiles(path): |
|
474 |
def ListVisibleFiles(path, _is_mountpoint=os.path.ismount):
|
|
471 | 475 |
"""Returns a list of visible files in a directory. |
472 | 476 |
|
473 | 477 |
@type path: str |
... | ... | |
480 | 484 |
if not IsNormAbsPath(path): |
481 | 485 |
raise errors.ProgrammerError("Path passed to ListVisibleFiles is not" |
482 | 486 |
" absolute/normalized: '%s'" % path) |
483 |
files = [i for i in os.listdir(path) if not i.startswith(".")] |
|
484 |
return files |
|
487 |
|
|
488 |
mountpoint = _is_mountpoint(path) |
|
489 |
|
|
490 |
def fn(name): |
|
491 |
"""File name filter. |
|
492 |
|
|
493 |
Ignores files starting with a dot (".") as by Unix convention they're |
|
494 |
considered hidden. The "lost+found" directory found at the root of some |
|
495 |
filesystems is also hidden. |
|
496 |
|
|
497 |
""" |
|
498 |
return not (name.startswith(".") or |
|
499 |
(mountpoint and name == _LOST_AND_FOUND and |
|
500 |
os.path.isdir(os.path.join(path, name)))) |
|
501 |
|
|
502 |
return filter(fn, os.listdir(path)) |
|
485 | 503 |
|
486 | 504 |
|
487 | 505 |
def EnsureDirs(dirs): |
b/test/ganeti.utils.io_unittest.py | ||
---|---|---|
239 | 239 |
self.failUnlessRaises(errors.ProgrammerError, utils.ListVisibleFiles, |
240 | 240 |
"/bin/../tmp") |
241 | 241 |
|
242 |
def testMountpoint(self): |
|
243 |
lvfmp_fn = compat.partial(utils.ListVisibleFiles, |
|
244 |
_is_mountpoint=lambda _: True) |
|
245 |
self.assertEqual(lvfmp_fn(self.path), []) |
|
246 |
|
|
247 |
# Create "lost+found" as a regular file |
|
248 |
self._CreateFiles(["foo", "bar", ".baz", "lost+found"]) |
|
249 |
self.assertEqual(set(lvfmp_fn(self.path)), |
|
250 |
set(["foo", "bar", "lost+found"])) |
|
251 |
|
|
252 |
# Replace "lost+found" with a directory |
|
253 |
laf_path = utils.PathJoin(self.path, "lost+found") |
|
254 |
utils.RemoveFile(laf_path) |
|
255 |
os.mkdir(laf_path) |
|
256 |
self.assertEqual(set(lvfmp_fn(self.path)), set(["foo", "bar"])) |
|
257 |
|
|
258 |
def testLostAndFoundNoMountpoint(self): |
|
259 |
files = ["foo", "bar", ".Hello World", "lost+found"] |
|
260 |
expected = ["foo", "bar", "lost+found"] |
|
261 |
self._test(files, expected) |
|
262 |
|
|
242 | 263 |
|
243 | 264 |
class TestWriteFile(unittest.TestCase): |
244 | 265 |
def setUp(self): |
Also available in: Unified diff