Re: [Vala] problem writing to a file that needs root permission



On Sun, Oct 11, 2009 at 09:30:38 +0200, a a wrote:
Hello.I'd like to replace the content of the file /etc/sudoers .
In the terminal I can do it using
echo '#attempt' | gksudo tee /etc/sudoers

I've tried this with no success:[Permission denied]

 private void copy_content () {
        try {
                string file_target="/etc/sudoers";
                string file_source =
"/home/one/Desktop/sudoersoriginal.txt";
                var file = File.new_for_path(file_source);
                var thefile_stream = file.copy
(File.new_for_path(file_target), FileCopyFlags.OVERWRITE, null, null);
        } catch (Error e) {
          stderr.printf ("Error: %s\n", e.message);
        }
    }

How should I do it?

Well, if you want to write to a file, you need to correct permissions and
that would be true in any language. In terminal, you are getting it with
gksudo, which is suid to root. From a program, you have several options:

 1. Set it suid root (chown root <program>; chmod u+s <program> -- obviously
    you need to do this as root).

 2. Invoke gksudo from your program just like you do in shell.
    Using some of the GLib.Process.spawn_* function.

 3. Use the libgksu2 library to get the gksudo functionality.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]