]>
Commit | Line | Data |
---|---|---|
92173b19 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
92173b19 AL |
3 | /* ###################################################################### |
4 | ||
9bd2313a DK |
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. | |
10 | ||
92173b19 AL |
11 | ##################################################################### */ |
12 | /*}}}*/ | |
13 | // Include Files /*{{{*/ | |
ea542140 DK |
14 | #include <config.h> |
15 | ||
0ec6b98b | 16 | #include <apt-pkg/configuration.h> |
453b82a3 DK |
17 | #include <apt-pkg/error.h> |
18 | #include <apt-pkg/fileutl.h> | |
63b1700f | 19 | #include <apt-pkg/hashes.h> |
453b82a3 DK |
20 | #include <apt-pkg/strutl.h> |
21 | #include <apt-pkg/aptconfiguration.h> | |
23e64f6d | 22 | #include "aptmethod.h" |
92173b19 | 23 | |
453b82a3 | 24 | #include <string.h> |
92173b19 | 25 | #include <sys/stat.h> |
246bbb61 | 26 | #include <sys/time.h> |
453b82a3 DK |
27 | #include <string> |
28 | #include <vector> | |
29 | ||
d77559ac | 30 | #include <apti18n.h> |
92173b19 AL |
31 | /*}}}*/ |
32 | ||
9bd2313a | 33 | class StoreMethod : public aptMethod |
92173b19 | 34 | { |
23e64f6d | 35 | std::string const Prog; |
3b302846 | 36 | virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE; |
9983999d | 37 | |
23e64f6d | 38 | public: |
9983999d | 39 | |
9bd2313a | 40 | explicit StoreMethod(std::string const &pProg) : aptMethod(pProg.c_str(),"1.2",SingleInstance | SendConfig), Prog(pProg) {}; |
23e64f6d | 41 | }; |
63b1700f | 42 | |
9bd2313a | 43 | static bool OpenFileWithCompressorByName(FileFd &fileFd, std::string const &Filename, unsigned int const Mode, std::string const &Name) |
92173b19 | 44 | { |
9bd2313a DK |
45 | if (Name == "store") |
46 | return fileFd.Open(Filename, Mode, FileFd::Extension); | |
d6bbcaad DK |
47 | |
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) | |
9bd2313a | 51 | if (compressor->Name == Name) |
d6bbcaad DK |
52 | break; |
53 | if (compressor == compressors.end()) | |
9bd2313a DK |
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); | |
56 | } | |
57 | ||
58 | ||
59 | /*}}}*/ | |
60 | bool StoreMethod::Fetch(FetchItem *Itm) /*{{{*/ | |
61 | { | |
62 | URI Get = Itm->Uri; | |
63 | std::string Path = Get.Host + Get.Path; // To account for relative paths | |
64 | ||
65 | FetchResult Res; | |
66 | Res.Filename = Itm->DestFile; | |
67 | URIStart(Res); | |
d6bbcaad | 68 | |
63b1700f | 69 | // Open the source and destination files |
af9e40c9 | 70 | FileFd From; |
0ec6b98b DK |
71 | if (_config->FindB("Method::Compress", false) == false) |
72 | { | |
9bd2313a DK |
73 | if (OpenFileWithCompressorByName(From, Path, FileFd::ReadOnly, Prog) == false) |
74 | return false; | |
479f6fa4 | 75 | if(From.IsCompressed() && From.FileSize() == 0) |
0ec6b98b | 76 | return _error->Error(_("Empty files can't be valid archives")); |
0ec6b98b DK |
77 | } |
78 | else | |
9bd2313a | 79 | From.Open(Path, FileFd::ReadOnly, FileFd::Extension); |
af9e40c9 DK |
80 | if (From.IsOpen() == false || From.Failed() == true) |
81 | return false; | |
82 | ||
83 | FileFd To; | |
9bd2313a | 84 | if (Itm->DestFile != "/dev/null" && Itm->DestFile != Path) |
af9e40c9 DK |
85 | { |
86 | if (_config->FindB("Method::Compress", false) == false) | |
9bd2313a DK |
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) | |
89 | return false; | |
af9e40c9 DK |
90 | |
91 | if (To.IsOpen() == false || To.Failed() == true) | |
92 | return false; | |
93 | To.EraseOnFailure(); | |
0ec6b98b | 94 | } |
0ec6b98b | 95 | |
127e6df3 | 96 | // Read data from source, generate checksums and write |
9224ce3d | 97 | Hashes Hash(Itm->ExpectedHashes); |
63b1700f | 98 | bool Failed = false; |
af9e40c9 | 99 | Res.Size = 0; |
9bd2313a | 100 | while (1) |
63b1700f AL |
101 | { |
102 | unsigned char Buffer[4*1024]; | |
650faab0 | 103 | unsigned long long Count = 0; |
9bd2313a | 104 | |
127e6df3 | 105 | if (!From.Read(Buffer,sizeof(Buffer),&Count)) |
63b1700f | 106 | { |
af9e40c9 DK |
107 | if (To.IsOpen()) |
108 | To.OpFail(); | |
127e6df3 | 109 | return false; |
63b1700f | 110 | } |
63b1700f AL |
111 | if (Count == 0) |
112 | break; | |
af9e40c9 | 113 | Res.Size += Count; |
127e6df3 | 114 | |
63b1700f | 115 | Hash.Add(Buffer,Count); |
af9e40c9 | 116 | if (To.IsOpen() && To.Write(Buffer,Count) == false) |
2204bd80 | 117 | { |
678bc33e AL |
118 | Failed = true; |
119 | break; | |
9bd2313a | 120 | } |
63b1700f | 121 | } |
9bd2313a | 122 | |
127e6df3 | 123 | From.Close(); |
246bbb61 | 124 | To.Close(); |
9ce3cfc9 | 125 | |
63b1700f AL |
126 | if (Failed == true) |
127 | return false; | |
9ce3cfc9 | 128 | |
93bf083d | 129 | // Transfer the modification times |
af9e40c9 DK |
130 | if (Itm->DestFile != "/dev/null") |
131 | { | |
132 | struct stat Buf; | |
133 | if (stat(Path.c_str(),&Buf) != 0) | |
134 | return _error->Errno("stat",_("Failed to stat")); | |
135 | ||
136 | struct timeval times[2]; | |
137 | times[0].tv_sec = Buf.st_atime; | |
138 | Res.LastModified = times[1].tv_sec = Buf.st_mtime; | |
139 | times[0].tv_usec = times[1].tv_usec = 0; | |
140 | if (utimes(Itm->DestFile.c_str(), times) != 0) | |
141 | return _error->Errno("utimes",_("Failed to set modification time")); | |
142 | } | |
9ce3cfc9 | 143 | |
93bf083d | 144 | // Return a Done response |
a7c835af | 145 | Res.TakeHashes(Hash); |
63b1700f | 146 | |
93bf083d | 147 | URIDone(Res); |
93bf083d AL |
148 | return true; |
149 | } | |
150 | /*}}}*/ | |
151 | ||
65512241 | 152 | int main(int, char *argv[]) |
93bf083d | 153 | { |
8b79c94a | 154 | return StoreMethod(flNotDir(argv[0])).Run(); |
92173b19 | 155 | } |