]> git.saurik.com Git - apt.git/blob - methods/aptmethod.h
0963ea21e9b9003d191d754ae5305d570bb9fafc
[apt.git] / methods / aptmethod.h
1 #ifndef APT_APTMETHOD_H
2 #define APT_APTMETHOD_H
3
4 #include <apt-pkg/acquire-method.h>
5 #include <apt-pkg/configuration.h>
6 #include <apt-pkg/error.h>
7
8 #include <locale>
9 #include <string>
10
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15
16 #include <apti18n.h>
17
18 class aptMethod : public pkgAcqMethod
19 {
20 protected:
21 std::string Binary;
22
23 public:
24 virtual bool Configuration(std::string Message) APT_OVERRIDE
25 {
26 if (pkgAcqMethod::Configuration(Message) == false)
27 return false;
28
29 std::string const conf = std::string("Binary::") + Binary;
30 _config->MoveSubTree(conf.c_str(), NULL);
31
32 DropPrivsOrDie();
33
34 return true;
35 }
36
37 bool CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const
38 {
39 Hashes Hash(Itm->ExpectedHashes);
40 FileFd Fd;
41 if (Fd.Open(Res.Filename, FileFd::ReadOnly) == false || Hash.AddFD(Fd) == false)
42 return false;
43 Res.TakeHashes(Hash);
44 return true;
45 }
46
47 void Warning(const char *Format,...)
48 {
49 va_list args;
50 va_start(args,Format);
51 PrintStatus("104 Warning", Format, args);
52 va_end(args);
53 }
54
55 bool TransferModificationTimes(char const * const From, char const * const To, time_t &LastModified)
56 {
57 if (strcmp(To, "/dev/null") == 0)
58 return true;
59
60 struct stat Buf2;
61 if (lstat(To, &Buf2) != 0 || S_ISLNK(Buf2.st_mode))
62 return true;
63
64 struct stat Buf;
65 if (stat(From, &Buf) != 0)
66 return _error->Errno("stat",_("Failed to stat"));
67
68 // we don't use utimensat here for compatibility reasons: #738567
69 struct timeval times[2];
70 times[0].tv_sec = Buf.st_atime;
71 LastModified = times[1].tv_sec = Buf.st_mtime;
72 times[0].tv_usec = times[1].tv_usec = 0;
73 if (utimes(To, times) != 0)
74 return _error->Errno("utimes",_("Failed to set modification time"));
75 return true;
76 }
77
78 aptMethod(char const * const Binary, char const * const Ver, unsigned long const Flags) :
79 pkgAcqMethod(Ver, Flags), Binary(Binary)
80 {
81 try {
82 std::locale::global(std::locale(""));
83 } catch (...) {
84 setlocale(LC_ALL, "");
85 }
86 }
87 };
88
89 #endif