Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   EXT3 Users (http://www.linux-archive.org/ext3-users/)
-   -   File system structure difference between cp and mv linux commands (http://www.linux-archive.org/ext3-users/379669-file-system-structure-difference-between-cp-mv-linux-commands.html)

Indira ramasamy 06-01-2010 07:43 AM

File system structure difference between cp and mv linux commands
 
Hi,



* I am facing one problem only with mv command not with cp command. I have a test program



#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/mount.h>

#include <fcntl.h>

#include <errno.h>



int sync_file(char *file)

{

*** FILE *fp=NULL;

*** int fd;



*** printf("file is %s
",file);

*** fp = fopen(file, "r");

*** if(!fp)

******* return -1;



*** fd = fileno(fp);

*** fflush(fp);

*** fsync(fd);

*** ioctl (fd, BLKFLSBUF, 0);

*** fclose(fp);

*** return 0;



}



int main()

{

*** int len=0;

*** FILE *fp = NULL;

*** char buf[1024];

*** char *fname = "/etc/test.conf";

*** char fname_tmp[129] = "";





*** len = sprintf(buf, "%s
", "Newly added Line is there");



*** snprintf(fname_tmp, 128, "%s.tmp", fname);



*** if( (fp = fopen(fname_tmp,"a")) == NULL )

******* printf(" ERROR: open(), error - %s
",strerror(errno));



*** fprintf(fp,"%s",buf);

*** fflush(fp);



*** fsync(fileno(fp));

*** fclose(fp);

*** system("cp -f /etc/test.conf.tmp /etc/test.conf");

** // system("mv -f /etc/test.conf.tmp /etc/test.conf");

*** sync_file(fname);

*** return 0;

}



Here i am opening a tmp file for writing. Then i am copying/moving for
original file. Then i do a fflush, fsync(), ioctl() to the original
file. Then i run this binary in linux machine(ext2 file system,
2.6.23.5 kernel) after that immediately* power off the machine. Then
power on machine, the file is disappeared or written data lost or file
gets corrupted if i move the tmp file to the original file. And there
is a no problem if i copy the tmp file to original file. So i want to
know the difference between the cp and mv command. Can you please give
me suggestion on it?




Thanks,
Indira.

_______________________________________________
Ext3-users mailing list
Ext3-users@redhat.com
https://www.redhat.com/mailman/listinfo/ext3-users

Stephen Samuel 06-01-2010 02:35 PM

File system structure difference between cp and mv linux commands
 
Reading the man page for fsync:
*Calling* fsync()* does not necessarily ensure that the entry in the directory containing the file has also reached disk.* For that an explicit fsync() on a file descriptor for the directory is also needed.



The difference between cp and mv is that mv simply adds a directory entry for the existing file in the destination directory.* If the destination is on the same filesystem (mount point) then no file data is moved.* The source directory entry is then deleted.


cp, on the other hand will create a new file, and copy the data into there.

So, with fsync on mv, there is no buffered data to be written.
With fsync on cp, there IS data to be written, and it looks like the directory entry just happens to get flushed with the file data.


It looks like there MAY be a bit of a bug with fsync not recognizing buffered data by a different name, but it should be written to recognize it by inode&device numbers, not name.

On Tue, Jun 1, 2010 at 12:43 AM, Indira ramasamy <vel.indira@gmail.com> wrote:

Hi,



* I am facing one problem only with mv command not with cp command. I have a test program



#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/mount.h>

#include <fcntl.h>

#include <errno.h>



int sync_file(char *file)

{

*** FILE *fp=NULL;

*** int fd;



*** printf("file is %s
",file);

*** fp = fopen(file, "r");

*** if(!fp)

******* return -1;



*** fd = fileno(fp);

*** fflush(fp);

*** fsync(fd);

*** ioctl (fd, BLKFLSBUF, 0);

*** fclose(fp);

*** return 0;



}



int main()

{

*** int len=0;

*** FILE *fp = NULL;

*** char buf[1024];

*** char *fname = "/etc/test.conf";

*** char fname_tmp[129] = "";





*** len = sprintf(buf, "%s
", "Newly added Line is there");



*** snprintf(fname_tmp, 128, "%s.tmp", fname);



*** if( (fp = fopen(fname_tmp,"a")) == NULL )

******* printf(" ERROR: open(), error - %s
",strerror(errno));



*** fprintf(fp,"%s",buf);

*** fflush(fp);



*** fsync(fileno(fp));

*** fclose(fp);

*** system("cp -f /etc/test.conf.tmp /etc/test.conf");

** // system("mv -f /etc/test.conf.tmp /etc/test.conf");

*** sync_file(fname);

*** return 0;

}



Here i am opening a tmp file for writing. Then i am copying/moving for
original file. Then i do a fflush, fsync(), ioctl() to the original
file. Then i run this binary in linux machine(ext2 file system,
2.6.23.5 kernel) after that immediately* power off the machine. Then
power on machine, the file is disappeared or written data lost or file
gets corrupted if i move the tmp file to the original file. And there
is a no problem if i copy the tmp file to original file. So i want to
know the difference between the cp and mv command. Can you please give
me suggestion on it?




Thanks,
Indira.


_______________________________________________

Ext3-users mailing list

Ext3-users@redhat.com

https://www.redhat.com/mailman/listinfo/ext3-users


--
Stephen Samuel http://www.bcgreen.com *Software, like love,

778-861-7641 * * * * * * * * * * * * * * *grows when you give it away

_______________________________________________
Ext3-users mailing list
Ext3-users@redhat.com
https://www.redhat.com/mailman/listinfo/ext3-users

"Lakshmipathi.G" 06-02-2010 04:57 AM

File system structure difference between cp and mv linux commands
 
If you don't have any* issue- in using system calls directly ,then instead of*
system("mv source target").
use -
link ("source","target") // creat hard link
unlink ("source")******** // remove old link


[check "man 2 link" and "man 2 unlink"* for more details]

HTH




On Tue, Jun 1, 2010 at 8:05 PM, Stephen Samuel <samuel@bcgreen.com> wrote:

Reading the man page for fsync:
*Calling* fsync()* does not necessarily ensure that the entry in the directory containing the file has also reached disk.* For that an explicit fsync() on a file descriptor for the directory is also needed.




The difference between cp and mv is that mv simply adds a directory entry for the existing file in the destination directory.* If the destination is on the same filesystem (mount point) then no file data is moved.* The source directory entry is then deleted.



cp, on the other hand will create a new file, and copy the data into there.

So, with fsync on mv, there is no buffered data to be written.
With fsync on cp, there IS data to be written, and it looks like the directory entry just happens to get flushed with the file data.



It looks like there MAY be a bit of a bug with fsync not recognizing buffered data by a different name, but it should be written to recognize it by inode&device numbers, not name.


On Tue, Jun 1, 2010 at 12:43 AM, Indira ramasamy <vel.indira@gmail.com> wrote:

Hi,



* I am facing one problem only with mv command not with cp command. I have a test program



#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/mount.h>

#include <fcntl.h>

#include <errno.h>



int sync_file(char *file)

{

*** FILE *fp=NULL;

*** int fd;



*** printf("file is %s
",file);

*** fp = fopen(file, "r");

*** if(!fp)

******* return -1;



*** fd = fileno(fp);

*** fflush(fp);

*** fsync(fd);

*** ioctl (fd, BLKFLSBUF, 0);

*** fclose(fp);

*** return 0;



}



int main()

{

*** int len=0;

*** FILE *fp = NULL;

*** char buf[1024];

*** char *fname = "/etc/test.conf";

*** char fname_tmp[129] = "";





*** len = sprintf(buf, "%s
", "Newly added Line is there");



*** snprintf(fname_tmp, 128, "%s.tmp", fname);



*** if( (fp = fopen(fname_tmp,"a")) == NULL )

******* printf(" ERROR: open(), error - %s
",strerror(errno));



*** fprintf(fp,"%s",buf);

*** fflush(fp);



*** fsync(fileno(fp));

*** fclose(fp);

*** system("cp -f /etc/test.conf.tmp /etc/test.conf");

** // system("mv -f /etc/test.conf.tmp /etc/test.conf");

*** sync_file(fname);

*** return 0;

}



Here i am opening a tmp file for writing. Then i am copying/moving for
original file. Then i do a fflush, fsync(), ioctl() to the original
file. Then i run this binary in linux machine(ext2 file system,
2.6.23.5 kernel) after that immediately* power off the machine. Then
power on machine, the file is disappeared or written data lost or file
gets corrupted if i move the tmp file to the original file. And there
is a no problem if i copy the tmp file to original file. So i want to
know the difference between the cp and mv command. Can you please give
me suggestion on it?




Thanks,
Indira.


_______________________________________________

Ext3-users mailing list

Ext3-users@redhat.com

https://www.redhat.com/mailman/listinfo/ext3-users


--
Stephen Samuel http://www.bcgreen.com *Software, like love,


778-861-7641 * * * * * * * * * * * * * * *grows when you give it away


_______________________________________________

Ext3-users mailing list

Ext3-users@redhat.com

https://www.redhat.com/mailman/listinfo/ext3-users


--
----
Cheers,
Lakshmipathi.G

FOSS Programmer.
www.giis.co.in

_______________________________________________
Ext3-users mailing list
Ext3-users@redhat.com
https://www.redhat.com/mailman/listinfo/ext3-users

Stephen Samuel 06-03-2010 02:51 PM

File system structure difference between cp and mv linux commands
 
If you do that, then you should probably be ready to use a 'cp', if the source and destination
aren't on the same device (e.g. just in case /tmp becomes a separate filesystem)


On Tue, Jun 1, 2010 at 9:57 PM, Lakshmipathi.G <lakshmipathi.g@gmail.com> wrote:

If you don't have any* issue- in using system calls directly ,then instead of*
system("mv source target").
use -
link ("source","target") // creat hard link
unlink ("source")******** // remove old link



[check "man 2 link" and "man 2 unlink"* for more details]
--
Stephen Samuel http://www.bcgreen.com *Software, like love,

778-861-7641 * * * * * * * * * * * * * * *grows when you give it away

_______________________________________________
Ext3-users mailing list
Ext3-users@redhat.com
https://www.redhat.com/mailman/listinfo/ext3-users


All times are GMT. The time now is 02:13 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.