Thread: setting numerical file permissions with open

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    setting numerical file permissions with open

    Hi there,

    i'm writing to a log file using open:
    Code:
    log_file = open(log, O_RDWR | O_CREAT | O_APPEND, 0644);
    as i understand it 0644 means owner can read and write, group and all can read only.

    why then, when the file is created, do it's permissions read as:
    Code:
    ---x-w-r-x    1 user user      120 Nov   8 10:17 logfile.log
    any ideas?

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    well, i think the mode parameter is only used when O_CREAT is specified.

    i.e. if the file does not exist, create it with the specified mode or use the default (the mode para is optional). The mode parameter is only used in this circumstance.

    if i do

    Code:
    chmod 0644 logfile.log
    the permissions are set correctly.

    you're right about opening with read/write - i'll change that.

    i presume unix numerical permissions are the same in linux!

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    solved the problem by using defined variables rather than numerical values:
    Code:
    log_file = open(log, O_CREAT | O_WRONLY | O_SYNC | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP);
    this creates a file with 0640 permissions.

    it important not to have a previous conflicting umask in the code - i'm not sure why!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM