Refactor file execution modules #67261
Replies: 16 comments
-
Kind of related, #23226 |
Beta Was this translation helpful? Give feedback.
-
@twangboy seems this is a feature request but I would like to get your opinion on this. Is this something we would like to do? This would change the way we approach the setup of this module. |
Beta Was this translation helpful? Give feedback.
-
@Ch3LL In the issue he mentions in the above feature request I suggested putting shared functions in a salt util. That might be a good way to clean this up. |
Beta Was this translation helpful? Give feedback.
-
The structure has been talked about before in the renaming the modules thread. Something along the lines of I see two ways of doing this (others may see more ways): In terms of file in general "File" should not be directly changed, they should be written out to a tmp file and then renamed, this avoids issues like disk space running out and having half files. Before the file is replaced it should be backed up/versions in a salt backup directory. "File" should perform all operations for "a" target file in one hit. i.e. accumulated the required changes from various adjustments in different state files. "File" should support ordering of operations against a target file "File" needs to track files it manages. No file operations should happen from any state/execution module with out "File" knowing about it. e.g. pick up one state wants "\r\n" and another wants "\n" or that a module wants to manage the file and report an error. Maybe allow modules in virtual() to notify "File" that they manage a file and indicate if its exclusive or open relationship. Their are two end of line markers "\r\n" and "\n" . If their is a source file e.g. template it should determine the line feed. But it should also be possible to select platform default or force it or use existing with a select fail back. Determine the file default end of line maker should be seeking to the end of the file -2 and reading the last two characters in the file. |
Beta Was this translation helpful? Give feedback.
-
@damon-atkins Thanks for your comments. It really makes sense to have a common scheme for dealing with platform-specifics. So we should try to use the same style for the Regarding line endings, I think that #14135 gives some good ideas. In particular, I agree with the idea that it should be possible to use the same templates for Linux and Windows minions and do the conversion in the file module. Another thing that maybe should be considered at the same time is conversion between different encodings. I think that most systems use UTF-8 nowadays, but I would not be surprised to find legacy applications that might still use something like ISO 8859-1 or CP-1252. Of course, it must also be possible to disable all conversions so that binary files can be managed in a reliable way. Regarding mmap vs regular I/O, I still maintain my opinion that for the tasks handled by the In #39882 you wrote that "Python is not multi-threaded". I answer here because I want to keep the discussion in #39882 specific to the PR. Python in fact is multi-threaded (please refer to the documentation of the It might be that Python's What I am trying to say is that in my experience using mmap usually is not worth the trouble. I learned about all these nasty details when I had to use mmap for communicating with a device with a kernel driver that only allowed access with mmap. As there is no benefit in performance (refer to these benchmarks for details), avoiding mmap is a good choice in my experience. |
Beta Was this translation helpful? Give feedback.
-
Salt currently uses |
Beta Was this translation helpful? Give feedback.
-
@lorengordon Thanks for your input. If Do you happen to know how Python’s |
Beta Was this translation helpful? Give feedback.
-
@smarsching seems likely that The tradeoffs seem worth it to me to use I can see how it might be sub-ideal for |
Beta Was this translation helpful? Give feedback.
-
If your just copying a file then yes 1MB reads and writes are best. Currently lot of the code reason a complete file into python ram, and does not even check the size of it first. mmap is better if your reading a file, and need to read behind the current file position. I was talking about just using it on the read side. Should not need to use it on the write side. Salt should not edit files in place. i.e. it should write a new file, set perms on the new file and move it over the top of the old file. (unless the file does not exist in the first place) 32bit Python has 32bit limits |
Beta Was this translation helpful? Give feedback.
-
I agree with you that implementing search and replace is much simpler when using |
Beta Was this translation helpful? Give feedback.
-
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue. |
Beta Was this translation helpful? Give feedback.
-
. |
Beta Was this translation helpful? Give feedback.
-
Thank you for updating this issue. It is no longer marked as stale. |
Beta Was this translation helpful? Give feedback.
-
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue. |
Beta Was this translation helpful? Give feedback.
-
Not stale |
Beta Was this translation helpful? Give feedback.
-
Thank you for updating this issue. It is no longer marked as stale. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The
file
execution module has different implementations for Windows and non-Windows systems. In the following, the term POSIX will be used for all non-Windows systems, even though some of them might not strictly adhere to the POSIX specification.Status quo
At the moment, the
file
module provides the functions for POSIX systems and thewin_file
module for Windows systems. On Windows systems,file
is actually an alias forwin_file
. However, thewin_file
module actually uses functions from thefile
module. As some of these functions depend on other functions from thefile
module, but these functions are actually overridden by thewin_file
module, thewin_file
module does not simply call the functions in thefile
module. Instead, it selectively imports some of the functions, wrapping them with a new context so that they effectively run in the context of thewin_file
module.While this concept works, there are a few drawbacks:
win_file
module has to be aware of internal implementation details of thefile
module. For example, when a function from thefile
module internally calls another function that is private to thefile
module, thewin_file
module still has to import this function (possibly also wrapping it in a new context).win_file
module is not very clean and only readable for experts that understand the logic behind binding the functions from thefile
module to a new context.These drawbacks are not just of a theoretical nature, but have lead to actual bugs (e.g. #39883).
Proposal
The logic for the
file
module should actually be broken into three parts:posix_file
module that providesfile
on POSIX systems.win_file
module that providesfile
on Windows systems.For implementing the shared libary, providing an abstract class seems like the most reasonable approach. The platform-specific modules can then both have more specific classes that implement the platform-specific method and - if needed - override methods in order to adjust them to platform specifics.
This new structure achieves several goals:
win_file
module.Beta Was this translation helpful? Give feedback.
All reactions