Quote:
Originally Posted by
hicksd8
As Don says, it's not possible to determine what this code is actually doing. However, it occurs to me that, it could be simply creating a link (and leaving the original file in-situ). If you then stat the link (inode) location you will likely get a zero length. However, when you list that link it will show the original file size.
However, please answer Don's questions and post what OS you are using.
Hi hicksd8,
Assuming that there is no power failure, disk failure, disk controller failure, CPU failure, memory failure, etc. between the time of the creation of the new link and the removal of the old link; the old link will be removed if a new link was created as a result of a
rename() request. If the original file size was greater than zero, you should never end up a with a zero size original file and a non-zero new file size as a result of just a
rename() request.
My guess would be that one of the following happened:
- /dir1/file1.bin and /dir2/newfile1.bin reside on different filesystems, the rename() is failing with an EXDEV error, the stat() is failing because the new link was not created, and the zero size is coming from uninitialized data on the stack in the struct stat;
- there is an existing /dir2/newfile1.bin that has size zero that the user invoking this program doesn't have permission to remove (getting an EPERM error from the rename()) and the code falls through the rename() failure and gets and prints the size of the unmodified destination file; or
- everything went as expected creating a new link, the stat() succeeded, and the printf("New file size is %d", dest_info.st_size); successfully printed the high order bits of an object of type size_t (which is a 64-bit type on most current systems) using %d (which prints a 32-bit integer on most current systems).
But, lots of other problems are also possible with missing
#include directives, unknown compiler diagnostics and warnings while the code was being built, etc. and we have absolutely no way to determine what happened with the information we have been given to analyze.