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.

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:

The system you're using seems to be meeting the requirements of the standards on this issue.

1 Like

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?

Yes.

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() ).

I will need to find a different approach to this.
Thanks for your help. I appreciate it.

What are you trying to do? You're setting the time for some reason.