Commit graph

259 commits

Author SHA1 Message Date
Paul Capron
5522d66d11 Make mv work accross filesystems
This commit fixes Gabriel439/Haskell-Turtle-Library#37

On a POSIX OS, Prelude.mv would fail if the two given paths are not on
the same filesystem. That’s because under the hood mv simply calls
Filesystem.rename[1], which on POSIX forwards to the rename syscall[2],
and (most implementations of) rename don’t work with files on different
filesystems.

This commit makes mv catch any error returned by Filesystem.rename and,
when an error is triggered because one tries to do an across-filesystem
move, resort to using a (non-atomic) "Filesystem.copyFile followed by
Filesystem.removeFile" combo to move the file. That way Prelude.mv behaves
similarly to POSIX mv[3]. Note however that, unlike POSIX mv, an across-fs
*directory* moving is (still) not supported.

Detecting why exactly Filesystem.rename failed is the tricky part.
On error, it returns a standard IOError[4], which is an opaque type.
We can get the error type[5] of this error. But the standard 2010 Haskell
only defines a limited set of common error types (EOF, already exists, …),
which does not cover the failure we are interested in (i.e.: rename
syscall returned EXDEV[6] “Invalid cross-device link”). Hence in our case,
Filesystem.rename throws an IOError with an unclassified type that
we cannot for sure relate to an across-fs issue.

We can get more precision about IO errors if we are willing to depend on
GHC-only stuff. Given that GHC is de facto *the* Haskell compiler, we are
OK to trade compiler-independance for much better handling of our case.

GHC exports a bunch of new IOErrorType on top of the standard ones.
For the failure we are concerned with, the IOErrorType is
UnsupportedOperation. This type is also used by GHC for a bunch of other
unusual errors[7], but among the possible errors returned by rename
(according to the POSIX spec and most implementations man pages), only
EXDEV is mapped to UnsupportedOperation (according to Foreign.C.Error
source code). Hence if we get an UnsupportedOperation from
Filesystem.rename, we can assume that it is because of an across-fs issue.

Note that we could be absolutely sure that the IOError is caused by EXDEV
if we were willing to be even less portable/more brittle:

  import Foreign.C.Error (Errno(Errno), eXDEV)
  import GHC.IO.Exception (ioe_errno)

  mv oldPath newPath = liftIO $ catchIOError (Filesystem.rename oldPath newPath)
   (\ioe -> if fmap Errno (ioe_errno ioe) == Just eXDEV
                then do
                    Filesystem.copyFile oldPath newPath
                    Filesystem.removeFile oldPath
                else ioError ioe)

But it seems that as-precise-as-possible error diagnostic at the price of
such dependence on GHC internals and low-level stuff is not worth it.
Checking against UnsupportedOperation, while not theoretically perfect, seems
good enough.

1: https://hackage.haskell.org/package/system-fileio-0.3.16.3/docs/Filesystem.html#v:rename
2: http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
3: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/mv.html
4: https://hackage.haskell.org/package/base-4.7.0.1/docs/System-IO-Error.html#t:IOError
5: https://hackage.haskell.org/package/base-4.7.0.1/docs/System-IO-Error.html#t:IOErrorType
6: http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_03.html
7: https://hackage.haskell.org/package/base-4.7.0.0/docs/src/Foreign-C-Error.html#errnoToIOError
2015-10-07 15:09:44 +02:00
Gabriel Gonzalez
3e6ac8e49f Added extra Windows scripting instructions. Fixes #108 2015-10-03 17:21:43 -07:00
Gabriel Gonzalez
7575f36fb2 Version 1.2.1 => 1.2.2 2015-09-27 09:31:11 -07:00
Gabriel Gonzalez
e054dcfb20 Added documentation for rws 2015-09-27 09:30:43 -07:00
Gabriel Gonzalez
e37300d13b Increased upper bound on optparse-applicative 2015-09-27 09:29:51 -07:00
Gabriel Gonzalez
2830341c09 Revert version bump
This is no longer necessary as I rolled back the breaking change and this
has not yet been released to Hackage.
2015-09-27 09:18:59 -07:00
Gabriel Gonzalez
9bfb25fe86 Use both mktemp and mktempfile
I added back the old `mktemp` behavior and split off the new behavior as
the `mktempfile` function.  This avoids a breaking API change and also
lets users choose how much convenience/safety they want.
2015-09-27 09:16:58 -07:00
Gabriel Gonzalez
5862c005fb Merge pull request #107 from massysett/master
Add text to tutorial describing the Stack shebang line
2015-09-25 19:55:27 -07:00
Omari Norman
eed56dfd21 Add text to tutorial describing the Stack shebang line
I already knew what Stack is and was excited to see it can be used
in this manner, but I had to spend several minutes searching the web
to figure out what was going on with the second line in these
scripts.
2015-09-25 22:09:31 -04:00
Gabriel Gonzalez
fe748817cc Merge pull request #105 from sjakobi/redundant-imports
Silence a few compiler warnings
2015-09-14 09:07:55 -07:00
Simon Jakobi
8bdf83fac2 Silence a few compiler warnings
Most warnings were "redundant import" warnings due to the inclusion
of Applicative and Monoid in the Prelude of base-4.8.*.
2015-09-14 00:00:57 +02:00
Gabriel Gonzalez
e95f0dc997 Merge pull request #104 from mitchellwrosen/master
Terminate processes when process thread dies #103
2015-09-09 15:06:19 -07:00
Mitchell Rosen
de972c8f9b Terminate processes when process thread dies #103 2015-09-09 15:00:58 -07:00
Gabriel Gonzalez
a7e9ad8772 Merge branch 'cufp' 2015-09-03 14:58:45 -07:00
Gabriel Gonzalez
17aefb8e54 Updated slides 2015-09-03 14:58:18 -07:00
Gabriel Gonzalez
490b3e25dd Add begins/ends/contains and clarify sed. Fixes #49 2015-09-02 11:29:47 -07:00
Gabriel Gonzalez
7b35eb4da8 Backup 2015-09-02 11:02:54 -07:00
Gabriel Gonzalez
a87bfa238c Changes 2015-09-01 17:24:41 -07:00
Gabriel Gonzalez
c833de0b45 Simplify mktemp. Fixes #98 2015-08-23 20:16:29 -07:00
Gabriel Gonzalez
681f5d3da9 Added paste, cut, endless. Fixes #95 2015-08-22 19:21:42 -07:00
Gabriel Gonzalez
f4bd366e29 Fix shadowed variable warning 2015-08-22 13:34:58 -07:00
Gabriel Gonzalez
a4d1d3072d Added nl 2015-08-22 13:33:16 -07:00
Gabriel Gonzalez
bc52e08f28 Add lowerBounded. Fixes #94 2015-08-22 13:03:42 -07:00
Gabriel Gonzalez
8f4a39256f Add lsif. Fixes #39 2015-08-22 12:54:34 -07:00
Gabriel Gonzalez
b824122bcf Merge branch 'master' of github.com:Gabriel439/Haskell-Turtle-Library 2015-08-22 12:45:01 -07:00
Gabriel Gonzalez
a2fff2acf9 Make it easier to discover the format fp trick. Fixes #97 2015-08-22 12:43:36 -07:00
Gabriel Gonzalez
b7ccdc6f23 Merge pull request #99 from rpglover64/master
`sed` now errors if passed a pattern matching the empty string
2015-08-22 12:35:34 -07:00
Alex Rozenshteyn
e02fae1f75 Use matchesEmpty predicate in sed 2015-08-18 21:27:55 -04:00
Index Int
da98f7eee8 Update README.md to use Stack
fixes #93
2015-08-13 16:25:18 +03:00
Gabriel Gonzalez
cffe0ef573 Generalized types of (.&&.) and (.||.) 2015-08-09 17:13:01 -07:00
Gabriel Gonzalez
a6225e6e5b Add with to exports. Fixes #92 2015-08-09 17:10:52 -07:00
Gabriel Gonzalez
096fde098c Version 1.2.0 => 1.2.1 2015-08-05 08:34:52 -07:00
Gabriel Gonzalez
ba32205ddd Removed unnecessary --resolver flag from tutorial examples 2015-08-02 10:51:04 -07:00
Gabriel Gonzalez
aef155925b Improved exception safety of external process runners 2015-08-02 10:35:15 -07:00
Gabriel Gonzalez
537aea2f02 Remove unnecessary hFlushes 2015-08-02 09:15:14 -07:00
Gabriel Gonzalez
aa821df59f Merge pull request #91 from Gabriel439/usestack
Update tutorial to use `stack`
2015-07-25 15:22:51 -07:00
Gabriel Gonzalez
4f907f177a Finally fixed the issues with system commands. Fixes #72. Fixes #82 2015-07-23 19:23:20 -07:00
Gabriel Gonzalez
6c9b0ffe7f Update tutorial to use stack 2015-07-22 18:48:55 -07:00
Gabriel Gonzalez
2dbfb45eec Merge pull request #90 from menelaos/fixdocumentation
Fix `find` example in documentation.
2015-07-20 21:57:05 -05:00
Andreas Schacker
c8d4fe1168 Fix find example in documentation. 2015-07-20 17:01:41 +02:00
Gabriel Gonzalez
9295e8134f Fixed tutorial examples. Fixes #88 2015-07-05 15:37:08 -07:00
Gabriel Gonzalez
3682328eea Added tutorial section on command-line options parsing 2015-07-04 20:05:37 -07:00
Gabriel Gonzalez
635e69e4e8 Added argPath and optPath 2015-07-04 20:05:27 -07:00
Gabriel Gonzalez
ab15df5e32 Merge branch 'ggonzalez/options2' 2015-07-04 13:42:03 -07:00
Gabriel Gonzalez
e6014004cf Added ShortName type synonym 2015-07-04 10:46:06 -07:00
Gabriel Gonzalez
c37e941a49 Added wc equivalent improved du. Fixes #71 2015-07-04 10:41:50 -07:00
Gabriel Gonzalez
c8d3ebed38 Updated options module
Changed arg/opt parsing to return monomorphic values and made short name
argument mandatory
2015-07-04 09:30:19 -07:00
Gabriel Gonzalez
c1a97ca50e Add cache. Fixes #74 2015-07-04 09:16:18 -07:00
Gabriel Gonzalez
b800a773d2 Add outhandle. Fixes #75 2015-07-04 08:26:55 -07:00
Gabriel Gonzalez
f9c0516844 Added hostname. Fixes #86 2015-07-04 08:23:17 -07:00