12

Someone is FTPing a file of size 10Mb to folder on a linux server. While the file is in transition a cron wakes up and fires off a Perl script that is designed to look at the ftp folder and move whatever it finds there to some alternate folder. I'm using the move() function from File::Copy. The Perl process actually renames the files as part of its task. Does that matter, or does the FTP not care what the file system describes the file as?

Will move() succeed and move a partial file, leaving the FTP to do what? Or will move fail and return 0?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • thanks for voting up my question! :) – Yevgeny Simkin Apr 30 '09 at 18:57
  • 3
    one of the greatest features of Linux as opposed to Windows IMHO is that you can do stuff to open files like moving, renaming, and even deleting and the apps that have them open will be oblivious. You can even delete programs while they're running and they will continue to run fine (but once they exit, you can never get them back...) – rmeador Apr 30 '09 at 19:04
  • What is move() and where is it moving things to? Is it possibly copying files instead of just renaming them – brian d foy Apr 30 '09 at 21:41
  • Its spelled "cron", not "chron" – Bklyn May 02 '09 at 03:52

3 Answers3

10

No, move should just let complete the download process on the new position . You are just moving the inode from one position to another. The open file descriptor from the download program should still point to it.

I just want to repeat what a few others mentioned. This only works as long as the move operation is on the same filesystem. If it as another filesystem than the inode cannot be transferred because it always belongs to the same filesystem. The most probably scenario then would be that the partial data at that moment is copied over to the new location while the program still downloads in the old inode which is not anymore attached to a file and therefor can not be used.

Norbert Hartl
  • 10,481
  • 5
  • 36
  • 46
  • 1
    Renaming those not matter because it doesn't change the inode. In the filesystem the name is associated with inodes. Moving and renaming are nearly the same regarding your question. A new file with the old filename will just contain another inode and is not problematic. Just in case this would be your next question :) – Norbert Hartl Apr 30 '09 at 18:52
  • 2
    Caveat: if you move across filesystems, the upload will finish, but you won't be able to get to the file as no reference to the inode will exist. You'll end up with a partial file on the other filesystem. – Jeff Ferland May 12 '09 at 16:43
5

Since there is no standard move, it's hard to know what's going on in your scenario. If you meant rename, then you probably won't have any problems, since the main way your situation would go wrong is if you were moving the file from one file system to another (and therefore doing a copy-and-delete, not a real move), and on most systems rename will fail under those circumstances. (So if your setup works at all, it'll be okay.)

If you're not using rename but some move function that, for example, will handle moving across filesystems, then you very well could wind up with a partial file if multiple filesystems are involved. (This can wind up being a very nasty gotcha if, for example, you're all on one filesystem now, but later on these files you're uploading take up a lot of space and you add a drive dedicated to storing them -- and now you're doing a cross-filesystem move.)

chaos
  • 122,029
  • 33
  • 303
  • 309
  • I'm using the perl move function which I think is just a wrapper around >mv -src -dest Specifically the code is move("$ftpDir$inFile","$someOtherDir$newFileName"); – Yevgeny Simkin Apr 30 '09 at 18:51
  • 2
    If it's a wrapper around the system /bin/mv, then you absolutely are in danger of partial files, *if and only if* you move across filesystems (now or in the future). – chaos Apr 30 '09 at 18:55
  • But as long as the file system is the same, you're agreeing there's no harm? – Yevgeny Simkin Apr 30 '09 at 18:59
  • Right. But keep in mind the last parenthetical sentence of my original answer. – chaos Apr 30 '09 at 19:04
  • perl has no 'move' keyword, but it does have the 'rename' keyword. If you're calling something called 'move' it may be the &move subroutine exported by the File::Copy module. If so, a look at the File::Copy documentation would be helpful. – converter42 Apr 30 '09 at 21:15
1

I am not sure, but most probably nothing wrong will happen. Moving does not change the file inode number, so that the FTP server will not notice the move at all and will continue writing into the file in the new location. To put it shortly, the move() will succeed and the upload will continue in the new location.

zoul
  • 102,279
  • 44
  • 260
  • 354