Skip to content

Commit

Permalink
Fix files.* facts when path is a link.
Browse files Browse the repository at this point in the history
These should still be checked and return `false` where the path is not
the expected type, rather than `null`. Using `test -e` does not work
for links as it attempts to follow them.
  • Loading branch information
Fizzadar committed Jan 8, 2022
1 parent 39c604b commit 6c1af9f
Show file tree
Hide file tree
Showing 13 changed files with 13 additions and 17 deletions.
6 changes: 1 addition & 5 deletions pyinfra/facts/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,14 @@ def _parse_mode(mode):


class File(FactBase):
# Types must match FLAG_TO_TYPE in .util.files.py
type = 'file'
test_flag = '-e'

def command(self, path):
return make_formatted_string_command((
'! test {test_flag} {0} || ' # only stat if the file exists
'! (test -e {0} || test -L {0} ) || ' # only stat if the path exists (file or symlink)
'( {linux_stat_command} {0} 2> /dev/null || {bsd_stat_command} {0} )'
),
QuoteString(path),
test_flag=self.test_flag,
linux_stat_command=LINUX_STAT_COMMAND,
bsd_stat_command=BSD_STAT_COMMAND,
)
Expand Down Expand Up @@ -118,7 +115,6 @@ def process(self, output):

class Link(File):
type = 'link'
test_flag = '-L'


class Directory(File):
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Directory/file.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/path/to/a/file",
"command": "! test -e /path/to/a/file || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /path/to/a/file 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /path/to/a/file )",
"command": "! (test -e /path/to/a/file || test -L /path/to/a/file ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /path/to/a/file 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /path/to/a/file )",
"output": [
"user=pyinfra group=pyinfra mode=-rwxrwxrwx atime=1594804767 mtime=1594804767 ctime=0 size=8 '/path/to/a/file'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Directory/link.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/mylink",
"command": "! test -e /home/pyinfra/mylink || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )",
"command": "! (test -e /home/pyinfra/mylink || test -L /home/pyinfra/mylink ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )",
"output": [
"user=root group=root mode=lrwxrwxrwx atime=1594804774 mtime=1594804770 ctime=0 size=6 '/home/pyinfra/mylink' -> 'file.txt'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Directory/valid.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/myd@-_ir",
"command": "! test -e /home/pyinfra/myd@-_ir || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )",
"command": "! (test -e /home/pyinfra/myd@-_ir || test -L /home/pyinfra/myd@-_ir ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )",
"output": [
"user=pyinfra group=pyinfra mode=drw-r--r-- atime=1594804583 mtime=1594804583 ctime=0 size=0 '/home/pyinfra/myd@-_ir'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.File/directory.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra",
"command": "! test -e /home/pyinfra || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra )",
"command": "! (test -e /home/pyinfra || test -L /home/pyinfra ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra )",
"output": [
"user=root group=root mode=drw-r--r-- atime=1594804583 mtime=1594804583 ctime=0 size=0 '/home/pyinfra'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.File/invalid_output.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/fil-@_e.txt",
"command": "! test -e /home/pyinfra/fil-@_e.txt || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )",
"command": "! (test -e /home/pyinfra/fil-@_e.txt || test -L /home/pyinfra/fil-@_e.txt ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )",
"output": [
"not-gonna-match"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.File/link.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/mylink",
"command": "! test -e /home/pyinfra/mylink || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )",
"command": "! (test -e /home/pyinfra/mylink || test -L /home/pyinfra/mylink ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/mylink 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/mylink )",
"output": [
"user=root group=root mode=lrwxrwxrwx atime=1594804774 mtime=1594804770 ctime=0 size=6 '/home/pyinfra/mylink' -> 'file.txt'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.File/valid.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/fil-@_e.txt",
"command": "! test -e /home/pyinfra/fil-@_e.txt || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )",
"command": "! (test -e /home/pyinfra/fil-@_e.txt || test -L /home/pyinfra/fil-@_e.txt ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )",
"output": [
"user=pyinfra group=domain users mode=-rwxrwx--- atime=1594804767 mtime=1594804767 ctime=0 size=8 '/home/pyinfra/fil-@_e.txt'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.File/valid_needs_quotes.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "fil () &&-@_e.txt",
"command": "! test -e 'fil () &&-@_e.txt' || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' 'fil () &&-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' 'fil () &&-@_e.txt' )",
"command": "! (test -e 'fil () &&-@_e.txt' || test -L 'fil () &&-@_e.txt' ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' 'fil () &&-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' 'fil () &&-@_e.txt' )",
"output": [
"user=pyinfra group=domain users mode=-rwxrwx--- atime=1594804767 mtime=1594804767 ctime=0 size=8 'fil () &&-@_e.txt'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.File/valid_with_space.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra dir/fil-@_e.txt",
"command": "! test -e '/home/pyinfra dir/fil-@_e.txt' || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' '/home/pyinfra dir/fil-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' '/home/pyinfra dir/fil-@_e.txt' )",
"command": "! (test -e '/home/pyinfra dir/fil-@_e.txt' || test -L '/home/pyinfra dir/fil-@_e.txt' ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' '/home/pyinfra dir/fil-@_e.txt' 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' '/home/pyinfra dir/fil-@_e.txt' )",
"output": [
"user=pyinfra group=pyinfra mode=-rw-r--r-- atime=1594804348 mtime=1594804348 ctime=0 size=0 '/home/pyinfra dir/fil-@_e.txt'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Link/directory.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/myd@-_ir",
"command": "! test -L /home/pyinfra/myd@-_ir || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )",
"command": "! (test -e /home/pyinfra/myd@-_ir || test -L /home/pyinfra/myd@-_ir ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/myd@-_ir 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/myd@-_ir )",
"output": [
"user=root group=root mode=drw-r--r-- atime=1594804583 mtime=1594804583 ctime=0 size=0 '/home/pyinfra/myd@-_ir'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Link/file.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/fil-@_e.txt",
"command": "! test -L /home/pyinfra/fil-@_e.txt || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )",
"command": "! (test -e /home/pyinfra/fil-@_e.txt || test -L /home/pyinfra/fil-@_e.txt ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/fil-@_e.txt 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/fil-@_e.txt )",
"output": [
"user=pyinfra group=pyinfra mode=-rwxrwxrwx atime=1594804767 mtime=1594804767 ctime=0 size=8 '/home/pyinfra/fil-@_e.txt'"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/facts/files.Link/valid.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "/home/pyinfra/my@-_link",
"command": "! test -L /home/pyinfra/my@-_link || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/my@-_link 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/my@-_link )",
"command": "! (test -e /home/pyinfra/my@-_link || test -L /home/pyinfra/my@-_link ) || ( stat -c 'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N' /home/pyinfra/my@-_link 2> /dev/null || stat -f 'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY' /home/pyinfra/my@-_link )",
"output": [
"user=pyinfra group=pyinfra mode=lrwxrwxrwx atime=1594804774 mtime=1594804770 ctime=0 size=6 '/home/pyinfra/my@-_link' -> '[email protected]'"
],
Expand Down

0 comments on commit 6c1af9f

Please sign in to comment.