mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #29216 from clan/file_search_on_proc_file
size is 0 doesn't mean no data, e.g, /proc/version
This commit is contained in:
commit
6a2ffbfb7c
1 changed files with 45 additions and 42 deletions
|
@ -1421,58 +1421,61 @@ def replace(path,
|
|||
append_if_not_found) \
|
||||
else repl
|
||||
|
||||
# mmap throws a ValueError if the file is empty, but if it is empty we
|
||||
# should be able to skip the search anyway. NOTE: Is there a use case for
|
||||
# searching an empty file with an empty pattern?
|
||||
if filesize is not 0:
|
||||
try:
|
||||
# First check the whole file, determine whether to make the replacement
|
||||
# Searching first avoids modifying the time stamp if there are no changes
|
||||
r_data = None
|
||||
try:
|
||||
# Use a read-only handle to open the file
|
||||
with salt.utils.fopen(path,
|
||||
mode='rb',
|
||||
buffering=bufsize) as r_file:
|
||||
# Use a read-only handle to open the file
|
||||
with salt.utils.fopen(path,
|
||||
mode='rb',
|
||||
buffering=bufsize) as r_file:
|
||||
try:
|
||||
# mmap throws a ValueError if the file is empty.
|
||||
r_data = mmap.mmap(r_file.fileno(),
|
||||
0,
|
||||
access=mmap.ACCESS_READ)
|
||||
if search_only:
|
||||
# Just search; bail as early as a match is found
|
||||
if re.search(cpattern, r_data):
|
||||
return True # `with` block handles file closure
|
||||
else:
|
||||
result, nrepl = re.subn(cpattern, repl, r_data, count)
|
||||
except ValueError:
|
||||
# size of file in /proc is 0, but contains data
|
||||
r_data = "".join(r_file)
|
||||
if search_only:
|
||||
# Just search; bail as early as a match is found
|
||||
if re.search(cpattern, r_data):
|
||||
return True # `with` block handles file closure
|
||||
else:
|
||||
result, nrepl = re.subn(cpattern, repl, r_data, count)
|
||||
|
||||
# found anything? (even if no change)
|
||||
if nrepl > 0:
|
||||
# found anything? (even if no change)
|
||||
if nrepl > 0:
|
||||
found = True
|
||||
# Identity check the potential change
|
||||
has_changes = True if pattern != repl else has_changes
|
||||
|
||||
if prepend_if_not_found or append_if_not_found:
|
||||
# Search for content, to avoid pre/appending the
|
||||
# content if it was pre/appended in a previous run.
|
||||
if re.search('^{0}$'.format(re.escape(content)),
|
||||
r_data,
|
||||
flags=flags_num):
|
||||
# Content was found, so set found.
|
||||
found = True
|
||||
# Identity check the potential change
|
||||
has_changes = True if pattern != repl else has_changes
|
||||
|
||||
if prepend_if_not_found or append_if_not_found:
|
||||
# Search for content, to avoid pre/appending the
|
||||
# content if it was pre/appended in a previous run.
|
||||
if re.search('^{0}$'.format(re.escape(content)),
|
||||
r_data,
|
||||
flags=flags_num):
|
||||
# Content was found, so set found.
|
||||
found = True
|
||||
# Keep track of show_changes here, in case the file isn't
|
||||
# modified
|
||||
if show_changes or append_if_not_found or \
|
||||
prepend_if_not_found:
|
||||
orig_file = r_data.read(filesize).splitlines(True) \
|
||||
if hasattr(r_data, 'read') \
|
||||
else r_data.splitlines(True)
|
||||
new_file = result.splitlines(True)
|
||||
|
||||
# Keep track of show_changes here, in case the file isn't
|
||||
# modified
|
||||
if show_changes or append_if_not_found or \
|
||||
prepend_if_not_found:
|
||||
orig_file = r_data.read(filesize).splitlines(True)
|
||||
new_file = result.splitlines(True)
|
||||
|
||||
except (OSError, IOError) as exc:
|
||||
raise CommandExecutionError(
|
||||
"Unable to open file '{0}'. "
|
||||
"Exception: {1}".format(path, exc)
|
||||
)
|
||||
finally:
|
||||
if r_data and isinstance(r_data, mmap.mmap):
|
||||
r_data.close()
|
||||
except (OSError, IOError) as exc:
|
||||
raise CommandExecutionError(
|
||||
"Unable to open file '{0}'. "
|
||||
"Exception: {1}".format(path, exc)
|
||||
)
|
||||
finally:
|
||||
if r_data and isinstance(r_data, mmap.mmap):
|
||||
r_data.close()
|
||||
|
||||
# Just search. We've searched the whole file now; if we didn't return True
|
||||
# already, then the pattern isn't present, so return False.
|
||||
|
|
Loading…
Add table
Reference in a new issue