-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
renameFile now consistently reports an error if the destination is a directory #8
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -638,24 +638,31 @@ Either path refers to an existing directory. | |
|
||
renameFile :: FilePath -> FilePath -> IO () | ||
renameFile opath npath = do | ||
-- XXX this test isn't performed atomically with the following rename | ||
#ifdef mingw32_HOST_OS | ||
-- ToDo: use Win32 API | ||
withFileOrSymlinkStatus "renameFile" opath $ \st -> do | ||
is_dir <- isDirectory st | ||
#else | ||
stat <- Posix.getSymbolicLinkStatus opath | ||
let is_dir = Posix.isDirectory stat | ||
#endif | ||
if is_dir | ||
then ioError (ioeSetErrorString | ||
(mkIOError InappropriateType "renameFile" Nothing (Just opath)) | ||
"is a directory") | ||
else do | ||
-- XXX the isDirectory tests are not performed atomically with the rename | ||
checkNotDir opath | ||
doRename `E.catch` renameExcHandler | ||
where checkNotDir path = do | ||
isdir <- pathIsDir path `E.catch` ((\ _ -> return False) :: IOException -> IO Bool) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use
|
||
when isdir $ dirIoError path | ||
dirIoError path = ioError $ ioeSetErrorString (mkIOError InappropriateType "renameFile" Nothing (Just path)) "is a directory" | ||
renameExcHandler :: IOException -> IO () | ||
renameExcHandler exc = do | ||
-- The underlying rename implementation throws odd exceptions | ||
-- sometimes when the destination is a directory. For example, | ||
-- Windows throws a permission error. In those cases check | ||
-- if the cause is actually the destination being a directory | ||
-- and throw InapprioriateType in that case. | ||
checkNotDir npath | ||
throw exc | ||
doRename :: IO () | ||
pathIsDir :: FilePath -> IO (Bool) | ||
#ifdef mingw32_HOST_OS | ||
Win32.moveFileEx opath npath Win32.mOVEFILE_REPLACE_EXISTING | ||
-- ToDo: use Win32 API | ||
pathIsDir path = withFileOrSymlinkStatus "renameFile" path isDirectory | ||
doRename = Win32.moveFileEx opath npath Win32.mOVEFILE_REPLACE_EXISTING | ||
#else | ||
Posix.rename opath npath | ||
pathIsDir path = Posix.isDirectory `fmap` Posix.getSymbolicLinkStatus path | ||
doRename = Posix.rename opath npath | ||
#endif | ||
|
||
#endif /* __GLASGOW_HASKELL__ */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import System.Directory | ||
import Control.Exception | ||
|
||
tmp1 = "T8482.tmp1" | ||
testdir = "T8482.dir" | ||
|
||
main = do | ||
writeFile tmp1 "hello" | ||
createDirectory testdir | ||
tryRenameFile testdir tmp1 >>= print -- InappropriateType | ||
tryRenameFile tmp1 testdir >>= print -- InappropriateType | ||
tryRenameFile tmp1 "." >>= print -- InappropriateType | ||
removeDirectory testdir | ||
removeFile tmp1 | ||
where tryRenameFile :: FilePath -> FilePath -> IO (Either IOException ()) | ||
tryRenameFile opath npath = try $ renameFile opath npath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Left T8482.dir: renameFile: inappropriate type (is a directory) | ||
Left T8482.dir: renameFile: inappropriate type (is a directory) | ||
Left .: renameFile: inappropriate type (is a directory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, unless the problem manifests itself elsewhere,
renameExcHandler
should be omitted for non-Windows systems.