Rename to and from to old and new in order to be consistant with Linux.

This commit is contained in:
John Scipione
2012-03-05 20:00:53 -05:00
parent c8dd9f3e8c
commit 95dd0de5ea
2 changed files with 55 additions and 55 deletions
+54 -54
View File
@@ -519,110 +519,110 @@ mknodat(int fd, const char *path, mode_t mode, dev_t dev)
int int
renameat(int fromFD, const char* from, int toFD, const char* to) renameat(int oldFD, const char* oldPath, int newFD, const char* newPath)
{ {
bool fromIsAbsolute = false; bool oldPathIsAbsolute = false;
bool toIsAbsolute = false; bool newPathIsAbsolute = false;
if (from && from[0] == '/') if (oldPath && oldPath[0] == '/')
fromIsAbsolute = true; oldPathIsAbsolute = true;
if (to && to[0] == '/') if (newPath && newPath[0] == '/')
toIsAbsolute = true; newPathIsAbsolute = true;
if (fromIsAbsolute && toIsAbsolute) { if (oldPathIsAbsolute && newPathIsAbsolute) {
// from and to are absolute, call rename() ignoring the fd's // oldPath and newPath are absolute, call rename() ignoring the fd's
return rename(from, to); return rename(oldPath, newPath);
} }
if ((!fromIsAbsolute && !fromFD) || (!toIsAbsolute && !toFD)) { if ((!oldPathIsAbsolute && !oldFD) || (!newPathIsAbsolute && !newFD)) {
// from is not an absolute path and fromFD is not a valid file // oldPath is not an absolute path and oldFD is not a valid file
// descriptor or to is not an absolute path and toFD is not a // descriptor or newPath is not an absolute path and newFD is not a
// valid file descriptor // valid file descriptor
errno = EBADF; errno = EBADF;
return -1; return -1;
} }
char *fromDirPath; char *oldDirPath;
char *toDirPath; char *newDirPath;
if (fromIsAbsolute || fromFD == AT_FDCWD) if (oldPathIsAbsolute || oldFD == AT_FDCWD)
fromDirPath = const_cast<char *>(from); oldDirPath = const_cast<char *>(oldPath);
else { else {
struct stat dirst; struct stat dirst;
if (fstat(fromFD, &dirst) < 0) { if (fstat(oldFD, &dirst) < 0) {
// failed to grab stat information, fstat() sets errno // failed to grab stat information, fstat() sets errno
return -1; return -1;
} }
if ((dirst.st_mode & S_IFDIR) != 0) { if ((dirst.st_mode & S_IFDIR) != 0) {
// fd does not point to a directory // oldFd does not point to a directory
errno = ENOTDIR; errno = ENOTDIR;
return -1; return -1;
} }
fromDirPath = (char *)malloc(MAXPATHLEN); oldDirPath = (char *)malloc(MAXPATHLEN);
if (fromDirPath == NULL) { if (oldDirPath == NULL) {
// ran out of memory allocating fromDirPath // ran out of memory allocating oldDirPath
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
if (fcntl(fromFD, F_GETPATH, fromDirPath) < 0) { if (fcntl(oldFD, F_GETPATH, oldDirPath) < 0) {
// failed to get the path of fromFD, fcntl sets errno // failed to get the path of oldFD, fcntl sets errno
free(fromDirPath); free(oldDirPath);
return -1; return -1;
} }
if (strlcat(fromDirPath, from, MAXPATHLEN) > MAXPATHLEN) { if (strlcat(oldDirPath, oldPath, MAXPATHLEN) > MAXPATHLEN) {
// full path is too long, set errno and return // full path is too long, set errno and return
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
free(fromDirPath); free(oldDirPath);
return -1; return -1;
} }
} }
if (toIsAbsolute || toFD == AT_FDCWD) if (newPathIsAbsolute || newFD == AT_FDCWD)
toDirPath = const_cast<char *>(to); newDirPath = const_cast<char *>(newPath);
else { else {
struct stat dirst; struct stat dirst;
if (fstat(toFD, &dirst) < 0) { if (fstat(newFD, &dirst) < 0) {
// failed to grab stat information, fstat() sets errno // failed to grab stat information, fstat() sets errno
return -1; return -1;
} }
if ((dirst.st_mode & S_IFDIR) != 0) { if ((dirst.st_mode & S_IFDIR) != 0) {
// fd does not point to a directory // newFD does not point to a directory
errno = ENOTDIR; errno = ENOTDIR;
return -1; return -1;
} }
toDirPath = (char *)malloc(MAXPATHLEN); newDirPath = (char *)malloc(MAXPATHLEN);
if (toDirPath == NULL) { if (newDirPath == NULL) {
// ran out of memory allocating toDirPath // ran out of memory allocating newDirPath
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
if (fcntl(toFD, F_GETPATH, toDirPath) < 0) { if (fcntl(newFD, F_GETPATH, newDirPath) < 0) {
// failed to get the path of toFD, fcntl sets errno // failed to get the path of newFD, fcntl sets errno
free(toDirPath); free(newDirPath);
return -1; return -1;
} }
if (strlcat(toDirPath, to, MAXPATHLEN) > MAXPATHLEN) { if (strlcat(newDirPath, newPath, MAXPATHLEN) > MAXPATHLEN) {
// full path is too long, set errno and return // full path is too long, set errno and return
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
free(toDirPath); free(newDirPath);
return -1; return -1;
} }
} }
int status = rename(fromDirPath, toDirPath); int status = rename(oldDirPath, newDirPath);
if (!fromIsAbsolute && fromFD != AT_FDCWD) if (!oldPathIsAbsolute && oldFD != AT_FDCWD)
free(fromDirPath); free(oldDirPath);
if (!toIsAbsolute && toFD != AT_FDCWD) if (!newPathIsAbsolute && newFD != AT_FDCWD)
free(toDirPath); free(newDirPath);
return status; return status;
} }
@@ -812,21 +812,21 @@ linkat(int oldFD, const char *oldPath, int newFD, const char *newPath,
return -1; return -1;
} }
bool oldIsAbsolute = false; bool oldPathIsAbsolute = false;
bool newIsAbsolute = false; bool newPathIsAbsolute = false;
if (oldPath && oldPath[0] == '/') if (oldPath && oldPath[0] == '/')
oldIsAbsolute = true; oldPathIsAbsolute = true;
if (newPath && newPath[0] == '/') if (newPath && newPath[0] == '/')
newIsAbsolute = true; newPathIsAbsolute = true;
if (oldIsAbsolute && newIsAbsolute) { if (oldPathIsAbsolute && newPathIsAbsolute) {
// oldPath and newPath are both absolute, call link() ignoring the fd's // oldPath and newPath are both absolute, call link() ignoring the fd's
return link(oldPath, newPath); return link(oldPath, newPath);
} }
if ((!oldIsAbsolute && !oldFD) || (!newIsAbsolute && !newFD)) { if ((!oldPathIsAbsolute && !oldFD) || (!newPathIsAbsolute && !newFD)) {
// oldPath is not an absolute path and oldFD is not a valid file // oldPath is not an absolute path and oldFD is not a valid file
// descriptor or newPath is not an absolute path and newFD is not a // descriptor or newPath is not an absolute path and newFD is not a
// valid file descriptor // valid file descriptor
@@ -837,7 +837,7 @@ linkat(int oldFD, const char *oldPath, int newFD, const char *newPath,
char *oldDirPath; char *oldDirPath;
char *newDirPath; char *newDirPath;
if (oldIsAbsolute || oldFD == AT_FDCWD) if (oldPathIsAbsolute || oldFD == AT_FDCWD)
oldDirPath = const_cast<char *>(oldPath); oldDirPath = const_cast<char *>(oldPath);
else { else {
struct stat dirst; struct stat dirst;
@@ -873,7 +873,7 @@ linkat(int oldFD, const char *oldPath, int newFD, const char *newPath,
} }
} }
if (newIsAbsolute || newFD == AT_FDCWD) if (newPathIsAbsolute || newFD == AT_FDCWD)
newDirPath = const_cast<char *>(newPath); newDirPath = const_cast<char *>(newPath);
else { else {
struct stat dirst; struct stat dirst;
@@ -909,9 +909,9 @@ linkat(int oldFD, const char *oldPath, int newFD, const char *newPath,
} }
int status = link(oldDirPath, newDirPath); int status = link(oldDirPath, newDirPath);
if (!oldIsAbsolute && oldFD != AT_FDCWD) if (!oldPathIsAbsolute && oldFD != AT_FDCWD)
free(oldDirPath); free(oldDirPath);
if (!newIsAbsolute && newFD != AT_FDCWD) if (!newPathIsAbsolute && newFD != AT_FDCWD)
free(newDirPath); free(newDirPath);
return status; return status;
} }
+1 -1
View File
@@ -23,7 +23,7 @@ int fstatat(int fd, const char *path, struct stat *st, int flag);
int mkdirat(int fd, const char *path, mode_t mode); int mkdirat(int fd, const char *path, mode_t mode);
int mkfifoat(int fd, const char *path, mode_t mode); int mkfifoat(int fd, const char *path, mode_t mode);
int mknodat(int fd, const char *name, mode_t mode, dev_t dev); int mknodat(int fd, const char *name, mode_t mode, dev_t dev);
int renameat(int fromFD, const char* from, int toFD, const char* to); int renameat(int oldFD, const char* oldPath, int newFD, const char* newPath);
ssize_t readlinkat(int fd, const char *path, char *buffer, size_t bufferSize); ssize_t readlinkat(int fd, const char *path, char *buffer, size_t bufferSize);
int symlinkat(const char *toPath, int fd, const char *symlinkPath); int symlinkat(const char *toPath, int fd, const char *symlinkPath);