posix rename behaviour

what happens if we try to rename a folder to an existing folder containing contain some file

EX:

in a folder D1 there is another Folder D2 with f1 file and also D3 with f2 file ,if we try to rename d2-->D3

how will be the behavior is it going to delete existing d3 and try to rename or any strange behavior

Upvotes: 0

Views: 796

Answers (2)

janneb
janneb

Reputation: 37188

As answered by "parsifal", the documentation for rename() can be found e.g. at http://pubs.opengroup.org/onlinepubs/007904875/functions/rename.html

I'm adding my own answer here, because the answer by parsifal leaves out a crucial detail, namely:

If new names an existing directory, it shall be required to be an empty 
directory.

In the list of errno numbers, we have

[EEXIST] or [ENOTEMPTY]
    The link named by new is a directory that is not an empty directory

Thus if the new points to a directory which is not empty, rename() will return -1, errno will be set to EEXIST or ENOTEMPTY, and no changes have been done.

Upvotes: 2

parsifal
parsifal

Reputation: 11

The POSIX documentation is available online, and a Google search for your question takes you here: http://pubs.opengroup.org/onlinepubs/007904875/functions/rename.html

If the directory named by the new argument exists, it shall be removed and old renamed to new.

Upvotes: 1

Related Questions