On 2013-08-08, Markus Osterhoff wrote:
> * Dahong Tang [130808 04:59]:
> > > $ FILE=/tmp/testfile
> > > $ sudo touch $FILE
> > > $ sudo chmod 400 $FILE
> > Thanks Sven. Your explanation makes sense if I used the command vim
> > $FILE, but I used sudo vim $FILE. Why does vim even bother to set the
> > file to readonly when I used sudo? That is the part that I don't
> > understand. 
> 
> Because you told it so: If a file has no (w)rite permission (as in 400,
> which is r--), vim would open it as read-only, so a
>       :w
> fails. But now that you are root (sudo), no-one prevents you from
> “changing” the flags, writing, and “changing flags back”. I quoted this,
> since I assume that the Linux kernel just allows root to write files
> that she/he has no obvious write permissions for.
> 
> Short: if vim sees a file without w-permission, it opens it as readonly;
> but root may overwrite it nevertheless.

Not just root but any user who is the owner of the file.  You can
repeat Sven's demonstration as a normal user.

    touch testfile
    chmod 400 testfile
    vim testfile

    :w

will fail but

    :w!

will succeed in writing the file.  See ":help :w" where is says that
"[t]his may change the permission and ownership of the file...."

You can see how Vim does this on Unix by looking at this section of
the buf_write() function in fileio.c:


    #if defined(UNIX) && !defined(ARCHIE)
        /* When using ":w!" and the file was read-only: make it writable */
        if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid()
                                         && vim_strchr(p_cpo, CPO_FWRITE) == 
NULL)
        {
            perm |= 0200;
            (void)mch_setperm(fname, perm);
            made_writable = TRUE;
        }
    #endif


Regards,
Gary

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to