Re: File permissions
From: Roger Leigh (${rleigh}_at_invalid.whinlatter.ukfsn.org.invalid)
Date: 10/30/05
- Previous message: puzzlecracker: "Re: suid clarification"
- In reply to: puzzlecracker: "File permissions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 30 Oct 2005 21:34:40 +0000
"puzzlecracker" <ironsel2000@gmail.com> writes:
> What is the quickest way to check if I have write permissions to and
> existence of the file and then execute a command on that file?
"Quickest" or "safest"? What is most appropriate depends upon what
you are needing to do, and you didn't give any detail.
See access(2) and stat(2).
However, consider that there will always be a delay between checking
if you have permission, and doing the task requiring the permission.
During that time the file ownership and permissions could have
changed, so it might be better to just do it and cope with any
possible error at that point.
> I am considerting using ostream to open the file- thus perror flag will
> be set if anything goes wrong and then execute the command... I ma just
> afraid that it is a waste of time to uselessly open the file...
[Please learn to use a spellchecker.]
C++ iostreams are terrible for stuff like permissions, since they are
too high-level. Use open(2), and if you must use iostreams, use a
__gnu_cxx::stdio_filebuf<> to construct a stream buffer from the fd
returned by open. You can then associate this with an iostream. You
can now use both the stream and the fd, so you can do stuff like fcntl
locking etc..
Note that this is not Standard C++; you'll need GCC, ideally >= 3.3.
Example:
#include <cstdio>
#include <fstream>
#include <ext/stdio_filebuf.h>
int
main (void)
{
// stream using a UNIX file descriptor
std::ofstream os;
__gnu_cxx::stdio_filebuf<char> fdbuf(STDOUT_FILENO, std::ios::out);
os.std::ios::rdbuf(&fdbuf);
os << "Hello, world!" << std::endl;
// stream using an ISO C FILE structure
__gnu_cxx::stdio_filebuf<char> filbuf(stdout, std::ios::out);
os.std::ios::rdbuf(&filbuf);
os << "Goodbye!" << std::endl;
return 0;
}
--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
- Previous message: puzzlecracker: "Re: suid clarification"
- In reply to: puzzlecracker: "File permissions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|