file.append doesn't work well with proc files

54 views
Skip to first unread message

Sandip Bhattacharya

unread,
May 30, 2014, 5:31:33 PM5/30/14
to salt-...@googlegroups.com
The current smart file.append adds a newline before the provided content if the
file doesn't end with a newline. Is this really necessary? If someone needs the
newline, I am guessing they will add it to the text they want added.

This behaviour current breaks writing to proc files, because of their response
to fseek.

When i am running:
$ sudo salt-call --local file.append /sys/module/printk/parameters/time 1
...
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
salt_call()
File "/usr/lib/python2.6/site-packages/salt/scripts.py", line
82, in salt_call
client.run()
File "/usr/lib/python2.6/site-packages/salt/cli/__init__.py",
line 314, in run
caller.run()
File "/usr/lib/python2.6/site-packages/salt/cli/caller.py", line
142, in run
ret = self.call()
File "/usr/lib/python2.6/site-packages/salt/cli/caller.py", line
80, in call
ret['return'] = func(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/salt/modules/file.py",
line 1403, in append
ofile.write('{0}\n'.format(line))
IOError: [Errno 22] Invalid argument

What I am expecting is:
$ echo -ne '1\n' | sudo tee /sys/module/printk/parameters/time
1

What is happening is:
$ echo -ne '\n1\n' | sudo tee /sys/module/printk/parameters/time

1
tee: /sys/module/printk/parameters/time: Invalid argument

This is because proc type files behave differently to seek:

>>> f=open("/sys/module/printk/parameters/time","r+")
>>> f.read()
'Y\n'
>>> f.tell()
2
>>> import os
>>> f.seek(-1,os.SEEK_END)
>>> f.tell()
4095
>>> f.read(1)
''

If we have just a dumb open("FNAME","a").write(TEXT) like before, this would
work, like it does in the previous version of this function.

- Sandip

Arnold Bechtoldt

unread,
May 31, 2014, 6:29:32 AM5/31/14
to salt-...@googlegroups.com
AFAIR I've encountered the same issue. Instead of using Salt's file
state module I built a cmd.run around an echo.

Simple but working.

https://github.com/bechtoldt/sysctl-formula


Arnold

--
Arnold Bechtoldt

Karlsruhe, Germany
0xE2356889.asc
signature.asc

Colton Myers

unread,
Jun 9, 2014, 6:15:24 PM6/9/14
to salt-...@googlegroups.com
So the general case for `file.append` is, from what I've seen, appending configuration options to config files and similar operations.  In these cases you assume that the file ends in a newline.  That's why it's adding a newline if there isn't one.

We should probably add a flag so that this behavior can be toggled.  Could you open an issue on Github?

--
Colton Myers
Reply all
Reply to author
Forward
0 new messages