Why do I get the error 'copy: preserving times for XXXX:Operation not permitted' when using COPYFILE command in MATLAB 7.8 (R2009a)?

93 views (last 30 days)
I want to be able to overwrite a certain file (‘file2’) with the contents of the new file (‘file1’) while preserving the original file if the copy operation fails. File 'file1' is owned by me and has 'rw rw rw' permissions. File 'file2' is owned by another user with 'rw rw rw' permissions.
I use the COPYFILE command in MATLAB to copy the contents of 'file1' file into the 'file2' file:
[a,b,c]=copyfile('file1,'file2')
I get an error:
a =
0
b =
copy: preserving times for '/usr/otheruser/file2':Operation not permitted
b =
MATLAB:COPYFILE:OSError
The COPYFILE command returned an error, however, I observe that the contents of the 'file2' file has changed successfully.
I get the same result when I use the COPYFILE command with the 'f' option:
[a,b,c]=copyfile('file','file2','f')
When I use the CP command in Linux with the '-p' option on the same machine I get the same result - the contents of the file are copied but the CP command returns an error.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Jan 2010
This change has been incorporated into the documentation in Release 2009b (R2009b). For previous releases, read below for any additional information:
The MATLAB command COPYFILE relies on the Operating System (OS) to perform the COPY operation. On Linux/Unix platforms, COPYFILE uses the 'cp -p' command. The COPYFILE command in MATLAB was designed to preserve timestamps consistently on all platforms. Windows preserves timestamps by default, but Unix does not, unless the CP command with the '-p' option is used. On Unix the timestamps cannot be preserved on the files the user does not own, which is why the following command
cp -p file1 file2
will fail if file1 and file 2 are owned by different users. The force flag 'f' in COPYFILE results in executing 'cp -p -f' on Unix, which also fails to preserve the timestamp of the file you do not own.
As a workaround, you can use the COPYFILE function to copy your file 'file1' into a new file 'fileInTheMiddle' (in the directory owned by a different user with 'rw rw rw' permissions) and then rename 'fileInTheMiddle' into 'file2' using the MOVEFILE function in MATLAB if the first copy operation succeeds. Note that if the copy operation fails, you might end up with a corrupted 'file2' which will no longer holds the original values. This might happen while using the CP command on Linux, or using the COPYFILE command in MATLAB (which uses the CP command).
To avoid this issue and to track the success of the copy operation, do the following:
1. Copy the file with the new values to the intermediate file:
[a,b,c]=copyfile('file1','/usr/otheruser/fileInTheMiddle ')
2. If the copy is successful, rename the intermediate file to file2
[a,b,c]=movefile(' /usr/otheruser/fileInTheMiddle ','/usr/otheruser/file2')
For more information on the MOVEFILE command in MATLAB, please refer to the documentation by executing the following in the MATLAB command prompt:
doc movefile

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Products


Release

R2009a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!