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/acquire-method.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/fileutl.h>
20 #include <apt-pkg/hashes.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/aptconfiguration.h>
23 #include "aptmethod.h"
34 class StoreMethod
: public aptMethod
36 std::string
const Prog
;
37 virtual bool Fetch(FetchItem
*Itm
) APT_OVERRIDE
;
41 explicit StoreMethod(std::string
const &pProg
) : aptMethod(pProg
.c_str(),"1.2",SingleInstance
| SendConfig
), Prog(pProg
) {};
44 static bool OpenFileWithCompressorByName(FileFd
&fileFd
, std::string
const &Filename
, unsigned int const Mode
, std::string
const &Name
)
47 return fileFd
.Open(Filename
, Mode
, FileFd::Extension
);
49 std::vector
<APT::Configuration::Compressor
> const compressors
= APT::Configuration::getCompressors();
50 std::vector
<APT::Configuration::Compressor
>::const_iterator compressor
= compressors
.begin();
51 for (; compressor
!= compressors
.end(); ++compressor
)
52 if (compressor
->Name
== Name
)
54 if (compressor
== compressors
.end())
55 return _error
->Error("Extraction of file %s requires unknown compressor %s", Filename
.c_str(), Name
.c_str());
56 return fileFd
.Open(Filename
, Mode
, *compressor
);
61 bool StoreMethod::Fetch(FetchItem
*Itm
) /*{{{*/
64 std::string Path
= Get
.Host
+ Get
.Path
; // To account for relative paths
67 Res
.Filename
= Itm
->DestFile
;
70 // Open the source and destination files
72 if (_config
->FindB("Method::Compress", false) == false)
74 if (OpenFileWithCompressorByName(From
, Path
, FileFd::ReadOnly
, Prog
) == false)
76 if(From
.FileSize() == 0)
77 return _error
->Error(_("Empty files can't be valid archives"));
80 From
.Open(Path
, FileFd::ReadOnly
, FileFd::Extension
);
81 if (From
.IsOpen() == false || From
.Failed() == true)
85 if (Itm
->DestFile
!= "/dev/null" && Itm
->DestFile
!= Path
)
87 if (_config
->FindB("Method::Compress", false) == false)
88 To
.Open(Itm
->DestFile
, FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
, FileFd::Extension
);
89 else if (OpenFileWithCompressorByName(To
, Itm
->DestFile
, FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
, Prog
) == false)
92 if (To
.IsOpen() == false || To
.Failed() == true)
97 // Read data from source, generate checksums and write
98 Hashes
Hash(Itm
->ExpectedHashes
);
103 unsigned char Buffer
[4*1024];
104 unsigned long long Count
= 0;
106 if (!From
.Read(Buffer
,sizeof(Buffer
),&Count
))
116 Hash
.Add(Buffer
,Count
);
117 if (To
.IsOpen() && To
.Write(Buffer
,Count
) == false)
130 // Transfer the modification times
131 if (Itm
->DestFile
!= "/dev/null")
134 if (stat(Path
.c_str(),&Buf
) != 0)
135 return _error
->Errno("stat",_("Failed to stat"));
137 struct timeval times
[2];
138 times
[0].tv_sec
= Buf
.st_atime
;
139 Res
.LastModified
= times
[1].tv_sec
= Buf
.st_mtime
;
140 times
[0].tv_usec
= times
[1].tv_usec
= 0;
141 if (utimes(Itm
->DestFile
.c_str(), times
) != 0)
142 return _error
->Errno("utimes",_("Failed to set modification time"));
145 // Return a Done response
146 Res
.TakeHashes(Hash
);
153 int main(int, char *argv
[])
155 setlocale(LC_ALL
, "");
157 StoreMethod
Mth(flNotDir(argv
[0]));