Skip to content

Commit 0453e8e

Browse files
committed
Add option to null-terminate filenames
1 parent 79d164f commit 0453e8e

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

cabal-install/Distribution/Client/CmdSdist.hs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{-# LANGUAGE RecordWildCards #-}
55
{-# LANGUAGE TupleSections #-}
66
{-# LANGUAGE ViewPatterns #-}
7-
module Distribution.Client.CmdSdist ( sdistCommand, sdistAction, packageToSdist ) where
7+
module Distribution.Client.CmdSdist ( sdistCommand, sdistAction, packageToSdist, OutputFormat(..), ArchiveFormat(..) ) where
88

99
import Distribution.Client.CmdErrorMessages
1010
( Plural(..), renderComponentKind )
@@ -101,10 +101,14 @@ sdistCommand = CommandUI
101101
"Set the name of the cabal.project file to search for in parent directories"
102102
sdistProjectFile (\pf flags -> flags { sdistProjectFile = pf })
103103
(reqArg "FILE" (succeedReadE Flag) flagToList)
104-
, option [] ["list-only"]
104+
, option ['l'] ["list-only"]
105105
"Just list the sources, do not make a tarball"
106106
sdistListSources (\v flags -> flags { sdistListSources = v })
107107
trueArg
108+
, option ['z'] ["null-sep"]
109+
"Separate the source files with NUL bytes rather than newlines."
110+
sdistNulSeparated (\v flags -> flags { sdistNulSeparated = v })
111+
trueArg
108112
, option [] ["archive-format"]
109113
"Choose what type of archive to create. No effect if given with '--list-only'"
110114
sdistArchiveFormat (\v flags -> flags { sdistArchiveFormat = v })
@@ -127,6 +131,7 @@ data SdistFlags = SdistFlags
127131
, sdistDistDir :: Flag FilePath
128132
, sdistProjectFile :: Flag FilePath
129133
, sdistListSources :: Flag Bool
134+
, sdistNulSeparated :: Flag Bool
130135
, sdistArchiveFormat :: Flag ArchiveFormat
131136
, sdistOutputPath :: Flag FilePath
132137
}
@@ -137,6 +142,7 @@ defaultSdistFlags = SdistFlags
137142
, sdistDistDir = mempty
138143
, sdistProjectFile = mempty
139144
, sdistListSources = toFlag False
145+
, sditNulSeparated = toFlag False
140146
, sdistArchiveFormat = toFlag TargzFormat
141147
, sdistOutputPath = mempty
142148
}
@@ -150,6 +156,7 @@ sdistAction SdistFlags{..} targetStrings globalFlags = do
150156
mProjectFile = flagToMaybe sdistProjectFile
151157
globalConfig = globalConfigFile globalFlags
152158
listSources = fromFlagOrDefault False sdistListSources
159+
nulSeparated = fromFlagOrDefault False sdistNulSeparated
153160
archiveFormat = fromFlagOrDefault TargzFormat sdistArchiveFormat
154161
mOutputPath = flagToMaybe sdistOutputPath
155162

@@ -169,14 +176,19 @@ sdistAction SdistFlags{..} targetStrings globalFlags = do
169176
Nothing -> return Nothing
170177

171178
let
172-
ext = case archiveFormat of
173-
TargzFormat -> "tar.gz"
174-
ZipFormat -> "zip"
175-
179+
format =
180+
if | listSources, nulSeparated -> SourceList '\n'
181+
| listSources -> SourceList '\0'
182+
| otherwise -> Archive archiveFormat
183+
184+
ext = case format of
185+
SourceList _ -> "list"
186+
Archive TargzFormat -> "tar.gz"
187+
Archive ZipFormat -> "zip"
188+
176189
outputPath pkg = case mOutputPath' of
177190
Just path
178191
| path == "-" -> "-"
179-
| listSources -> path </> prettyShow (packageId pkg) <.> "list"
180192
| otherwise -> path </> prettyShow (packageId pkg) <.> ext
181193
Nothing
182194
| listSources -> "-"
@@ -187,15 +199,20 @@ sdistAction SdistFlags{..} targetStrings globalFlags = do
187199
case reifyTargetSelectors localPkgs targetSelectors of
188200
Left errs -> die' verbosity . unlines . fmap renderTargetProblem $ errs
189201
Right pkgs
190-
| length pkgs > 1, Just "-" <- mOutputPath' ->
202+
| length pkgs > 1, not listSources, Just "-" <- mOutputPath' ->
191203
die' verbosity "Can't write multiple tarballs to standard output!"
192-
| otherwise -> mapM_ (\pkg -> packageToSdist verbosity listSources archiveFormat (outputPath pkg) pkg) pkgs
204+
| otherwise ->
205+
mapM_ (\pkg -> packageToSdist verbosity format (outputPath pkg) pkg) pkgs
193206

194207
data IsExec = Exec | NoExec
195208
deriving (Show, Eq)
196209

197-
packageToSdist :: Verbosity -> Bool -> ArchiveFormat -> FilePath -> UnresolvedSourcePackage -> IO ()
198-
packageToSdist verbosity listSources archiveFormat outputFile pkg = do
210+
data OutputFormat = SourceList Char
211+
| Archive ArchiveFormat
212+
deriving (Show, Eq)
213+
214+
packageToSdist :: Verbosity -> OutputFormat -> FilePath -> UnresolvedSourcePackage -> IO ()
215+
packageToSdist verbosity format outputFile pkg = do
199216
dir <- case packageSource pkg of
200217
LocalUnpackedPackage path -> return path
201218
_ -> die' verbosity "The impossible happened: a local package isn't local"
@@ -208,10 +225,10 @@ packageToSdist verbosity listSources archiveFormat outputFile pkg = do
208225
let write = if outputFile == "-" then BSL.putStr else BSL.writeFile outputFile
209226
files = nub . sortOn snd $ nonexec ++ exec
210227

211-
if
212-
| listSources -> do
228+
case format of
229+
SourceList nulSep -> do
213230
notice verbosity $ "File manifest for package " ++ prettyShow (packageId pkg) ++ ":\n"
214-
write (BSL.pack . unlines . fmap snd $ files)
231+
write (BSL.pack . (++ [nulSep]) . intercalate nulSep . fmap snd $ files)
215232
| archiveFormat == TargzFormat -> do
216233
let entriesM :: StateT (Set.Set FilePath) (WriterT [Tar.Entry] IO) ()
217234
entriesM = forM_ files $ \(perm, file) -> do

0 commit comments

Comments
 (0)