diff options
author | Ćukasz 'sil2100' Zemczak <lukasz.zemczak@ubuntu.com> | 2017-09-04 10:27:07 +0200 |
---|---|---|
committer | usd-importer <ubuntu-server@lists.ubuntu.com> | 2017-09-04 09:38:24 +0000 |
commit | 185ceb32fea5d5c2a43d7b6ee2a40228489055f4 (patch) | |
tree | 2e1c9cc42510c4a922cf63fa265ec0e1945ec14b /azurelinuxagent/common/utils/fileutil.py | |
parent | 43bdf9debe5377216aed0086bff2aad864f6ba82 (diff) | |
download | vyos-walinuxagent-185ceb32fea5d5c2a43d7b6ee2a40228489055f4.tar.gz vyos-walinuxagent-185ceb32fea5d5c2a43d7b6ee2a40228489055f4.zip |
Import patches-unapplied version 2.2.16-0ubuntu1 to ubuntu/artful-proposed
Imported using git-ubuntu import.
Changelog parent: 43bdf9debe5377216aed0086bff2aad864f6ba82
New changelog entries:
* New upstream release (LP: #1714299).
Diffstat (limited to 'azurelinuxagent/common/utils/fileutil.py')
-rw-r--r-- | azurelinuxagent/common/utils/fileutil.py | 56 |
1 files changed, 51 insertions, 5 deletions
diff --git a/azurelinuxagent/common/utils/fileutil.py b/azurelinuxagent/common/utils/fileutil.py index bae1957..96b5b82 100644 --- a/azurelinuxagent/common/utils/fileutil.py +++ b/azurelinuxagent/common/utils/fileutil.py @@ -21,15 +21,30 @@ File operation util functions """ +import errno as errno import glob import os +import pwd import re import shutil -import pwd +import string + import azurelinuxagent.common.logger as logger -from azurelinuxagent.common.future import ustr import azurelinuxagent.common.utils.textutil as textutil +from azurelinuxagent.common.future import ustr + +KNOWN_IOERRORS = [ + errno.EIO, # I/O error + errno.ENOMEM, # Out of memory + errno.ENFILE, # File table overflow + errno.EMFILE, # Too many open files + errno.ENOSPC, # Out of space + errno.ENAMETOOLONG, # Name too long + errno.ELOOP, # Too many symbolic links encountered + errno.EREMOTEIO # Remote I/O error +] + def copy_file(from_path, to_path=None, to_dir=None): if to_path is None: to_path = os.path.join(to_dir, os.path.basename(from_path)) @@ -160,18 +175,31 @@ def chmod_tree(path, mode): for file_name in files: os.chmod(os.path.join(root, file_name), mode) -def findstr_in_file(file_path, pattern_str): +def findstr_in_file(file_path, line_str): + """ + Return True if the line is in the file; False otherwise. + (Trailing whitespace is ignore.) + """ + try: + for line in (open(file_path, 'r')).readlines(): + if line_str == line.rstrip(): + return True + except Exception as e: + pass + return False + +def findre_in_file(file_path, line_re): """ Return match object if found in file. """ try: - pattern = re.compile(pattern_str) + pattern = re.compile(line_re) for line in (open(file_path, 'r')).readlines(): match = re.search(pattern, line) if match: return match except: - raise + pass return None @@ -184,3 +212,21 @@ def get_all_files(root_path): result.extend([os.path.join(root, file) for file in files]) return result + +def clean_ioerror(e, paths=[]): + """ + Clean-up possibly bad files and directories after an IO error. + The code ignores *all* errors since disk state may be unhealthy. + """ + if isinstance(e, IOError) and e.errno in KNOWN_IOERRORS: + for path in paths: + if path is None: + continue + + try: + if os.path.isdir(path): + shutil.rmtree(path, ignore_errors=True) + else: + os.remove(path) + except Exception as e: + pass |