Revision 778b75bb lib/backend.py
b/lib/backend.py | ||
---|---|---|
1417 | 1417 |
return result |
1418 | 1418 |
|
1419 | 1419 |
|
1420 |
def _TransformFileStorageDir(file_storage_dir): |
|
1421 |
"""Checks whether given file_storage_dir is valid. |
|
1422 |
|
|
1423 |
Checks wheter the given file_storage_dir is within the cluster-wide |
|
1424 |
default file_storage_dir stored in SimpleStore. Only paths under that |
|
1425 |
directory are allowed. |
|
1426 |
|
|
1427 |
Args: |
|
1428 |
file_storage_dir: string with path |
|
1429 |
|
|
1430 |
Returns: |
|
1431 |
normalized file_storage_dir (string) if valid, None otherwise |
|
1432 |
|
|
1433 |
""" |
|
1434 |
file_storage_dir = os.path.normpath(file_storage_dir) |
|
1435 |
base_file_storage_dir = ssconf.SimpleStore().GetFileStorageDir() |
|
1436 |
if (not os.path.commonprefix([file_storage_dir, base_file_storage_dir]) == |
|
1437 |
base_file_storage_dir): |
|
1438 |
logger.Error("file storage directory '%s' is not under base file" |
|
1439 |
" storage directory '%s'" % |
|
1440 |
(file_storage_dir, base_file_storage_dir)) |
|
1441 |
return None |
|
1442 |
return file_storage_dir |
|
1443 |
|
|
1444 |
|
|
1445 |
def CreateFileStorageDir(file_storage_dir): |
|
1446 |
"""Create file storage directory. |
|
1447 |
|
|
1448 |
Args: |
|
1449 |
file_storage_dir: string containing the path |
|
1450 |
|
|
1451 |
Returns: |
|
1452 |
tuple with first element a boolean indicating wheter dir |
|
1453 |
creation was successful or not |
|
1454 |
|
|
1455 |
""" |
|
1456 |
file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
|
1457 |
result = True, |
|
1458 |
if not file_storage_dir: |
|
1459 |
result = False, |
|
1460 |
else: |
|
1461 |
if os.path.exists(file_storage_dir): |
|
1462 |
if not os.path.isdir(file_storage_dir): |
|
1463 |
logger.Error("'%s' is not a directory" % file_storage_dir) |
|
1464 |
result = False, |
|
1465 |
else: |
|
1466 |
try: |
|
1467 |
os.makedirs(file_storage_dir, 0750) |
|
1468 |
except OSError, err: |
|
1469 |
logger.Error("Cannot create file storage directory '%s': %s" % |
|
1470 |
(file_storage_dir, err)) |
|
1471 |
result = False, |
|
1472 |
return result |
|
1473 |
|
|
1474 |
|
|
1475 |
def RemoveFileStorageDir(file_storage_dir): |
|
1476 |
"""Remove file storage directory. |
|
1477 |
|
|
1478 |
Remove it only if it's empty. If not log an error and return. |
|
1479 |
|
|
1480 |
Args: |
|
1481 |
file_storage_dir: string containing the path |
|
1482 |
|
|
1483 |
Returns: |
|
1484 |
tuple with first element a boolean indicating wheter dir |
|
1485 |
removal was successful or not |
|
1486 |
|
|
1487 |
""" |
|
1488 |
file_storage_dir = _TransformFileStorageDir(file_storage_dir) |
|
1489 |
result = True, |
|
1490 |
if not file_storage_dir: |
|
1491 |
result = False, |
|
1492 |
else: |
|
1493 |
if os.path.exists(file_storage_dir): |
|
1494 |
if not os.path.isdir(file_storage_dir): |
|
1495 |
logger.Error("'%s' is not a directory" % file_storage_dir) |
|
1496 |
result = False, |
|
1497 |
# deletes dir only if empty, otherwise we want to return False |
|
1498 |
try: |
|
1499 |
os.rmdir(file_storage_dir) |
|
1500 |
except OSError, err: |
|
1501 |
logger.Error("Cannot remove file storage directory '%s': %s" % |
|
1502 |
(file_storage_dir, err)) |
|
1503 |
result = False, |
|
1504 |
return result |
|
1505 |
|
|
1506 |
|
|
1507 |
def RenameFileStorageDir(old_file_storage_dir, new_file_storage_dir): |
|
1508 |
"""Rename the file storage directory. |
|
1509 |
|
|
1510 |
Args: |
|
1511 |
old_file_storage_dir: string containing the old path |
|
1512 |
new_file_storage_dir: string containing the new path |
|
1513 |
|
|
1514 |
Returns: |
|
1515 |
tuple with first element a boolean indicating wheter dir |
|
1516 |
rename was successful or not |
|
1517 |
|
|
1518 |
""" |
|
1519 |
old_file_storage_dir = _TransformFileStorageDir(old_file_storage_dir) |
|
1520 |
new_file_storage_dir = _TransformFileStorageDir(new_file_storage_dir) |
|
1521 |
result = True, |
|
1522 |
if not old_file_storage_dir or not new_file_storage_dir: |
|
1523 |
result = False, |
|
1524 |
else: |
|
1525 |
if not os.path.exists(new_file_storage_dir): |
|
1526 |
if os.path.isdir(old_file_storage_dir): |
|
1527 |
try: |
|
1528 |
os.rename(old_file_storage_dir, new_file_storage_dir) |
|
1529 |
except OSError, err: |
|
1530 |
logger.Error("Cannot rename '%s' to '%s': %s" |
|
1531 |
% (old_file_storage_dir, new_file_storage_dir, err)) |
|
1532 |
result = False, |
|
1533 |
else: |
|
1534 |
logger.Error("'%s' is not a directory" % old_file_storage_dir) |
|
1535 |
result = False, |
|
1536 |
else: |
|
1537 |
if os.path.exists(old_file_storage_dir): |
|
1538 |
logger.Error("Cannot rename '%s' to '%s'. Both locations exist." % |
|
1539 |
old_file_storage_dir, new_file_storage_dir) |
|
1540 |
result = False, |
|
1541 |
return result |
|
1542 |
|
|
1543 |
|
|
1420 | 1544 |
class HooksRunner(object): |
1421 | 1545 |
"""Hook runner. |
1422 | 1546 |
|
Also available in: Unified diff