views:

617

answers:

3

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?

+10  A: 

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
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
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.
Autocracy
+1  A: 

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
+5  A: 

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
I'm using the perl move function which I think is just a wrapper around >mv -src -destSpecifically the code is move("$ftpDir$inFile","$someOtherDir$newFileName");
Dr.Dredel
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
But as long as the file system is the same, you're agreeing there's no harm?
Dr.Dredel
Right. But keep in mind the last parenthetical sentence of my original answer.
chaos
converter42