File date/time modification and permissions


 
Thread Tools Search this Thread
Top Forums Programming File date/time modification and permissions
# 1  
Old 03-25-2015
Wrench File date/time modification and permissions

First, oh great Unix gurus, forgive if this is a stupid question.

Unix/Linux is not my main thing but I have been programming in C/C++ for many years. I will do my best to be specific.

I have a program in C/C++ that needs to modify the time of a given file. Currently I do this using utime() passing it a filespec and an FTIME structure (I get the time of other files using ustat elsewhere).

This code works fine when there are no permission issues (hence the title), so I think it is safe to say I am using these functions correctly.

However, when the file to be modified was created by root and the user running my program is (in some way) not as privileged in the permissions area, then utime() call fails with errno = 1.

What I don't understand is why it fails if the file is set for RW for User, Group and World. Even though user X running the program is not root, I thought they can still write to it because root has it open to the world for writing. What am I missing here?

If the file is owned by user X it works or if root is the user it works, but if they don't match it does not. I would expect it to work for all users if the file is writable for everyone.

Should I be using a different method? I did think is was odd that I could call utime without opening the file to get a file handle first.

Someone said it is because the containing directory is hwere the write permissions have to be, but I tried making the folder RW for world too.

Please enlighten me to the underlying issue here. I want to understand this.

Thanks.
# 2  
Old 03-25-2015
According to the standards, the utime(path, times) function is required to return -1 with errno set to EPERM (which is 1 on most UNIX Systems) when:
Quote:
The times argument is not a null pointer and the effective user ID of the calling
process does not match the owner of the file and the calling process does not
have appropriate privileges.
The system you're using seems to be meeting the requirements of the standards on this issue.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-25-2015
I see. So you are saying that the function is supposed to work that way.
Only the owner of the file or a superuser can change it.
So in my case it can't be done.

Is there a setting I can apply to the file to allow all users to access it?
Like a group setting. Or is it hopeless?
# 4  
Old 03-25-2015
Quote:
Originally Posted by Pug
I see. So you are saying that the function is supposed to work that way.
Only the owner of the file or a superuser can change it.
So in my case it can't be done.
Yes.

Quote:
Is there a setting I can apply to the file to allow all users to access it?
Like a group setting. Or is it hopeless?
Access (read, write, execute) is not the same as change file characteristics (access permissions, owner ID, group ID, and timestamps).

If a file has access permissions 666 (read and write by owner, group, and world), anybody can set the modification and status change timestamps of the file by writing to it and anybody can set the access timestamp of the file by reading it (but you can only set those timestamps to the current time; not some arbitrary time in the past, the present, or the future as you can with utime()).
# 5  
Old 03-26-2015
I will need to find a different approach to this.
Thanks for your help. I appreciate it.
# 6  
Old 03-27-2015
What are you trying to do? You're setting the time for some reason.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subtract a file's modification date with current date

SunOS -s 5.10 Generic_147440-04 sun4u sparc SUNW,SPARC-Enterprise Hi, In a folder, there are files. I have a script which reads the current date and subtract the modification date of each file. How do I achieve this? Regards, Joe (2 Replies)
Discussion started by: roshanbi
2 Replies

2. Shell Programming and Scripting

How to change modification time of file?

Explain it with proper e.g (4 Replies)
Discussion started by: sidpatil
4 Replies

3. UNIX for Dummies Questions & Answers

How do you get the last modification date of a file?

I'm trying to get the date output to be in the form yyyy-mm-dd (e.g. 2013-01-18) !/bin/sh modDate=$(stat -c %y $1) echo $modDate >> $1 When I run this on another file (by typing ./dateScript theFile.txt), I keep getting this message: stat: illegal option -- c What's wrong with my code... (2 Replies)
Discussion started by: Nate18
2 Replies

4. UNIX for Dummies Questions & Answers

Need Modification Time of a file

Hi all, I need the modification time of a file on a particular day say 3 days before. I just don't want the last modification time. I need all the modification times on a particualar day. Is there anyway to do it? Kindly help. Could anyone tell me where the modification time is stored?... (1 Reply)
Discussion started by: vidhyab
1 Replies

5. Shell Programming and Scripting

File modification time comparison

Hi All, I have two files (given below) each exists under different paths. I want to compare the modification time stamp of file1.txt is lessthan the modification time of file2.txt. month1=`ls -l file1.txt | awk '{ print $6}'` date1=`ls -file1.txt | awk '{ print $7}'` time1=`ls... (1 Reply)
Discussion started by: Arunprasad
1 Replies

6. Shell Programming and Scripting

ls -e to find out File modification time in secs

Hi All, I would like to know the file modification time till seconds in Unix. So I tried ls -e and it worked fine. This Solaris 5.10 -rw-rw-r-- 1 test admin 22 Sep 12 11:01:37 2008 test_message But I am not able to run the same command in SOlaris 5.6 and also in AIX/HP Is there... (3 Replies)
Discussion started by: rahulkav
3 Replies

7. Shell Programming and Scripting

Displaying the Last Modification Time of a specific file

How can I get and display the last modification time of a file? in scripting or specifically using Batch file I want this info for me to determine whether an image has been edited or not by using the last modification time and compare it to our stored date of modification. can somebody help... (5 Replies)
Discussion started by: jaque18
5 Replies

8. UNIX for Dummies Questions & Answers

How to change the file modification time of a file on nfs mount point

Hi I am accessing a file on nfs mounted device, after completing using of the file, i am tring to restore the access time and modification times of the file. So i got the previous modified time of the file using stat() function and trying to set the date and time for the file, To set these... (6 Replies)
Discussion started by: deepthi.s
6 Replies

9. UNIX for Dummies Questions & Answers

command for modification date of a file

Good morning, I would like to find all files of a certain type and display their name as well as their modification date. In order to do this, I would do the following: find ./ -name *.csv | ???????? My question: what to put after the pipe instead of the question marks? Is there a basic... (5 Replies)
Discussion started by: scampsd
5 Replies

10. UNIX for Dummies Questions & Answers

File modification time

Does anyone know how to display the time with seconds of when a file was last modified. I can get hour & minutes but would also like seconds. --Running AIX (1 Reply)
Discussion started by: edog
1 Replies
Login or Register to Ask a Question