@@ -797,7 +797,7 @@ namespace {
797797 ++fs->fdCount ;
798798 return fdNum;
799799 }
800- void RemoveFd2 (struct FSInternal * fs, struct Fd * fd, int i) {
800+ void RemoveFd (struct FSInternal * fs, struct Fd * fd, int i) {
801801 struct BaseINode * inode = fd->inode ;
802802 if (inode->nlink == 0 )
803803 RemoveINode (fs, inode);
@@ -813,24 +813,6 @@ namespace {
813813 Realloc (fs->fds , fs->fdCount , fs->fdCount - 1 );
814814 --fs->fdCount ;
815815 }
816- int RemoveFd (struct FSInternal * fs, unsigned int fd) {
817- if (fs->fdCount == 0 )
818- return -FS_EBADF;
819- int low = 0 ;
820- int high = fs->fdCount - 1 ;
821- while (low <= high) {
822- int mid = low + ((high - low) / 2 );
823- struct Fd * f = fs->fds [mid];
824- if (f->fd == fd) {
825- RemoveFd2 (fs, f, mid);
826- return 0 ;
827- }
828- if (f->fd < fd)
829- low = mid + 1 ;
830- else high = mid - 1 ;
831- }
832- return -FS_EBADF;
833- }
834816 struct Fd * GetFd (struct FSInternal * fs, unsigned int fdNum) {
835817 if (fs->fdCount == 0 )
836818 return NULL ;
@@ -1251,7 +1233,22 @@ int FileSystem::Creat(const char* path, fs_mode_t mode) {
12511233int FileSystem::Close (unsigned int fd) {
12521234 struct FSInternal * fs = (struct FSInternal *)data;
12531235 ScopedLock lock (fs->mtx );
1254- return RemoveFd (fs, fd);
1236+ if (fs->fdCount == 0 )
1237+ return -FS_EBADF;
1238+ int low = 0 ;
1239+ int high = fs->fdCount - 1 ;
1240+ while (low <= high) {
1241+ int mid = low + ((high - low) / 2 );
1242+ struct Fd * f = fs->fds [mid];
1243+ if (f->fd == fd) {
1244+ RemoveFd (fs, f, mid);
1245+ return 0 ;
1246+ }
1247+ if (f->fd < fd)
1248+ low = mid + 1 ;
1249+ else high = mid - 1 ;
1250+ }
1251+ return -FS_EBADF;
12551252}
12561253int FileSystem::CloseRange (unsigned int fd, unsigned int maxFd) {
12571254 if (fd > maxFd)
@@ -1261,11 +1258,11 @@ int FileSystem::CloseRange(unsigned int fd, unsigned int maxFd) {
12611258 for (int i = 0 ; i != fs->fdCount ; ++i) {
12621259 struct Fd * f = fs->fds [i];
12631260 if (f->fd >= fd) {
1264- RemoveFd2 (fs, f, i);
1261+ RemoveFd (fs, f, i);
12651262 while (i != fs->fdCount ) {
12661263 f = fs->fds [i];
12671264 if (f->fd < maxFd)
1268- RemoveFd2 (fs, f, i);
1265+ RemoveFd (fs, f, i);
12691266 else break ;
12701267 }
12711268 break ;
@@ -1694,10 +1691,13 @@ int FileSystem::RenameAt2(
16941691 return res;
16951692 if (oldInode == newInode)
16961693 return 0 ;
1697- if (flags & FS_RENAME_NOREPLACE && newInode)
1698- return -FS_EEXIST;
1699- if (flags & FS_RENAME_EXCHANGE && !newInode)
1700- return -FS_ENOENT;
1694+ if (newInode) {
1695+ if (flags & FS_RENAME_NOREPLACE)
1696+ return -FS_EEXIST;
1697+ } else {
1698+ if (flags & FS_RENAME_EXCHANGE)
1699+ return -FS_ENOENT;
1700+ }
17011701 if (FS_S_ISDIR (oldInode->mode )) {
17021702 if (newInode) {
17031703 if (!FS_S_ISDIR (newInode->mode ))
0 commit comments