* Rename file over another with the same inode number fails silently.
@ 2007-02-08 16:48 John Muir
2007-02-08 17:14 ` Matthew Wilcox
0 siblings, 1 reply; 2+ messages in thread
From: John Muir @ 2007-02-08 16:48 UTC (permalink / raw)
To: linux-fsdevel
[-- Attachment #1: Type: text/plain, Size: 796 bytes --]
The attached test program creates a file, and then some hard links to
that file (file0 - fileN).
The test program then attempts to rename(fileN, file) for every hard
link created.
My expectation is that the hard links file0 - fileN would simply
disappear, or that rename would respond with an error result and an
appropriate errno value indicating the problem.
My observation is that the hard links file0 to fileN do not in fact
disappear and rename returns 0.
Do I have the wrong expectations? If so, why should I have to stat the
files to determine if they are the same inode before I rename?
Is this a VFS bug? It seems to apply to all file-systems (I have tried
only EXT3, XFS, and TMPFS). This also seems to occur on other unixes.
Regards,
John
--
John Muir
NORTEL
muirj@nortel.com
[-- Attachment #2: renameover.c --]
[-- Type: text/x-csrc, Size: 2327 bytes --]
// renameover
// ------------------------------------------------------------------
// Simple program which renames hard links over each other.
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define FILE_BLOCK_SIZE 4096
// main
// ------------------------------------------------------------------
int
main (
int argc,
char **argv )
{
int fd;
int count = 0;
int i;
char *buf;
struct stat statBuf;
if( argc < 2 )
{
fprintf( stderr, "renameover <filename> [count]\n" );
return EINVAL;
}
if( argc >= 3 )
count = atoi( argv[2] );
if( count <= 0 )
count = 10;
fd = open( argv[1], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
if( fd < 0 )
{
perror( "open create" );
return errno;
}
buf = alloca( FILE_BLOCK_SIZE );
memset( buf, ( count % 26 ) + 'A', FILE_BLOCK_SIZE );
buf[ FILE_BLOCK_SIZE - 1 ] = '\n';
write( fd, buf, FILE_BLOCK_SIZE );
if( close( fd ) < 0 )
{
perror( "close" );
return errno;
}
i = 0;
while( i < count )
{
snprintf( buf, FILE_BLOCK_SIZE, "%s%d", argv[1], i );
if( link( argv[1], buf ) < 0 )
{
perror( "link" );
return errno;
}
++i;
}
if( stat( argv[1], &statBuf ) < 0 )
{
perror( "stat" );
return errno;
}
printf( "Hard link count for '%s': %u (expecting %d).\n",
argv[1], statBuf.st_nlink, count + 1 );
i = 0;
while( i < count )
{
snprintf( buf, FILE_BLOCK_SIZE, "%s%d", argv[1], i );
if( rename( buf, argv[1] ) < 0 )
{
perror( "rename" );
return errno;
}
else
{
if( stat( buf, &statBuf ) < 0 )
{
if( errno != ENOENT )
perror( "stat" );
}
else
{
printf( "Unexpected: '%s' still exists. "
"Hard link count: %u, expected %d.\n",
buf, statBuf.st_nlink, count - i );
}
}
++i;
}
return 0;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Rename file over another with the same inode number fails silently.
2007-02-08 16:48 Rename file over another with the same inode number fails silently John Muir
@ 2007-02-08 17:14 ` Matthew Wilcox
0 siblings, 0 replies; 2+ messages in thread
From: Matthew Wilcox @ 2007-02-08 17:14 UTC (permalink / raw)
To: John Muir; +Cc: linux-fsdevel
On Thu, Feb 08, 2007 at 11:48:16AM -0500, John Muir wrote:
> The attached test program creates a file, and then some hard links to
> that file (file0 - fileN).
> The test program then attempts to rename(fileN, file) for every hard
> link created.
>
> My expectation is that the hard links file0 - fileN would simply
> disappear, or that rename would respond with an error result and an
> appropriate errno value indicating the problem.
>
> My observation is that the hard links file0 to fileN do not in fact
> disappear and rename returns 0.
>
> Do I have the wrong expectations? If so, why should I have to stat the
> files to determine if they are the same inode before I rename?
This is a POSIX requirement. See
http://www.opengroup.org/onlinepubs/009695399/functions/rename.html
where it says,
"If the old argument and the new argument resolve to the same existing
file, rename() shall return successfully and perform no other action."
There's not really much point in discussing whether your expectations
are wrong or whether the extra work you have to do is silly ... POSIX
says so, so we have to behave this way.
Personally, I think this is an unexpected oversight on POSIX's part.
The rationale refers to 'rename("x", "x");', and doesn't discuss hard
links. Possibly something to raise with the POSIX ctte, but I doubt
they'd be interested in changing this now ... perhaps if you can find
some other unices which behave differently, you might have a shot.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-02-08 17:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-02-08 16:48 Rename file over another with the same inode number fails silently John Muir
2007-02-08 17:14 ` Matthew Wilcox
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).