1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Store method - Takes a file URI and stores its content (for which it will
6 calculate the hashes) in the given destination. The input file will be
7 extracted based on its file extension (or with the given compressor if
8 called with one of the compatible symlinks) and potentially recompressed
9 based on the file extension of the destination filename.
11 ##################################################################### */
13 // Include Files /*{{{*/
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/hashes.h>
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/aptconfiguration.h>
22 #include "aptmethod.h"
33 class StoreMethod
: public aptMethod
35 std::string
const Prog
;
36 virtual bool Fetch(FetchItem
*Itm
) APT_OVERRIDE
;
40 explicit StoreMethod(std::string
const &pProg
) : aptMethod(pProg
.c_str(),"1.2",SingleInstance
| SendConfig
), Prog(pProg
) {};
43 static bool OpenFileWithCompressorByName(FileFd
&fileFd
, std::string
const &Filename
, unsigned int const Mode
, std::string
const &Name
)
46 return fileFd
.Open(Filename
, Mode
, FileFd::Extension
);
48 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
49 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
50 for (; compressor
!= compressors
.end(); ++compressor
)
51 if (compressor
->Name
== Name
)
53 if (compressor
== compressors
.end())
54 return _error
->Error("Extraction of file %s requires unknown compressor %s", Filename
.c_str(), Name
.c_str());
55 return fileFd
.Open(Filename
, Mode
, *compressor
);
60 bool StoreMethod::Fetch(FetchItem
*Itm
) /*{{{*/
63 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
66 Res
.Filename
= Itm
->DestFile
;
69 // Open the source and destination files
71 if (_config
->FindB("Method::Compress", false) == false)
73 if (OpenFileWithCompressorByName(From
, Path
, FileFd::ReadOnly
, Prog
) == false)
75 if(From
.IsCompressed() && From
.FileSize() == 0)
76 return _error
->Error(_("Empty files can't be valid archives"));
79 From
.Open(Path
, FileFd::ReadOnly
, FileFd::Extension
);
80 if (From
.IsOpen() == false || From
.Failed() == true)
84 if (Itm
->DestFile
!= "/dev/null" && Itm
->DestFile
!= Path
)
86 if (_config
->FindB("Method::Compress", false) == false)
87 To
.Open(Itm
->DestFile
, FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
, FileFd::Extension
);
88 else if (OpenFileWithCompressorByName(To
, Itm
->DestFile
, FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
, Prog
) == false)
91 if (To
.IsOpen() == false || To
.Failed() == true)
96 // Read data from source, generate checksums and write
97 Hashes
Hash(Itm
->ExpectedHashes
);
102 unsigned char Buffer
[4*1024];
103 unsigned long long Count
= 0;
105 if (!From
.Read(Buffer
,sizeof(Buffer
),&Count
))
115 Hash
.Add(Buffer
,Count
);
116 if (To
.IsOpen() && To
.Write(Buffer
,Count
) == false)
129 if (TransferModificationTimes(Path
.c_str(), Itm
->DestFile
.c_str(), Res
.LastModified
) == false)
132 // Return a Done response
133 Res
.TakeHashes(Hash
);
140 int main(int, char *argv
[])
142 return StoreMethod(flNotDir(argv
[0])).Run();