Re: File permissions

From: Roger Leigh (${rleigh}_at_invalid.whinlatter.ukfsn.org.invalid)
Date: 10/30/05

  • Next message: Maxim Yegorushkin: "Re: mmap and I/O error"
    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.
    

  • Next message: Maxim Yegorushkin: "Re: mmap and I/O error"

    Relevant Pages

    • Re: group membership needed for looking at network usage?
      ... still run it by supplying the full path in the command. ... E.g. several programs run only as root; ... the user has execute permission. ...
      (comp.os.linux.networking)
    • Re: [PHP] Not able to execute Linux binary
      ... On Monday 08 December 2003 12:56, Karam Chand wrote: ... > I think there is some problem with the permission. ... > Even if I execute a command like - ...
      (php.general)
    • Problem accessing Local Activation Permission for Application
      ... and run an executable (for a file extraction program, ... I can run the same exact command line manually with no problems. ... The machine-default permission settings do not grant Local Activation ... Does the error mean that the ASPNET user account cannot execute the program? ...
      (microsoft.public.windowsxp.security_admin)
    • Problem accessing Local Activation Permission for Application
      ... and run an executable (for a file extraction program, ... I can run the same exact command line manually with no problems. ... The machine-default permission settings do not grant Local Activation ... Does the error mean that the ASPNET user account cannot execute the program? ...
      (microsoft.public.dotnet.framework.aspnet)
    • Re: System.Security.SecurityException was unhandled
      ... The exception gave you the CLSID. ... the first thing to check might be whether the COM server ... assembly actually has the permission in question. ... When I execute the application I received and error message. ...
      (microsoft.public.dotnet.security)

  • Quantcast