summaryrefslogtreecommitdiff
path: root/methods
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2015-11-04 13:19:14 +0100
committerDavid Kalnischkies <david@kalnischkies.de>2015-11-05 12:21:33 +0100
commit30c8107e9c56d7d78dcf9136f94aeed9d631dfb3 (patch)
treec8d9a601ab50de8d8cd059c2f84e94fedf4f8213 /methods
parent23d35ec15a849ee755f51a99939b0131e8faefa5 (diff)
drop privileges in copy:// method as we do for file://
Continueing on the track of dropping privileges in all methods, lets drop it in copy, too, as the reasoning for it is very similar to file and the interaction between the too quiet interesting as copy kinda surfed as a fallback for file not being able to read the file. Both now show a better error message as well as it was previously claiming to have a hashsum mismatch, given that it couldn't read the file. Git-Dch: Ignore
Diffstat (limited to 'methods')
-rw-r--r--methods/aptmethod.h37
-rw-r--r--methods/copy.cc26
-rw-r--r--methods/file.cc29
3 files changed, 54 insertions, 38 deletions
diff --git a/methods/aptmethod.h b/methods/aptmethod.h
new file mode 100644
index 000000000..61d7b78f1
--- /dev/null
+++ b/methods/aptmethod.h
@@ -0,0 +1,37 @@
+#ifndef APT_APTMETHOD_H
+#define APT_APTMETHOD_H
+
+#include <apt-pkg/acquire-method.h>
+
+#include <string>
+
+class aptMethod : public pkgAcqMethod
+{
+ char const * const Binary;
+ public:
+ virtual bool Configuration(std::string Message) APT_OVERRIDE;
+
+ bool CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const;
+
+ aptMethod(char const * const Binary, char const * const Ver, unsigned long const Flags) : pkgAcqMethod(Ver, Flags), Binary(Binary) {};
+};
+bool aptMethod::Configuration(std::string Message)
+{
+ if (pkgAcqMethod::Configuration(Message) == false)
+ return false;
+
+ DropPrivsOrDie();
+
+ return true;
+}
+bool aptMethod::CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const
+{
+ Hashes Hash(Itm->ExpectedHashes);
+ FileFd Fd;
+ if (Fd.Open(Res.Filename, FileFd::ReadOnly) == false || Hash.AddFD(Fd) == false)
+ return false;
+ Res.TakeHashes(Hash);
+ return true;
+}
+
+#endif
diff --git a/methods/copy.cc b/methods/copy.cc
index 373ad3604..e515b2def 100644
--- a/methods/copy.cc
+++ b/methods/copy.cc
@@ -17,6 +17,7 @@
#include <apt-pkg/error.h>
#include <apt-pkg/hashes.h>
#include <apt-pkg/configuration.h>
+#include "aptmethod.h"
#include <string>
#include <sys/stat.h>
@@ -25,23 +26,14 @@
#include <apti18n.h>
/*}}}*/
-class CopyMethod : public pkgAcqMethod
+class CopyMethod : public aptMethod
{
virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
- void CalculateHashes(FetchItem const * const Itm, FetchResult &Res);
-
+
public:
-
- CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
-};
-void CopyMethod::CalculateHashes(FetchItem const * const Itm, FetchResult &Res)
-{
- Hashes Hash(Itm->ExpectedHashes);
- FileFd Fd(Res.Filename, FileFd::ReadOnly);
- Hash.AddFD(Fd);
- Res.TakeHashes(Hash);
-}
+ CopyMethod() : aptMethod("copy", "1.0",SingleInstance | SendConfig) {};
+};
// CopyMethod::Fetch - Fetch a file /*{{{*/
// ---------------------------------------------------------------------
@@ -76,12 +68,7 @@ bool CopyMethod::Fetch(FetchItem *Itm)
FileFd From(File,FileFd::ReadOnly);
FileFd To(Itm->DestFile,FileFd::WriteAtomic);
To.EraseOnFailure();
- if (_error->PendingError() == true)
- {
- To.OpFail();
- return false;
- }
-
+
// Copy the file
if (CopyFile(From,To) == false)
{
@@ -101,7 +88,6 @@ bool CopyMethod::Fetch(FetchItem *Itm)
return _error->Errno("utimes",_("Failed to set modification time"));
CalculateHashes(Itm, Res);
-
URIDone(Res);
return true;
}
diff --git a/methods/file.cc b/methods/file.cc
index 8a087c36d..4e3410078 100644
--- a/methods/file.cc
+++ b/methods/file.cc
@@ -21,6 +21,7 @@
#include <apt-pkg/hashes.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/strutl.h>
+#include "aptmethod.h"
#include <string>
#include <sys/stat.h>
@@ -28,24 +29,13 @@
#include <apti18n.h>
/*}}}*/
-class FileMethod : public pkgAcqMethod
+class FileMethod : public aptMethod
{
virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
- virtual bool Configuration(std::string Message) APT_OVERRIDE;
public:
-
- FileMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig | LocalOnly) {};
+ FileMethod() : aptMethod("file", "1.0", SingleInstance | SendConfig | LocalOnly) {};
};
-bool FileMethod::Configuration(std::string Message)
-{
- if (pkgAcqMethod::Configuration(Message) == false)
- return false;
-
- DropPrivsOrDie();
-
- return true;
-}
// FileMethod::Fetch - Fetch a file /*{{{*/
// ---------------------------------------------------------------------
@@ -78,6 +68,7 @@ bool FileMethod::Fetch(FetchItem *Itm)
if (Res.IMSHit != true)
RemoveFile("file", Itm->DestFile);
+ int olderrno = 0;
// See if the file exists
if (stat(File.c_str(),&Buf) == 0)
{
@@ -92,11 +83,10 @@ bool FileMethod::Fetch(FetchItem *Itm)
Res.IMSHit = true;
}
- Hashes Hash(Itm->ExpectedHashes);
- FileFd Fd(File, FileFd::ReadOnly);
- Hash.AddFD(Fd);
- Res.TakeHashes(Hash);
+ CalculateHashes(Itm, Res);
}
+ else
+ olderrno = errno;
if (Res.IMSHit == false)
URIStart(Res);
@@ -128,7 +118,10 @@ bool FileMethod::Fetch(FetchItem *Itm)
else if (Res.Filename.empty() == false)
URIDone(Res);
else
- return _error->Error(_("File not found"));
+ {
+ errno = olderrno;
+ return _error->Errno(File.c_str(), _("File not found"));
+ }
return true;
}