Is it better to use fchmod over chmod?
Is using fchmod(in开发者_如何学Pythont fildes, mode_t mode)
a better idea than using chmod(const char * path, mode_t mode)?
It's pretty much identical. chmod
will take ever so slightly little longer as it has to convert the path to an inode or filenode, whereas fchmod
has the inode/filenode already looked up.
Of course, there are fewer error conditions which could occur with fchmod
since the file is already known to exist, have permission for open, etc.
It depends on whether race conditions are a concern or not. With chmod
you run the risk of someone renaming the file out from under you and chmodding the wrong file. In certain situations (especially if you're root) this can be a huge security hole.
If you are processing the contents of a file, you already have the file descriptor (either because you used open()
to get it, or because you used fopen()
and can use fileno()
on the FILE *
pointer to get it), you can use fchmod()
. If you don't want to open the file for any processing but just change mode, use chmod()
.
精彩评论