Usage of the Linux C language Rename-rename () does not work across different mount points, even if the same file system are mounted on both

Source: Internet
Author: User
Tags bit set symlink

The recent execution of a file on a project has been found to fail, the temporary video file is stored in the/tmp/directory of eMMC, when the recording is complete, call rename attempt to move the file to/mnt/sdcard/mmcblk1p1/(this is an external SD card), But every time rename is executed, it returns to failure.

Man 2 rename explained as follows:

1RENAME (2) Linux Programmer's Manual RENAME (2)2 3 NAME4Rename-Change the name or location of a file5 6 Synopsis7#include <stdio.h>8 9        intRenameConst Char*oldpath,Const Char*NewPath);Ten  One DESCRIPTION ARename () Renames a file, moving it between directoriesifRequired. Any and hard links to the file ( asCreatedusingLink2) is unaffected. Open -File descriptors forOldPath is also unaffected. -  theIf NewPath already exists it'll be atomically replaced (subject to a few conditions; see ERRORS below), so that there isno point at which another - process attempting to access NewPath'll find it missing. -  - If OldPath and NewPath is existing hard links referring to the same file, then rename () does nothing, and Retu RNs a success status. +  -If NewPath exists but the operation fails forSome reason rename () guarantees to leave an instance of NewPathinchPlace . +  AOldPath can specify a directory. Inch This  Case, NewPath must either not exist, or it must specify an empty directory. at  -However, when overwriting there would probably be a windowinchwhich both OldPath and NewPath refer to the file being renamed. -  -If OldPath refers to a symbolic link the link isRenamedifNewPath refers to a symbolic link the link would be a overwritten. -  - RETURN VALUE inOn success, Zero isReturned. On Error,-1  isReturned, and errno is Setappropriately. -  to ERRORS +Eacces Write Permission isDenied forThe directory containing OldPath or NewPath, or, search permission isDenied forOne of the directoriesinchThe path -Prefix of OldPath or NewPath, or OldPath isa directory and does not allow Write permission (needed to update the.  Entry). (See also Path_res‐ theOlution (7).) *  $Ebusy The rename fails because OldPath or NewPath isA directory that is inchUse by some process (perhaps asCurrent working directory, or asRoot direc‐Panax NotoginsengTory, or because it was open forReading) or is inchUse by the system ( forExample asMount point), whileThe system considers ThisAn error. -(Note that there isNo requirement toreturnEbusyinchSuch cases-there isNothing wrong with doing the rename anyway-but it isAllowed toreturn theEbusyifThe system cannot otherwise handle such situations.)


Efault OldPath or NewPath points outside your accessible address space.

EINVAL the new pathname contained a path prefix of the old, or, more generally, an attempt is made to make a directory a subdirectory of itself.

Eisdir NewPath is a existing directory, but OldPath are not a directory.

Eloop Too Many symbolic links were encountered in resolving OldPath or NewPath.

Emlink OldPath already have the maximum number of links to it, or it is a directory and the directory containing NewPath h As the maximum number of links.

Enametoolong
OldPath or NewPath was too long.

ENOENT the link named by OldPath does not exist; Or, a directory component in NewPath does not exist; Or, OldPath or NewPath is an empty string.

ENOMEM Insufficient kernel memory was available.

ENOSPC the device containing the file has no, the new directory entry.

Enotdir
A component used as a directory in OldPath or NewPath are not, in fact, a directory. Or, OldPath is a directory, and NewPath exists are not a
Directory.

Enotempty or Eexist
NewPath is a nonempty directory, which is, contains entries other than "." and "...".

Eperm or Eacces
The directory containing oldpath have the sticky bit (S_ISVTX) set and the process ' S effective user ID is neither the user ID of the file to IS
deleted nor that of the directory containing it, and the process are not privileged (linux:does not has the Cap_fowner CA pability); Or NewPath is
An existing file and the directory containing it have the sticky bit set and the process ' s effective user ID is neither the User ID of the file to
be replaced nor that of the directory containing it, and the process is not privileged (linux:does not has the Cap_fowne R capability); or the
File system containing pathname does not support renaming of the type requested.

Erofs the file is on a read-only the file system.

Exdev OldPath and NewPath is not on the same mounted file system. (Linux permits a file system to being mounted at multiple points, but rename () does not
Work across different mount points, even if the same file system are mounted on both.)


Exdev OldPath and NewPath is not on the same mounted file system. (Linux permits a file system to being mounted at multiple points, but rename () does not
Work across different mount points, even if the same file system are mounted on both.)

Conforming to
4.3BSD, C89, C99, posix.1-2001.

BUGS
On NFS file systems, you can not assume the if the operation failed the file is not renamed. If the server does the rename operation and then crashes,
The retransmitted RPC which would be processed when the server was up again causes a failure. The application is expected to deal with this. See link (2)
For a similar problem.

See ALSO
MV (1), chmod (2), Link (2), Renameat (2), Symlink (2), unlink (2), path_resolution (7), Symlink (7)

Colophon
This page was part of release 3.35 of the Linux Man-pages project. A description of the project, and information about reporting bugs, can is found at
Http://man7.org/linux/man-pages/.

Linux 2009-03-30

Watch the last errors type.

Exdev OldPath and NewPath is not on the same mounted file system. (Linux permits a file system to being mounted at multiple points, but rename () does not
Work across different mount points, even if the same file system are mounted on both.)

Recall that the previous use of rename did not fail, because it is operating on one disk, now is on two disks on different mount points. This is the reason why rename failed.

So the last one can only give up this method, using the system MV or read and write two files at the same time test found that not much difference in efficiency is relatively low.

Linux C language Rename usage-rename () does not work across different mount points, even if the same file system are mounted on Bo Th

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.