Note that your specification explicitly talks about newly created files. You couldn't even use chmod
to change that, since a new file would have some set of permissions when created, before you could use chmod
on them.
Also, while that Ruby script doesn't run the chmod
command/utility, it does use the same underlying system call, so it's practically the same thing. If it were me, I wouldn't allow that.
Without giving out the full answer, I'd suggest looking at what the man page for open()
says about the permissions of the created file:
The mode argument specifies the file mode bits be applied when
a new file is created. This argument must be supplied when
O_CREAT
or O_TMPFILE
is specified in flags; if neither O_CREAT
nor O_TMPFILE
is specified, then mode is ignored. The
effective mode is modified by the process's umask in the usual
way: in the absence of a default ACL, the mode of the created
file is (mode & ~umask)
. Note that this mode applies only to
future accesses of the newly created file; the open()
call
that creates a read-only file may well return a read/write
file descriptor.
The following symbolic constants are provided for mode:
S_IRWXU
00700
user (file owner) has read, write, and execute
permission
[etc.]
Also note that the part you quoted said to "decrease the permissions by 222", not to 222. That's also important regarding the function I'm thinking of.
Since it was already mentioned in the comments
I'm thinking of umask
, which limits the permissions of newly-created files. It should be a standard feature, and should work the same in Linux and in DragonFly.
Of course, all of this is just my interpretation of that assignment. There's no way for any of us here to know for sure what the correct answer is in the opinion of your teacher/professor, especially since sometimes the answers given by professors are not even the correct ones.
stat
to get the current perms, then "decrease" them by 222 (e.g. by AND-ing them with "not 222"), theninstall -m "$newperms" "$origfile" "$tempfile"
andmv -f $tempfile "$origfile"
(but this will break any hard links) – cas Aug 26 '19 at 06:11umask
. As this is homework, I won't give you an exact answer, but that ought to be enough to point you in the right direction. If you solve the problem using this, please write it up as an answer. – cas Aug 26 '19 at 06:44install -m $(expr $(stat F2 | awk -F' ' '{print $2}' | sed '4q;d' | sed s/[^0-9]//g) - 222) F2 F2_cp && mv F2_cp F2 -f
This worked on a GNU/Linux system. On Unix however, stat shows results differently... So it doesn't work! – 15 Volts Aug 26 '19 at 10:55umask
command, then I suggest that you read its manual again. You have the solution to you issue, you just haven't put the pieces together in the right order (literally). – Kusalananda Aug 26 '19 at 12:09