Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-02

Problems with moving/renaming file using sysutils.renamefile

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

Free Pascal Compiler version 2.6.0-1 [2012/01/13] for i386 Linux x86

Trying to move a file (and rename it at the same time):

//ExtractedCompiler is a string variable
//BootstrapCompiler is a string property
//I'm using my own version of debugln, not important here
debugln('Going to rename/move ' + ExtractedCompiler + ' to ' +
BootstrapCompiler);
sysutils.DeleteFile(BootstrapCompiler); //ignore errors
OperationSucceeded:=renamefile(ExtractedCompiler, BootstrapCompiler);

The program prints out this:
Debug: Going to rename/move /tmp/TMP00000.tmp.out to /home/pascaldev/fpcbootstrap/i386-linux-ppc386-1
... but OperationSucceeded is set to false; file move failed.

Original file exists:
ls /tmp/TMP00000.tmp.out
/tmp/TMP00000.tmp.out

Target directory exists:
ls /home/pascaldev/fpcbootstrap
#directory exists

And I can rename it using the shell:
mv /tmp/TMP00000.tmp.out
/home/pascaldev/fpcbootstrap/i386-linux-ppc386-1
#yep, works:
ls /home/pascaldev/fpcbootstrap
i386-linux-ppc386-1

What am I doing wrong here?

Thanks,
Reinier

Reply by email

Jonas Maebe
to Reinier Olislagers
Reinier Olislagers wrote 9 lines
Trying to move a file (and rename it at the same time): //ExtractedCompiler is a string variable //BootstrapCompiler is a string property //I'm using my own version of debugln, not important here debugln('Going to rename/move ' + ExtractedCompiler + ' to ' + BootstrapCompiler); sysutils.DeleteFile(BootstrapCompiler); //ignore errors OperationSucceeded:=renamefile(ExtractedCompiler, BootstrapCompiler);

Renaming is not the same as moving. Renaming only works if source and destination are on the same volume.

Jonas

Reply by email

Reinier Olislagers
to Jonas Maebe
Jonas Maebe wrote 15 lines
On 10 Feb 2012, at 12:19, Reinier Olislagers wrote: > Trying to move a file (and rename it at the same time): > > //ExtractedCompiler is a string variable > //BootstrapCompiler is a string property > //I'm using my own version of debugln, not important here > debugln('Going to rename/move ' + ExtractedCompiler + ' to ' + > BootstrapCompiler); > sysutils.DeleteFile(BootstrapCompiler); //ignore errors > OperationSucceeded:=renamefile(ExtractedCompiler, BootstrapCompiler); Renaming is not the same as moving. Renaming only works if source and destination are on the same volume.

Thanks, Jonas.

1. Renaming is not moving???
The help says:
"RenameFile renames a file from OldName to NewName. The function returns True if successful, False otherwise. Remark: you cannot rename across disks or partitions."
What does this mean, then?
If you rename from e.g. /etc/b to /var/b, (and assuming they're on the same partition), you're moving the files from one directory to another, aren't you?
If you cannot move files between directories at all but only rename them, the remark in the help does not make sense.
Maybe it helps if something like this is put there: "RenameFile renames a file from OldName to NewName. The function returns True if successful, False otherwise. Remark: this function can move and/or rename files from one directory to another, but you cannot rename across disks or partitions."

2. What is the function for "properly" moving files in FPC that also moves between partitions, if one exists?
Or a file copy routine (I've found only Lazarus fileutil.Copyfile)? And could a link to that be put in the RenameFile help?

3. You were right that I was moving between partitions; apparently ubuntu has a separate partition for /tmp... I hadn't realized that.

Thanks a lot for helping a poor non-Delphi programmer ;)

Reinier

Reply by email

Marco van de Voort
to Reinier Olislagers

In our previous episode, Reinier Olislagers said:

Show quoted text 6 lines
1. Renaming is not moving??? The help says: "RenameFile renames a file from OldName to NewName. The function returns True if successful, False otherwise. Remark: you cannot rename across disks or partitions." What does this mean, then?

Only the entry in the directory is changed on rename. Not the data. If you rename across partitions or disks, the data must also be moved.

Reply by email

Jonas Maebe
to Reinier Olislagers: Thanks, Jonas. 1. Renaming is not moving??? The help says: "RenameFile renames a file…
Reinier Olislagers wrote 11 lines
1. Renaming is not moving??? The help says: "RenameFile renames a file from OldName to NewName. The function returns True if successful, False otherwise. Remark: you cannot rename across disks or partitions." What does this mean, then? If you rename from e.g. /etc/b to /var/b, (and assuming they're on the same partition), you're moving the files from one directory to another, aren't you?

At the level of the filesystem, that is also a rename operation.

2. What is the function for "properly" moving files in FPC that also moves between partitions, if one exists?

I don't think there is one.

Or a file copy routine (I've found only Lazarus fileutil.Copyfile)? And could a link to that be put in the RenameFile help?

You indeed have to copy the file and then delete the original one in that case. I don't know whether there is a copyfile routine in the RTL.

Jonas

Reply by email

Unknown author
to Reinier Olislagers: Thanks, Jonas. 1. Renaming is not moving??? The help says: "RenameFile renames a file…
Reinier Olislagers wrote 25 lines
On 10-2-2012 13:09, Jonas Maebe wrote: > > On 10 Feb 2012, at 12:19, Reinier Olislagers wrote: > >> Trying to move a file (and rename it at the same time): >> >> //ExtractedCompiler is a string variable >> //BootstrapCompiler is a string property >> //I'm using my own version of debugln, not important here >> debugln('Going to rename/move ' + ExtractedCompiler + ' to ' + >> BootstrapCompiler); >> sysutils.DeleteFile(BootstrapCompiler); //ignore errors >> OperationSucceeded:=renamefile(ExtractedCompiler, BootstrapCompiler); > > Renaming is not the same as moving. Renaming only works if source and > destination are on the same volume. Thanks, Jonas. 1. Renaming is not moving??? The help says: "RenameFile renames a file from OldName to NewName. The function returns True if successful, False otherwise. Remark: you cannot rename across disks or partitions."

That is exactly what Jonas said.

What does this mean, then? If you rename from e.g. /etc/b to /var/b, (and assuming they're on the same partition), you're moving the files from one directory to another, aren't you?

No, you are renaming them.

But not necessarily. On Linux, you see all directories in the same tree. This does not necessarily mean that they are on the same partition or disk. This depends on the mount configuration.

If you cannot move files between directories at all but only rename them, the remark in the help does not make sense.

You can move them, if the directories are on the same partition.

Show quoted text 5 lines
Maybe it helps if something like this is put there: "RenameFile renames a file from OldName to NewName. The function returns True if successful, False otherwise. Remark: this function can move and/or rename files from one directory to another, but you cannot rename across disks or partitions."

It seems to me you are confusing 'moving' and 'renaming'.

The function just renames. The result may be that the file ends up in another directory, i.e. is moved.

2. What is the function for "properly" moving files in FPC that also moves between partitions, if one exists?

None exists.

Michael.

Reply by email

Reinier Olislagers
to Marco van de Voort: In our previous episode, Reinier Olislagers said: Only the entry in the directory is…
Marco van de Voort wrote 11 lines
In our previous episode, Reinier Olislagers said: > 1. Renaming is not moving??? > The help says: > "RenameFile renames a file from OldName to NewName. The function returns > True if successful, False otherwise. Remark: you cannot rename across > disks or partitions." > What does this mean, then? Only the entry in the directory is changed on rename. Not the data. If you rename across partitions or disks, the data must also be moved.

I understand that must be the reason why you can't rename across partitions but I'd suggest spelling things out a bit more in the help... Then again, my preferences might not be the help writers' ;)

Thanks,
Reinier

Reply by email

Reinier Olislagers
to Unknown author: That is exactly what Jonas said. No, you are renaming them. But not necessarily. On…
Show quoted text 14 lines
On Fri, 10 Feb 2012, Reinier Olislagers wrote: > On 10-2-2012 13:09, Jonas Maebe wrote: > 1. Renaming is not moving??? > The help says: > "RenameFile renames a file from OldName to NewName. The function returns > True if successful, False otherwise. Remark: you cannot rename across > disks or partitions." That is exactly what Jonas said. > What does this mean, then? > If you rename from e.g. /etc/b to /var/b, (and assuming they're on the > same partition), you're moving the files from one directory to another, > aren't you? No, you are renaming them.

I understand that you might be renaming them from a fs/OS standpoint as there is no actual file data being transferred... but to the user it has moved.

> If you cannot move files between directories at all but only rename > them, the remark in the help does not make sense. You can move them, if the directories are on the same partition.

Exactly. To the user/programmer it looks as if they've moved (the fs really moved the pointer to the file data into another part of the tree).

Show quoted text 6 lines
> Maybe it helps if something like this is put there: > "RenameFile renames a file from OldName to NewName. The function returns > True if successful, False otherwise. Remark: this function can move > and/or rename files from one directory to another, but you cannot rename > across disks or partitions." It seems to me you are confusing 'moving' and 'renaming'.

I'd welcome a better suggestion for adding to the help file...

The function just renames. The result may be that the file ends up in another directory, i.e. is moved.

Mmm yes. To me that is arguing over semantics. Or maybe we're trying to say the same thing.

I understand that's the way the underlying filesystem works (on Unixes at least), but that is not the case everywhere.
E.g. Windows rename command help gives:
Renames a file or files.[...]Note that you cannot specify a new drive or path for your destination file.

So there, rename (the command at least) does NOT allow you to move files between directories.

In other words, programmers coming from a Windows background might not expect that the renamefile command allows "moving" files between directories on the same partition.

I do understand what Jonas & you meant (and I even understood the point about not being able to move*cough* rename files across directories from the help before writing to the list - just erroneously assumed /tmp and /home were on the same partition).

My point is that it would help to expand the help a bit so that it's clearer.

Then again, my Delphi-less background may lead me to strange errors such as these which people with a Delphi history may dismiss ;)

> 2. What is the function for "properly" moving files in FPC that also > moves between partitions, if one exists? None exists.

Thanks, I feared as much.

Thanks for the help, you 2,

Regards,
Reinier

Reply by email

Sven Barth
to Reinier Olislagers: Thanks, Jonas. 1. Renaming is not moving??? The help says: "RenameFile renames a file…
Reinier Olislagers wrote
2. What is the function for "properly" moving files in FPC that also moves between partitions, if one exists?

What I'd use in those cases:

var
   dest, source: TFileStream;
begin
   dest := Nil;
   source := TFileStream.Create(aSourceFileName, fmOpenRead);
   try
     dest := TFileStream.Create(aDestFileName, fmCreate or fmOpenWrite);
     dest.CopyFrom(source, source.Size);
   finally
     dest.Free;
     source.Free;
   end;
end;

Regards,
Sven

Reply by email

Reinier Olislagers
to Reinier Olislagers: I understand that you might be renaming them from a fs/OS standpoint as there is no…
Reinier Olislagers wrote 18 lines
On 10-2-2012 13:42, wrote: > The function just renames. The result may be that the file ends up in > another directory, i.e. is moved. Mmm yes. To me that is arguing over semantics. Or maybe we're trying to say the same thing. I understand that's the way the underlying filesystem works (on Unixes at least), but that is not the case everywhere. E.g. Windows rename command help gives: Renames a file or files.[...]Note that you cannot specify a new drive or path for your destination file. So there, rename (the command at least) does NOT allow you to move files between directories. In other words, programmers coming from a Windows background might not expect that the renamefile command allows "moving" files between directories on the same partition.

I've written this wiki page:
http://wiki.lazarus.freepascal.org/Rosetta_Stone

... hope that conveys my meaning a bit better, and may help other people who have the same preconceptions...

Of course, additions/corrections welcome, as always...

Regards,
Reinier

Reply by email