Results 1 to 3 of 3

Thread: File creation ignores S_IWOTH param

  1. #1
    Join Date
    Nov 2010
    Location
    Down the rabbit hole
    Beans
    435
    Distro
    Ubuntu Development Release

    File creation ignores S_IWOTH param

    Hi,
    Any ideas why?

    The source code below creates a program which I use (for testing purposes) to copy a file "Makefile" to "Makefile2", and I get (ls -ls):
    Code:
    8 -rw-rw-r-- 1 fox fox  4845 2012-02-20 01:22 Makefile
    8 -rw-rw-r-- 1 fox fox  4845 2012-02-21 20:55 Makefile2
    So, S_IWOTH was ignored.

    File copy.c:
    PHP Code:
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>

    #define BUF_SIZE 1024

    int main(int argcchar *argv[]) {
        
    int inputFdoutputFdopenFlags;
        
    mode_t filePerms;
        
    ssize_t numRead;
        
    char buf[BUF_SIZE];
        
        if(
    argc != 3) {
            
    printf("%s old-file new-file\n"argv[0]);
            return 
    EXIT_FAILURE;
        }
        
        
    inputFd open(argv[1], O_RDONLY);
        if(
    inputFd == -1) {
            
    printf("Error opening file %s"argv[1]);
            return 
    EXIT_FAILURE;
        }
        
        
    openFlags O_CREAT O_WRONLY O_TRUNC;
        
    filePerms S_IRUSR S_IWUSR S_IRGRP S_IWGRP S_IROTH S_IWOTH;
        
        
    outputFd open(argv[2], openFlagsfilePerms);
        if(
    outputFd == -1) {
            
    printf("Error opening file %s\n"argv[2]);
            return 
    EXIT_FAILURE;
        }
        
        while( (
    numRead read(inputFdbufBUF_SIZE)) > 0) {
            if(
    write(outputFdbufnumRead) != numRead) {
                
    printf("Couldn't write whole buffer\n");
                break;
            }
        }
        
        if(
    close(inputFd) == -1) {
            
    printf("close input\n");
            return 
    EXIT_FAILURE;
        }
        
        if(
    close(outputFd) == -1) {
            
    printf("close output\n");
            return 
    EXIT_FAILURE;
        }
        
        if(
    numRead == -1) {
            return 
    EXIT_FAILURE;
        }
        
        exit(
    EXIT_SUCCESS);

    Your ads here, just 9.99$/week !!

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: File creation ignores S_IWOTH param

    For a permission bit to be set on a file at creation, it must both be specified in open() and NOT be forbidden by the umask. In your case, write-by-others is forbidden by the umask. See man umask.

    EDIT: Also, nitpicking but for buffers that contain arbitrary data, it is better to declare them as unsigned char[] rather than just char[], so that signed integer arithmetic won't stab you in the back later.
    Last edited by Bachstelze; February 21st, 2012 at 08:49 PM.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Nov 2010
    Location
    Down the rabbit hole
    Beans
    435
    Distro
    Ubuntu Development Release

    Re: File creation ignores S_IWOTH param

    Thanks a lot

    EDIT: and thanks for the unsigned char remark, though I'm not sure how exactly an arithmetic issue might arise in the case above.
    Last edited by t1497f35; February 21st, 2012 at 10:38 PM.
    Your ads here, just 9.99$/week !!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •