[libvirt] [PATCH] Create file in virFileWriteStr() if it doesn't exist

Eric Blake eblake at redhat.com
Thu Dec 2 23:55:05 UTC 2010


On 12/02/2010 06:23 AM, Jean-Baptiste Rouault wrote:
> This patch adds a virFileWriteStrEx() function with a
> third parameter to set the created file permissions.

I'm not a fan of Microsoft's naming conventions (Ex conveys no meaning,
and it doesn't scale well to future extensions[1]).  Can we come up with
a better name, such as virFileWriteStrCreate?
[1] http://blogs.msdn.com/b/michkap/archive/2006/01/31/520694.aspx

Alternatively, I only counted 16 existing users of virFileWriteStr; and
this is an internal API.  We could easily rewrite all clients to always
pass a third parameter, and change the signature of virFileWriteStr to
require a mode_t argument.  Hmm; some of those clients are writing to
kernel files that should always exist (/proc/sys/net/ipv4/ip_forward,
for example), where it's tough to justify what we would pass as a mode_t
argument.  So maybe pass mode==0 as a sentinel to require a pre-existing
file (that is, even though open(,O_CREAT,0) is a valid syscall, it
seldom makes sense, since the resulting created file cannot be re-read
once closed):

int virFileWriteStr(const char *path, const char *str, mode_t mode) {
    int fd;

    if (mode == 0)
        fd = open(path, O_WRONLY|O_TRUNC);
    else
        fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
    if (fd == -1)
...

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20101202/5d5ca473/attachment-0001.sig>


More information about the libvir-list mailing list