diff options
Diffstat (limited to 'methods')
-rw-r--r-- | methods/bzip2.cc | 9 | ||||
-rw-r--r-- | methods/cdrom.cc | 5 | ||||
-rw-r--r-- | methods/connect.cc | 20 | ||||
-rw-r--r-- | methods/connect.h | 5 | ||||
-rw-r--r-- | methods/copy.cc | 7 | ||||
-rw-r--r-- | methods/file.cc | 11 | ||||
-rw-r--r-- | methods/ftp.cc | 28 | ||||
-rw-r--r-- | methods/ftp.h | 18 | ||||
-rw-r--r-- | methods/gpgv.cc | 22 | ||||
-rw-r--r-- | methods/gzip.cc | 8 | ||||
-rw-r--r-- | methods/http.cc | 102 | ||||
-rw-r--r-- | methods/http.h | 64 | ||||
-rw-r--r-- | methods/http_main.cc | 2 | ||||
-rw-r--r-- | methods/https.cc | 40 | ||||
-rw-r--r-- | methods/https.h | 9 | ||||
-rw-r--r-- | methods/makefile | 4 | ||||
-rw-r--r-- | methods/mirror.cc | 30 | ||||
-rw-r--r-- | methods/mirror.h | 25 | ||||
-rw-r--r-- | methods/rfc2553emu.cc | 4 | ||||
-rw-r--r-- | methods/rred.cc | 130 | ||||
-rw-r--r-- | methods/rsh.cc | 48 | ||||
-rw-r--r-- | methods/rsh.h | 24 |
22 files changed, 351 insertions, 264 deletions
diff --git a/methods/bzip2.cc b/methods/bzip2.cc index 42932dded..8e7e46557 100644 --- a/methods/bzip2.cc +++ b/methods/bzip2.cc @@ -13,11 +13,14 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/fileutl.h> #include <apt-pkg/error.h> #include <apt-pkg/acquire-method.h> #include <apt-pkg/strutl.h> #include <apt-pkg/hashes.h> +#include <apt-pkg/configuration.h> #include <sys/stat.h> #include <unistd.h> @@ -45,9 +48,9 @@ class Bzip2Method : public pkgAcqMethod bool Bzip2Method::Fetch(FetchItem *Itm) { URI Get = Itm->Uri; - string Path = Get.Host + Get.Path; // To account for relative paths + std::string Path = Get.Host + Get.Path; // To account for relative paths - string GzPathOption = "Dir::bin::"+string(Prog); + std::string GzPathOption = "Dir::bin::" + std::string(Prog); FetchResult Res; Res.Filename = Itm->DestFile; @@ -76,7 +79,7 @@ bool Bzip2Method::Fetch(FetchItem *Itm) SetCloseExec(STDOUT_FILENO,false); const char *Args[3]; - string Tmp = _config->Find(GzPathOption,Prog); + std::string Tmp = _config->Find(GzPathOption,Prog); Args[0] = Tmp.c_str(); Args[1] = "-d"; Args[2] = 0; diff --git a/methods/cdrom.cc b/methods/cdrom.cc index ae699dfc3..22d4b9164 100644 --- a/methods/cdrom.cc +++ b/methods/cdrom.cc @@ -8,12 +8,15 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/acquire-method.h> #include <apt-pkg/cdrom.h> #include <apt-pkg/cdromutl.h> #include <apt-pkg/error.h> #include <apt-pkg/configuration.h> #include <apt-pkg/fileutl.h> +#include <apt-pkg/strutl.h> #include <apt-pkg/hashes.h> #include <sys/stat.h> @@ -265,7 +268,7 @@ bool CDROMMethod::Fetch(FetchItem *Itm) Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); URIDone(Res); diff --git a/methods/connect.cc b/methods/connect.cc index a5af1f1a6..9a092a43c 100644 --- a/methods/connect.cc +++ b/methods/connect.cc @@ -11,9 +11,12 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ -#include "connect.h" +#include <config.h> + #include <apt-pkg/error.h> #include <apt-pkg/fileutl.h> +#include <apt-pkg/strutl.h> +#include <apt-pkg/acquire-method.h> #include <stdio.h> #include <errno.h> @@ -29,17 +32,18 @@ #include <arpa/inet.h> #include <netdb.h> +#include "connect.h" #include "rfc2553emu.h" #include <apti18n.h> /*}}}*/ -static string LastHost; +static std::string LastHost; static int LastPort = 0; static struct addrinfo *LastHostAddr = 0; static struct addrinfo *LastUsed = 0; // Set of IP/hostnames that we timed out before or couldn't resolve -static std::set<string> bad_addr; +static std::set<std::string> bad_addr; // RotateDNS - Select a new server from a DNS rotation /*{{{*/ // --------------------------------------------------------------------- @@ -56,7 +60,7 @@ void RotateDNS() // DoConnect - Attempt a connect operation /*{{{*/ // --------------------------------------------------------------------- /* This helper function attempts a connection to a single address. */ -static bool DoConnect(struct addrinfo *Addr,string Host, +static bool DoConnect(struct addrinfo *Addr,std::string Host, unsigned long TimeOut,int &Fd,pkgAcqMethod *Owner) { // Show a status indicator @@ -71,7 +75,7 @@ static bool DoConnect(struct addrinfo *Addr,string Host, Owner->Status(_("Connecting to %s (%s)"),Host.c_str(),Name); // if that addr did timeout before, we do not try it again - if(bad_addr.find(string(Name)) != bad_addr.end()) + if(bad_addr.find(std::string(Name)) != bad_addr.end()) return false; /* If this is an IP rotation store the IP we are using.. If something goes @@ -98,7 +102,7 @@ static bool DoConnect(struct addrinfo *Addr,string Host, /* This implements a timeout for connect by opening the connection nonblocking */ if (WaitFd(Fd,true,TimeOut) == false) { - bad_addr.insert(bad_addr.begin(), string(Name)); + bad_addr.insert(bad_addr.begin(), std::string(Name)); Owner->SetFailReason("Timeout"); return _error->Error(_("Could not connect to %s:%s (%s), " "connection timed out"),Host.c_str(),Service,Name); @@ -117,7 +121,7 @@ static bool DoConnect(struct addrinfo *Addr,string Host, Owner->SetFailReason("ConnectionRefused"); else if (errno == ETIMEDOUT) Owner->SetFailReason("ConnectionTimedOut"); - bad_addr.insert(bad_addr.begin(), string(Name)); + bad_addr.insert(bad_addr.begin(), std::string(Name)); return _error->Errno("connect",_("Could not connect to %s:%s (%s)."),Host.c_str(), Service,Name); } @@ -128,7 +132,7 @@ static bool DoConnect(struct addrinfo *Addr,string Host, // Connect - Connect to a server /*{{{*/ // --------------------------------------------------------------------- /* Performs a connection to the server */ -bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd, +bool Connect(std::string Host,int Port,const char *Service,int DefPort,int &Fd, unsigned long TimeOut,pkgAcqMethod *Owner) { if (_error->PendingError() == true) diff --git a/methods/connect.h b/methods/connect.h index 6f208e31d..bbe1bb35d 100644 --- a/methods/connect.h +++ b/methods/connect.h @@ -11,9 +11,10 @@ #define CONNECT_H #include <string> -#include <apt-pkg/acquire-method.h> -bool Connect(string To,int Port,const char *Service,int DefPort, +class pkgAcqMethod; + +bool Connect(std::string To,int Port,const char *Service,int DefPort, int &Fd,unsigned long TimeOut,pkgAcqMethod *Owner); void RotateDNS(); diff --git a/methods/copy.cc b/methods/copy.cc index a6bb372a3..e81d0022b 100644 --- a/methods/copy.cc +++ b/methods/copy.cc @@ -9,7 +9,10 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/fileutl.h> +#include <apt-pkg/strutl.h> #include <apt-pkg/acquire-method.h> #include <apt-pkg/error.h> #include <apt-pkg/hashes.h> @@ -35,7 +38,7 @@ class CopyMethod : public pkgAcqMethod bool CopyMethod::Fetch(FetchItem *Itm) { URI Get = Itm->Uri; - string File = Get.Path; + std::string File = Get.Path; // Stat the file and send a start message struct stat Buf; @@ -82,7 +85,7 @@ bool CopyMethod::Fetch(FetchItem *Itm) Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); URIDone(Res); diff --git a/methods/file.cc b/methods/file.cc index 9cdd5bc2d..7ed4e6f60 100644 --- a/methods/file.cc +++ b/methods/file.cc @@ -13,10 +13,13 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/acquire-method.h> #include <apt-pkg/error.h> #include <apt-pkg/hashes.h> #include <apt-pkg/fileutl.h> +#include <apt-pkg/strutl.h> #include <sys/stat.h> #include <unistd.h> @@ -38,7 +41,7 @@ class FileMethod : public pkgAcqMethod bool FileMethod::Fetch(FetchItem *Itm) { URI Get = Itm->Uri; - string File = Get.Path; + std::string File = Get.Path; FetchResult Res; if (Get.Host.empty() == false) return _error->Error(_("Invalid URI, local URIS must not start with //")); @@ -56,10 +59,10 @@ bool FileMethod::Fetch(FetchItem *Itm) } // See if we can compute a file without a .gz exentsion - string::size_type Pos = File.rfind(".gz"); + std::string::size_type Pos = File.rfind(".gz"); if (Pos + 3 == File.length()) { - File = string(File,0,Pos); + File = std::string(File,0,Pos); if (stat(File.c_str(),&Buf) == 0) { FetchResult AltRes; @@ -80,7 +83,7 @@ bool FileMethod::Fetch(FetchItem *Itm) Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); URIDone(Res); return true; diff --git a/methods/ftp.cc b/methods/ftp.cc index eb7fedd85..d55ac1224 100644 --- a/methods/ftp.cc +++ b/methods/ftp.cc @@ -15,11 +15,14 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/fileutl.h> #include <apt-pkg/acquire-method.h> #include <apt-pkg/error.h> #include <apt-pkg/hashes.h> #include <apt-pkg/netrc.h> +#include <apt-pkg/configuration.h> #include <sys/stat.h> #include <sys/time.h> @@ -30,7 +33,6 @@ #include <errno.h> #include <stdarg.h> #include <iostream> -#include <apti18n.h> // Internet stuff #include <netinet/in.h> @@ -41,6 +43,7 @@ #include "rfc2553emu.h" #include "connect.h" #include "ftp.h" +#include <apti18n.h> /*}}}*/ using namespace std; @@ -74,6 +77,7 @@ FTPConn::FTPConn(URI Srv) : Len(0), ServerFd(-1), DataFd(-1), { Debug = _config->FindB("Debug::Acquire::Ftp",false); PasvAddr = 0; + Buffer[0] = '\0'; } /*}}}*/ // FTPConn::~FTPConn - Destructor /*{{{*/ @@ -618,8 +622,7 @@ bool FTPConn::ExtGoPasv() } // Get a new passive address. - int Res; - if ((Res = getaddrinfo(IP.c_str(),PStr,&Hints,&PasvAddr)) != 0) + if (getaddrinfo(IP.c_str(),PStr,&Hints,&PasvAddr) != 0) return true; return true; @@ -628,7 +631,7 @@ bool FTPConn::ExtGoPasv() // FTPConn::Size - Return the size of a file /*{{{*/ // --------------------------------------------------------------------- /* Grab the file size from the server, 0 means no size or empty file */ -bool FTPConn::Size(const char *Path,unsigned long &Size) +bool FTPConn::Size(const char *Path,unsigned long long &Size) { // Query the size unsigned int Tag; @@ -638,7 +641,7 @@ bool FTPConn::Size(const char *Path,unsigned long &Size) return false; char *End; - Size = strtol(Msg.c_str(),&End,10); + Size = strtoull(Msg.c_str(),&End,10); if (Tag >= 400 || End == Msg.c_str()) Size = 0; return true; @@ -717,14 +720,13 @@ bool FTPConn::CreateDataFd() DataListenFd = -1; // Get the information for a listening socket. - struct addrinfo *BindAddr = 0; + struct addrinfo *BindAddr = NULL; struct addrinfo Hints; memset(&Hints,0,sizeof(Hints)); Hints.ai_socktype = SOCK_STREAM; Hints.ai_flags |= AI_PASSIVE; Hints.ai_family = ((struct sockaddr *)&ServerAddr)->sa_family; - int Res; - if ((Res = getaddrinfo(0,"0",&Hints,&BindAddr)) != 0) + if (getaddrinfo(0,"0",&Hints,&BindAddr) != 0 || BindAddr == NULL) return _error->Error(_("getaddrinfo was unable to get a listening socket")); // Construct the socket @@ -840,7 +842,7 @@ bool FTPConn::Finalize() // --------------------------------------------------------------------- /* This opens a data connection, sends REST and RETR and then transfers the file over. */ -bool FTPConn::Get(const char *Path,FileFd &To,unsigned long Resume, +bool FTPConn::Get(const char *Path,FileFd &To,unsigned long long Resume, Hashes &Hash,bool &Missing) { Missing = false; @@ -865,7 +867,7 @@ bool FTPConn::Get(const char *Path,FileFd &To,unsigned long Resume, if (Resume != 0) { - if (Hash.AddFD(To.Fd(),Resume) == false) + if (Hash.AddFD(To,Resume) == false) { _error->Errno("read",_("Problem hashing file")); return false; @@ -1003,7 +1005,7 @@ bool FtpMethod::Fetch(FetchItem *Itm) // Get the files information Status(_("Query")); - unsigned long Size; + unsigned long long Size; if (Server->Size(File,Size) == false || Server->ModTime(File,FailTime) == false) { @@ -1025,7 +1027,7 @@ bool FtpMethod::Fetch(FetchItem *Itm) struct stat Buf; if (stat(Itm->DestFile.c_str(),&Buf) == 0) { - if (Size == (unsigned)Buf.st_size && FailTime == Buf.st_mtime) + if (Size == (unsigned long long)Buf.st_size && FailTime == Buf.st_mtime) { Res.Size = Buf.st_size; Res.LastModified = Buf.st_mtime; @@ -1035,7 +1037,7 @@ bool FtpMethod::Fetch(FetchItem *Itm) } // Resume? - if (FailTime == Buf.st_mtime && Size > (unsigned)Buf.st_size) + if (FailTime == Buf.st_mtime && Size > (unsigned long long)Buf.st_size) Res.ResumePoint = Buf.st_size; } diff --git a/methods/ftp.h b/methods/ftp.h index d7f1f7fbe..2634f0732 100644 --- a/methods/ftp.h +++ b/methods/ftp.h @@ -10,6 +10,10 @@ #ifndef APT_FTP_H #define APT_FTP_H +#include <apt-pkg/strutl.h> + +#include <string> + class FTPConn { char Buffer[1024*10]; @@ -33,7 +37,7 @@ class FTPConn socklen_t ServerAddrLen; // Private helper functions - bool ReadLine(string &Text); + bool ReadLine(std::string &Text); bool Login(); bool CreateDataFd(); bool Finalize(); @@ -43,8 +47,8 @@ class FTPConn bool Comp(URI Other) {return Other.Host == ServerName.Host && Other.Port == ServerName.Port && Other.User == ServerName.User && Other.Password == ServerName.Password; }; // Raw connection IO - bool ReadResp(unsigned int &Ret,string &Text); - bool WriteMsg(unsigned int &Ret,string &Text,const char *Fmt,...); + bool ReadResp(unsigned int &Ret,std::string &Text); + bool WriteMsg(unsigned int &Ret,std::string &Text,const char *Fmt,...); // Connection control bool Open(pkgAcqMethod *Owner); @@ -53,9 +57,9 @@ class FTPConn bool ExtGoPasv(); // Query - bool Size(const char *Path,unsigned long &Size); + bool Size(const char *Path,unsigned long long &Size); bool ModTime(const char *Path, time_t &Time); - bool Get(const char *Path,FileFd &To,unsigned long Resume, + bool Get(const char *Path,FileFd &To,unsigned long long Resume, Hashes &MD5,bool &Missing); FTPConn(URI Srv); @@ -65,11 +69,11 @@ class FTPConn class FtpMethod : public pkgAcqMethod { virtual bool Fetch(FetchItem *Itm); - virtual bool Configuration(string Message); + virtual bool Configuration(std::string Message); FTPConn *Server; - static string FailFile; + static std::string FailFile; static int FailFd; static time_t FailTime; static void SigTerm(int); diff --git a/methods/gpgv.cc b/methods/gpgv.cc index 3ad3e8d84..25ba0d063 100644 --- a/methods/gpgv.cc +++ b/methods/gpgv.cc @@ -1,9 +1,11 @@ +#include <config.h> + #include <apt-pkg/error.h> #include <apt-pkg/acquire-method.h> #include <apt-pkg/strutl.h> #include <apt-pkg/fileutl.h> #include <apt-pkg/indexcopy.h> -#include <apti18n.h> +#include <apt-pkg/configuration.h> #include <utime.h> #include <stdio.h> @@ -12,9 +14,13 @@ #include <sys/wait.h> #include <iostream> #include <sstream> - #include <vector> +#include <apti18n.h> + +using std::string; +using std::vector; + #define GNUPGPREFIX "[GNUPG:]" #define GNUPGBADSIG "[GNUPG:] BADSIG" #define GNUPGNOPUBKEY "[GNUPG:] NO_PUBKEY" @@ -92,8 +98,16 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile, // Read a line. Sigh. while ((c = getc(pipein)) != EOF && c != '\n') { - if (bufferoff == buffersize) - buffer = (char *) realloc(buffer, buffersize *= 2); + if (bufferoff == buffersize) + { + char* newBuffer = (char *) realloc(buffer, buffersize *= 2); + if (newBuffer == NULL) + { + free(buffer); + return "Couldn't allocate a buffer big enough for reading"; + } + buffer = newBuffer; + } *(buffer+bufferoff) = c; bufferoff++; } diff --git a/methods/gzip.cc b/methods/gzip.cc index fc4e1ecfd..6ab6548ef 100644 --- a/methods/gzip.cc +++ b/methods/gzip.cc @@ -9,6 +9,8 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/fileutl.h> #include <apt-pkg/error.h> #include <apt-pkg/acquire-method.h> @@ -39,14 +41,14 @@ class GzipMethod : public pkgAcqMethod bool GzipMethod::Fetch(FetchItem *Itm) { URI Get = Itm->Uri; - string Path = Get.Host + Get.Path; // To account for relative paths + std::string Path = Get.Host + Get.Path; // To account for relative paths FetchResult Res; Res.Filename = Itm->DestFile; URIStart(Res); // Open the source and destination files - FileFd From(Path,FileFd::ReadOnlyGzip); + FileFd From(Path,FileFd::ReadOnly, FileFd::Gzip); if(From.FileSize() == 0) return _error->Error(_("Empty files can't be valid archives")); @@ -62,7 +64,7 @@ bool GzipMethod::Fetch(FetchItem *Itm) while (1) { unsigned char Buffer[4*1024]; - unsigned long Count; + unsigned long long Count = 0; if (!From.Read(Buffer,sizeof(Buffer),&Count)) { diff --git a/methods/http.cc b/methods/http.cc index 65a0cbbb7..c62ca71d3 100644 --- a/methods/http.cc +++ b/methods/http.cc @@ -25,8 +25,11 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/fileutl.h> #include <apt-pkg/acquire-method.h> +#include <apt-pkg/configuration.h> #include <apt-pkg/error.h> #include <apt-pkg/hashes.h> #include <apt-pkg/netrc.h> @@ -39,10 +42,9 @@ #include <stdio.h> #include <errno.h> #include <string.h> +#include <climits> #include <iostream> #include <map> -#include <apti18n.h> - // Internet stuff #include <netdb.h> @@ -51,6 +53,8 @@ #include "connect.h" #include "rfc2553emu.h" #include "http.h" + +#include <apti18n.h> /*}}}*/ using namespace std; @@ -63,15 +67,15 @@ bool AllowRedirect = false; bool Debug = false; URI Proxy; -unsigned long CircleBuf::BwReadLimit=0; -unsigned long CircleBuf::BwTickReadData=0; +unsigned long long CircleBuf::BwReadLimit=0; +unsigned long long CircleBuf::BwTickReadData=0; struct timeval CircleBuf::BwReadTick={0,0}; const unsigned int CircleBuf::BW_HZ=10; // CircleBuf::CircleBuf - Circular input buffer /*{{{*/ // --------------------------------------------------------------------- /* */ -CircleBuf::CircleBuf(unsigned long Size) : Size(Size), Hash(0) +CircleBuf::CircleBuf(unsigned long long Size) : Size(Size), Hash(0) { Buf = new unsigned char[Size]; Reset(); @@ -87,7 +91,7 @@ void CircleBuf::Reset() InP = 0; OutP = 0; StrPos = 0; - MaxGet = (unsigned int)-1; + MaxGet = (unsigned long long)-1; OutQueue = string(); if (Hash != 0) { @@ -102,7 +106,7 @@ void CircleBuf::Reset() is non-blocking.. */ bool CircleBuf::Read(int Fd) { - unsigned long BwReadMax; + unsigned long long BwReadMax; while (1) { @@ -117,7 +121,7 @@ bool CircleBuf::Read(int Fd) struct timeval now; gettimeofday(&now,0); - unsigned long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 + + unsigned long long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 + now.tv_usec-CircleBuf::BwReadTick.tv_usec; if(d > 1000000/BW_HZ) { CircleBuf::BwReadTick = now; @@ -131,7 +135,7 @@ bool CircleBuf::Read(int Fd) } // Write the buffer segment - int Res; + ssize_t Res; if(CircleBuf::BwReadLimit) { Res = read(Fd,Buf + (InP%Size), BwReadMax > LeftRead() ? LeftRead() : BwReadMax); @@ -180,7 +184,7 @@ void CircleBuf::FillOut() return; // Write the buffer segment - unsigned long Sz = LeftRead(); + unsigned long long Sz = LeftRead(); if (OutQueue.length() - StrPos < Sz) Sz = OutQueue.length() - StrPos; memcpy(Buf + (InP%Size),OutQueue.c_str() + StrPos,Sz); @@ -214,7 +218,7 @@ bool CircleBuf::Write(int Fd) return true; // Write the buffer segment - int Res; + ssize_t Res; Res = write(Fd,Buf + (OutP%Size),LeftWrite()); if (Res == 0) @@ -240,7 +244,7 @@ bool CircleBuf::Write(int Fd) bool CircleBuf::WriteTillEl(string &Data,bool Single) { // We cheat and assume it is unneeded to have more than one buffer load - for (unsigned long I = OutP; I < InP; I++) + for (unsigned long long I = OutP; I < InP; I++) { if (Buf[I%Size] != '\n') continue; @@ -258,7 +262,7 @@ bool CircleBuf::WriteTillEl(string &Data,bool Single) Data = ""; while (OutP < I) { - unsigned long Sz = LeftWrite(); + unsigned long long Sz = LeftWrite(); if (Sz == 0) return false; if (I - OutP < Sz) @@ -286,6 +290,11 @@ void CircleBuf::Stats() clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/ } /*}}}*/ +CircleBuf::~CircleBuf() +{ + delete [] Buf; + delete Hash; +} // ServerState::ServerState - Constructor /*{{{*/ // --------------------------------------------------------------------- @@ -453,7 +462,7 @@ bool ServerState::RunData() return false; // See if we are done - unsigned long Len = strtol(Data.c_str(),0,16); + unsigned long long Len = strtoull(Data.c_str(),0,16); if (Len == 0) { In.Limit(-1); @@ -526,10 +535,6 @@ bool ServerState::HeaderLine(string Line) if (Line.empty() == true) return true; - // The http server might be trying to do something evil. - if (Line.length() >= MAXLEN) - return _error->Error(_("Got a single header line over %u chars"),MAXLEN); - string::size_type Pos = Line.find(' '); if (Pos == string::npos || Pos+1 > Line.length()) { @@ -553,7 +558,7 @@ bool ServerState::HeaderLine(string Line) // Evil servers return no version if (Line[4] == '/') { - int const elements = sscanf(Line.c_str(),"HTTP/%u.%u %u%[^\n]",&Major,&Minor,&Result,Code); + int const elements = sscanf(Line.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major,&Minor,&Result,Code); if (elements == 3) { Code[0] = '\0'; @@ -567,7 +572,7 @@ bool ServerState::HeaderLine(string Line) { Major = 0; Minor = 9; - if (sscanf(Line.c_str(),"HTTP %u%[^\n]",&Result,Code) != 2) + if (sscanf(Line.c_str(),"HTTP %3u%359[^\n]",&Result,Code) != 2) return _error->Error(_("The HTTP server sent an invalid reply header")); } @@ -577,7 +582,7 @@ bool ServerState::HeaderLine(string Line) Persistent = false; else { - if (Major == 1 && Minor <= 0) + if (Major == 1 && Minor == 0) Persistent = false; else Persistent = true; @@ -595,9 +600,10 @@ bool ServerState::HeaderLine(string Line) // The length is already set from the Content-Range header if (StartPos != 0) return true; - - if (sscanf(Val.c_str(),"%lu",&Size) != 1) - return _error->Error(_("The HTTP server sent an invalid Content-Length header")); + + Size = strtoull(Val.c_str(), NULL, 10); + if (Size == ULLONG_MAX) + return _error->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header")); return true; } @@ -611,9 +617,9 @@ bool ServerState::HeaderLine(string Line) { HaveContent = true; - if (sscanf(Val.c_str(),"bytes %lu-%*u/%lu",&StartPos,&Size) != 2) + if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&Size) != 2) return _error->Error(_("The HTTP server sent an invalid Content-Range header")); - if ((unsigned)StartPos > Size) + if ((unsigned long long)StartPos > Size) return _error->Error(_("This HTTP server has broken range support")); return true; } @@ -708,7 +714,19 @@ void HttpMethod::SendReq(FetchItem *Itm,CircleBuf &Out) } } - + // If we ask for uncompressed files servers might respond with content- + // negotation which lets us end up with compressed files we do not support, + // see 657029, 657560 and co, so if we have no extension on the request + // ask for text only. As a sidenote: If there is nothing to negotate servers + // seem to be nice and ignore it. + if (_config->FindB("Acquire::http::SendAccept", true) == true) + { + size_t const filepos = Itm->Uri.find_last_of('/'); + string const file = Itm->Uri.substr(filepos + 1); + if (flExtension(file) == file) + strcat(Buf,"Accept: text/*\r\n"); + } + string Req = Buf; // Check for a partial file @@ -716,7 +734,7 @@ void HttpMethod::SendReq(FetchItem *Itm,CircleBuf &Out) if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) { // In this case we send an if-range query with a range header - sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",(long)SBuf.st_size - 1, + sprintf(Buf,"Range: bytes=%lli-\r\nIf-Range: %s\r\n",(long long)SBuf.st_size - 1, TimeRFC1123(SBuf.st_mtime).c_str()); Req += Buf; } @@ -740,7 +758,7 @@ void HttpMethod::SendReq(FetchItem *Itm,CircleBuf &Out) Base64Encode(Uri.User + ":" + Uri.Password) + "\r\n"; } Req += "User-Agent: " + _config->Find("Acquire::http::User-Agent", - "Debian APT-HTTP/1.3 ("VERSION")") + "\r\n\r\n"; + "Debian APT-HTTP/1.3 ("PACKAGE_VERSION")") + "\r\n\r\n"; if (Debug == true) cerr << Req << endl; @@ -999,31 +1017,21 @@ HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv) FailFile.c_str(); // Make sure we dont do a malloc in the signal handler FailFd = File->Fd(); FailTime = Srv->Date; - - // Set the expected size - if (Srv->StartPos >= 0) - { - Res.ResumePoint = Srv->StartPos; - if (ftruncate(File->Fd(),Srv->StartPos) < 0) - _error->Errno("ftruncate", _("Failed to truncate file")); - } - - // Set the start point - lseek(File->Fd(),0,SEEK_END); delete Srv->In.Hash; Srv->In.Hash = new Hashes; - - // Fill the Hash if the file is non-empty (resume) - if (Srv->StartPos > 0) + + // Set the expected size and read file for the hashes + if (Srv->StartPos >= 0) { - lseek(File->Fd(),0,SEEK_SET); - if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false) + Res.ResumePoint = Srv->StartPos; + File->Truncate(Srv->StartPos); + + if (Srv->In.Hash->AddFD(*File,Srv->StartPos) == false) { _error->Errno("read",_("Problem hashing file")); return ERROR_NOT_FROM_SERVER; } - lseek(File->Fd(),0,SEEK_END); } SetNonBlock(File->Fd(),true); @@ -1319,7 +1327,7 @@ int HttpMethod::Loop() after the same URI is seen twice in a queue item. */ StringVector &R = Redirected[Queue->DestFile]; bool StopRedirects = false; - if (R.size() == 0) + if (R.empty() == true) R.push_back(Queue->Uri); else if (R[0] == "STOP" || R.size() > 10) StopRedirects = true; diff --git a/methods/http.h b/methods/http.h index 6302807c5..7a3ccda54 100644 --- a/methods/http.h +++ b/methods/http.h @@ -11,41 +11,42 @@ #ifndef APT_HTTP_H #define APT_HTTP_H -#define MAXLEN 360 +#include <apt-pkg/strutl.h> -#include <apt-pkg/hashes.h> +#include <string> using std::cout; using std::endl; class HttpMethod; +class Hashes; class CircleBuf { unsigned char *Buf; - unsigned long Size; - unsigned long InP; - unsigned long OutP; - string OutQueue; - unsigned long StrPos; - unsigned long MaxGet; + unsigned long long Size; + unsigned long long InP; + unsigned long long OutP; + std::string OutQueue; + unsigned long long StrPos; + unsigned long long MaxGet; struct timeval Start; - static unsigned long BwReadLimit; - static unsigned long BwTickReadData; + static unsigned long long BwReadLimit; + static unsigned long long BwTickReadData; static struct timeval BwReadTick; static const unsigned int BW_HZ; - unsigned long LeftRead() const + unsigned long long LeftRead() const { - unsigned long Sz = Size - (InP - OutP); + unsigned long long Sz = Size - (InP - OutP); if (Sz > Size - (InP%Size)) Sz = Size - (InP%Size); return Sz; } - unsigned long LeftWrite() const + unsigned long long LeftWrite() const { - unsigned long Sz = InP - OutP; + unsigned long long Sz = InP - OutP; if (InP > MaxGet) Sz = MaxGet - OutP; if (Sz > Size - (OutP%Size)) @@ -60,14 +61,14 @@ class CircleBuf // Read data in bool Read(int Fd); - bool Read(string Data); + bool Read(std::string Data); // Write data out bool Write(int Fd); - bool WriteTillEl(string &Data,bool Single = false); + bool WriteTillEl(std::string &Data,bool Single = false); // Control the write limit - void Limit(long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;} + void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;} bool IsLimit() const {return MaxGet == OutP;}; void Print() const {cout << MaxGet << ',' << OutP << endl;}; @@ -79,8 +80,8 @@ class CircleBuf void Reset(); void Stats(); - CircleBuf(unsigned long Size); - ~CircleBuf() {delete [] Buf; delete Hash;}; + CircleBuf(unsigned long long Size); + ~CircleBuf(); }; struct ServerState @@ -89,17 +90,17 @@ struct ServerState unsigned int Major; unsigned int Minor; unsigned int Result; - char Code[MAXLEN]; + char Code[360]; // These are some statistics from the last parsed header lines - unsigned long Size; - signed long StartPos; + unsigned long long Size; + signed long long StartPos; time_t Date; bool HaveContent; enum {Chunked,Stream,Closes} Encoding; enum {Header, Data} State; bool Persistent; - string Location; + std::string Location; // This is a Persistent attribute of the server itself. bool Pipeline; @@ -112,11 +113,12 @@ struct ServerState int ServerFd; URI ServerName; - bool HeaderLine(string Line); + bool HeaderLine(std::string Line); bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;}; - void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0; - Encoding = Closes; time(&Date); ServerFd = -1; - Pipeline = true;}; + void Reset() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; Size = 0; + StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false; + State = Header; Persistent = false; ServerFd = -1; + Pipeline = true;}; /** \brief Result of the header acquire */ enum RunHeadersResult { @@ -167,10 +169,10 @@ class HttpMethod : public pkgAcqMethod /** \brief Try to AutoDetect the proxy */ bool AutoDetectProxy(); - virtual bool Configuration(string Message); + virtual bool Configuration(std::string Message); // In the event of a fatal signal this file will be closed and timestamped. - static string FailFile; + static std::string FailFile; static int FailFd; static time_t FailTime; static void SigTerm(int); @@ -178,8 +180,8 @@ class HttpMethod : public pkgAcqMethod protected: virtual bool Fetch(FetchItem *); - string NextURI; - string AutoDetectProxyCmd; + std::string NextURI; + std::string AutoDetectProxyCmd; public: friend struct ServerState; diff --git a/methods/http_main.cc b/methods/http_main.cc index 7815c2fc1..2ca91bfc9 100644 --- a/methods/http_main.cc +++ b/methods/http_main.cc @@ -1,3 +1,5 @@ +#include <config.h> + #include <apt-pkg/fileutl.h> #include <apt-pkg/acquire-method.h> #include <signal.h> diff --git a/methods/https.cc b/methods/https.cc index fc649d6c2..fac7ba790 100644 --- a/methods/https.cc +++ b/methods/https.cc @@ -10,11 +10,14 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + #include <apt-pkg/fileutl.h> #include <apt-pkg/acquire-method.h> #include <apt-pkg/error.h> #include <apt-pkg/hashes.h> #include <apt-pkg/netrc.h> +#include <apt-pkg/configuration.h> #include <sys/stat.h> #include <sys/time.h> @@ -25,12 +28,11 @@ #include <errno.h> #include <string.h> #include <iostream> -#include <apti18n.h> #include <sstream> #include "config.h" #include "https.h" - +#include <apti18n.h> /*}}}*/ using namespace std; @@ -51,7 +53,7 @@ HttpsMethod::progress_callback(void *clientp, double dltotal, double dlnow, { HttpsMethod *me = (HttpsMethod *)clientp; if(dltotal > 0 && me->Res.Size == 0) { - me->Res.Size = (unsigned long)dltotal; + me->Res.Size = (unsigned long long)dltotal; me->URIStart(me->Res); } return 0; @@ -98,7 +100,6 @@ void HttpsMethod::SetupProxy() /*{{{*/ depth. */ bool HttpsMethod::Fetch(FetchItem *Itm) { - stringstream ss; struct stat SBuf; struct curl_slist *headers=NULL; char curl_errorstr[CURL_ERROR_SIZE]; @@ -197,6 +198,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm) if (_config->FindB("Acquire::https::No-Store", _config->FindB("Acquire::http::No-Store",false)) == true) headers = curl_slist_append(headers,"Cache-Control: no-store"); + stringstream ss; ioprintf(ss, "Cache-Control: max-age=%u", _config->FindI("Acquire::https::Max-Age", _config->FindI("Acquire::http::Max-Age",0))); headers = curl_slist_append(headers, ss.str().c_str()); @@ -217,7 +219,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm) curl_easy_setopt(curl, CURLOPT_USERAGENT, _config->Find("Acquire::https::User-Agent", _config->Find("Acquire::http::User-Agent", - "Debian APT-CURL/1.0 ("VERSION")").c_str()).c_str()); + "Debian APT-CURL/1.0 ("PACKAGE_VERSION")").c_str()).c_str()); // set timeout int const timeout = _config->FindI("Acquire::https::Timeout", @@ -240,15 +242,28 @@ bool HttpsMethod::Fetch(FetchItem *Itm) // error handling curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr); + // If we ask for uncompressed files servers might respond with content- + // negotation which lets us end up with compressed files we do not support, + // see 657029, 657560 and co, so if we have no extension on the request + // ask for text only. As a sidenote: If there is nothing to negotate servers + // seem to be nice and ignore it. + if (_config->FindB("Acquire::https::SendAccept", _config->FindB("Acquire::http::SendAccept", true)) == true) + { + size_t const filepos = Itm->Uri.find_last_of('/'); + string const file = Itm->Uri.substr(filepos + 1); + if (flExtension(file) == file) + headers = curl_slist_append(headers, "Accept: text/*"); + } + // if we have the file send an if-range query with a range header if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) { char Buf[1000]; - sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n", - (long)SBuf.st_size - 1, - TimeRFC1123(SBuf.st_mtime).c_str()); + sprintf(Buf, "Range: bytes=%li-", (long) SBuf.st_size - 1); + headers = curl_slist_append(headers, Buf); + sprintf(Buf, "If-Range: %s", TimeRFC1123(SBuf.st_mtime).c_str()); headers = curl_slist_append(headers, Buf); - } + } else if(Itm->LastModified > 0) { curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); @@ -270,14 +285,17 @@ bool HttpsMethod::Fetch(FetchItem *Itm) long curl_servdate; curl_easy_getinfo(curl, CURLINFO_FILETIME, &curl_servdate); + File->Close(); + // cleanup if(success != 0) { _error->Error("%s", curl_errorstr); + // unlink, no need keep 401/404 page content in partial/ + unlink(File->Name().c_str()); Fail(); return true; } - File->Close(); // Timestamp struct utimbuf UBuf; @@ -309,7 +327,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm) // take hashes Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); // keep apt updated diff --git a/methods/https.h b/methods/https.h index 3f0c416b6..b1961a870 100644 --- a/methods/https.h +++ b/methods/https.h @@ -8,10 +8,8 @@ ##################################################################### */ /*}}}*/ -#ifndef APT_HTTP_H -#define APT_HTTP_H - -#define MAXLEN 360 +#ifndef APT_HTTPS_H +#define APT_HTTPS_H #include <iostream> #include <curl/curl.h> @@ -20,7 +18,7 @@ using std::cout; using std::endl; class HttpsMethod; - +class FileFd; class HttpsMethod : public pkgAcqMethod { @@ -45,6 +43,7 @@ class HttpsMethod : public pkgAcqMethod }; }; +#include <apt-pkg/strutl.h> URI Proxy; #endif diff --git a/methods/makefile b/methods/makefile index 6ba51058e..6fe95f29a 100644 --- a/methods/makefile +++ b/methods/makefile @@ -7,7 +7,7 @@ include ../buildlib/defaults.mak BIN := $(BIN)/methods include ../buildlib/libversion.mak -APT_DOMAIN := libapt-pkg$(LIBAPTPKG_MAJOR) +APT_DOMAIN := apt # The file method PROGRAM=file @@ -67,7 +67,7 @@ include $(PROGRAM_H) # The rred method PROGRAM=rred -SLIBS = -lapt-pkg -lz $(SOCKETLIBS) $(INTLLIBS) +SLIBS = -lapt-pkg $(SOCKETLIBS) $(INTLLIBS) LIB_MAKES = apt-pkg/makefile SOURCE = rred.cc include $(PROGRAM_H) diff --git a/methods/mirror.cc b/methods/mirror.cc index cb24a06cf..eb6d97425 100644 --- a/methods/mirror.cc +++ b/methods/mirror.cc @@ -8,6 +8,9 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#include <config.h> + +#include <apt-pkg/aptconfiguration.h> #include <apt-pkg/fileutl.h> #include <apt-pkg/acquire-method.h> #include <apt-pkg/acquire-item.h> @@ -15,7 +18,8 @@ #include <apt-pkg/error.h> #include <apt-pkg/hashes.h> #include <apt-pkg/sourcelist.h> - +#include <apt-pkg/configuration.h> +#include <apt-pkg/metaindex.h> #include <algorithm> #include <fstream> @@ -33,7 +37,7 @@ using namespace std; #include "mirror.h" #include "http.h" -#include "apti18n.h" +#include <apti18n.h> /*}}}*/ /* Done: @@ -120,9 +124,10 @@ bool MirrorMethod::Clean(string Dir) if (I == list.end()) unlink(Dir->d_name); }; - - chdir(StartDir.c_str()); + closedir(D); + if (chdir(StartDir.c_str()) != 0) + return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str()); return true; } @@ -134,9 +139,24 @@ bool MirrorMethod::DownloadMirrorFile(string mirror_uri_str) string fetch = BaseUri; fetch.replace(0,strlen("mirror://"),"http://"); +#if 0 // no need for this, the getArchitectures() will also include the main + // arch + // append main architecture + fetch += "?arch=" + _config->Find("Apt::Architecture"); +#endif + + // append all architectures + std::vector<std::string> vec = APT::Configuration::getArchitectures(); + for (std::vector<std::string>::const_iterator I = vec.begin(); + I != vec.end(); ++I) + if (I == vec.begin()) + fetch += "?arch" + (*I); + else + fetch += "&arch=" + (*I); + // append the dist as a query string if (Dist != "") - fetch += "?dist=" + Dist; + fetch += "&dist=" + Dist; if(Debug) clog << "MirrorMethod::DownloadMirrorFile(): '" << fetch << "'" diff --git a/methods/mirror.h b/methods/mirror.h index 97d18144a..81e531e21 100644 --- a/methods/mirror.h +++ b/methods/mirror.h @@ -11,8 +11,9 @@ #ifndef APT_MIRROR_H #define APT_MIRROR_H - #include <iostream> +#include <string> +#include <vector> using std::cout; using std::cerr; @@ -24,29 +25,29 @@ class MirrorMethod : public HttpMethod { FetchResult Res; // we simply transform between BaseUri and Mirror - string BaseUri; // the original mirror://... url - string Mirror; // the selected mirror uri (http://...) - vector<string> AllMirrors; // all available mirrors - string MirrorFile; // the file that contains the list of mirrors + std::string BaseUri; // the original mirror://... url + std::string Mirror; // the selected mirror uri (http://...) + std::vector<std::string> AllMirrors; // all available mirrors + std::string MirrorFile; // the file that contains the list of mirrors bool DownloadedMirrorFile; // already downloaded this session - string Dist; // the target distrubtion (e.g. sid, oneiric) + std::string Dist; // the target distrubtion (e.g. sid, oneiric) bool Debug; protected: - bool DownloadMirrorFile(string uri); - bool RandomizeMirrorFile(string file); - string GetMirrorFileName(string uri); + bool DownloadMirrorFile(std::string uri); + bool RandomizeMirrorFile(std::string file); + std::string GetMirrorFileName(std::string uri); bool InitMirrors(); bool TryNextMirror(); void CurrentQueueUriToMirror(); - bool Clean(string dir); + bool Clean(std::string dir); // we need to overwrite those to transform the url back - virtual void Fail(string Why, bool Transient = false); + virtual void Fail(std::string Why, bool Transient = false); virtual void URIStart(FetchResult &Res); virtual void URIDone(FetchResult &Res,FetchResult *Alt = 0); - virtual bool Configuration(string Message); + virtual bool Configuration(std::string Message); public: MirrorMethod(); diff --git a/methods/rfc2553emu.cc b/methods/rfc2553emu.cc index 66bc906e9..f00e85889 100644 --- a/methods/rfc2553emu.cc +++ b/methods/rfc2553emu.cc @@ -14,12 +14,14 @@ ##################################################################### */ /*}}}*/ -#include "rfc2553emu.h" +#include <config.h> + #include <stdlib.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <stdio.h> +#include "rfc2553emu.h" #ifndef HAVE_GETADDRINFO // getaddrinfo - Resolve a hostname /*{{{*/ diff --git a/methods/rred.cc b/methods/rred.cc index 6c55880ca..38554464d 100644 --- a/methods/rred.cc +++ b/methods/rred.cc @@ -1,10 +1,13 @@ // Includes /*{{{*/ +#include <config.h> + #include <apt-pkg/fileutl.h> #include <apt-pkg/mmap.h> #include <apt-pkg/error.h> #include <apt-pkg/acquire-method.h> #include <apt-pkg/strutl.h> #include <apt-pkg/hashes.h> +#include <apt-pkg/configuration.h> #include <sys/stat.h> #include <sys/uio.h> @@ -34,13 +37,10 @@ class RredMethod : public pkgAcqMethod { // return values enum State {ED_OK, ED_ORDERING, ED_PARSER, ED_FAILURE, MMAP_FAILED}; - State applyFile(gzFile &ed_cmds, FILE *in_file, FILE *out_file, + State applyFile(FileFd &ed_cmds, FileFd &in_file, FileFd &out_file, unsigned long &line, char *buffer, Hashes *hash) const; - void ignoreLineInFile(FILE *fin, char *buffer) const; - void ignoreLineInFile(gzFile &fin, char *buffer) const; - void copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines, - Hashes *hash, char *buffer) const; - void copyLinesFromFileToFile(gzFile &fin, FILE *fout, unsigned int lines, + void ignoreLineInFile(FileFd &fin, char *buffer) const; + void copyLinesFromFileToFile(FileFd &fin, FileFd &fout, unsigned int lines, Hashes *hash, char *buffer) const; State patchFile(FileFd &Patch, FileFd &From, FileFd &out_file, Hashes *hash) const; @@ -69,10 +69,10 @@ public: * \param hash the created file for correctness * \return the success State of the ed command executor */ -RredMethod::State RredMethod::applyFile(gzFile &ed_cmds, FILE *in_file, FILE *out_file, +RredMethod::State RredMethod::applyFile(FileFd &ed_cmds, FileFd &in_file, FileFd &out_file, unsigned long &line, char *buffer, Hashes *hash) const { // get the current command and parse it - if (gzgets(ed_cmds, buffer, BUF_SIZE) == NULL) { + if (ed_cmds.ReadLine(buffer, BUF_SIZE) == NULL) { if (Debug == true) std::clog << "rred: encounter end of file - we can start patching now." << std::endl; line = 0; @@ -127,7 +127,7 @@ RredMethod::State RredMethod::applyFile(gzFile &ed_cmds, FILE *in_file, FILE *ou unsigned char mode = *idx; // save the current position - unsigned const long pos = gztell(ed_cmds); + unsigned const long long pos = ed_cmds.Tell(); // if this is add or change then go to the next full stop unsigned int data_length = 0; @@ -161,7 +161,7 @@ RredMethod::State RredMethod::applyFile(gzFile &ed_cmds, FILE *in_file, FILE *ou // include data from ed script if (mode == MODE_CHANGED || mode == MODE_ADDED) { - gzseek(ed_cmds, pos, SEEK_SET); + ed_cmds.Seek(pos); copyLinesFromFileToFile(ed_cmds, out_file, data_length, hash, buffer); } @@ -175,44 +175,24 @@ RredMethod::State RredMethod::applyFile(gzFile &ed_cmds, FILE *in_file, FILE *ou return ED_OK; } /*}}}*/ -void RredMethod::copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines,/*{{{*/ +void RredMethod::copyLinesFromFileToFile(FileFd &fin, FileFd &fout, unsigned int lines,/*{{{*/ Hashes *hash, char *buffer) const { while (0 < lines--) { do { - fgets(buffer, BUF_SIZE, fin); - size_t const written = fwrite(buffer, 1, strlen(buffer), fout); - hash->Add((unsigned char*)buffer, written); + fin.ReadLine(buffer, BUF_SIZE); + unsigned long long const towrite = strlen(buffer); + fout.Write(buffer, towrite); + hash->Add((unsigned char*)buffer, towrite); } while (strlen(buffer) == (BUF_SIZE - 1) && buffer[BUF_SIZE - 2] != '\n'); } } /*}}}*/ -void RredMethod::copyLinesFromFileToFile(gzFile &fin, FILE *fout, unsigned int lines,/*{{{*/ - Hashes *hash, char *buffer) const { - while (0 < lines--) { - do { - gzgets(fin, buffer, BUF_SIZE); - size_t const written = fwrite(buffer, 1, strlen(buffer), fout); - hash->Add((unsigned char*)buffer, written); - } while (strlen(buffer) == (BUF_SIZE - 1) && - buffer[BUF_SIZE - 2] != '\n'); - } -} - /*}}}*/ -void RredMethod::ignoreLineInFile(FILE *fin, char *buffer) const { /*{{{*/ - fgets(buffer, BUF_SIZE, fin); +void RredMethod::ignoreLineInFile(FileFd &fin, char *buffer) const { /*{{{*/ + fin.ReadLine(buffer, BUF_SIZE); while (strlen(buffer) == (BUF_SIZE - 1) && buffer[BUF_SIZE - 2] != '\n') { - fgets(buffer, BUF_SIZE, fin); - buffer[0] = ' '; - } -} - /*}}}*/ -void RredMethod::ignoreLineInFile(gzFile &fin, char *buffer) const { /*{{{*/ - gzgets(fin, buffer, BUF_SIZE); - while (strlen(buffer) == (BUF_SIZE - 1) && - buffer[BUF_SIZE - 2] != '\n') { - gzgets(fin, buffer, BUF_SIZE); + fin.ReadLine(buffer, BUF_SIZE); buffer[0] = ' '; } } @@ -220,21 +200,18 @@ void RredMethod::ignoreLineInFile(gzFile &fin, char *buffer) const { /*{{{*/ RredMethod::State RredMethod::patchFile(FileFd &Patch, FileFd &From, /*{{{*/ FileFd &out_file, Hashes *hash) const { char buffer[BUF_SIZE]; - FILE* fFrom = fdopen(From.Fd(), "r"); - gzFile fPatch = Patch.gzFd(); - FILE* fTo = fdopen(out_file.Fd(), "w"); /* we do a tail recursion to read the commands in the right order */ unsigned long line = -1; // assign highest possible value - State const result = applyFile(fPatch, fFrom, fTo, line, buffer, hash); + State const result = applyFile(Patch, From, out_file, line, buffer, hash); /* read the rest from infile */ if (result == ED_OK) { - while (fgets(buffer, BUF_SIZE, fFrom) != NULL) { - size_t const written = fwrite(buffer, 1, strlen(buffer), fTo); - hash->Add((unsigned char*)buffer, written); + while (From.ReadLine(buffer, BUF_SIZE) != NULL) { + unsigned long long const towrite = strlen(buffer); + out_file.Write(buffer, towrite); + hash->Add((unsigned char*)buffer, towrite); } - fflush(fTo); } return result; } @@ -250,28 +227,32 @@ struct EdCommand { char type; }; #define IOV_COUNT 1024 /* Don't really want IOV_MAX since it can be arbitrarily large */ +ssize_t retry_writev(int fd, const struct iovec *iov, int iovcnt) { + ssize_t Res; + errno = 0; + ssize_t i = 0; + do { + Res = writev(fd, iov + i, iovcnt); + if (Res < 0 && errno == EINTR) + continue; + if (Res < 0) + return _error->Errno("writev",_("Write error")); + iovcnt -= Res; + i += Res; + } while (Res > 0 && iovcnt > 0); + return i; +} #endif /*}}}*/ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/ FileFd &out_file, Hashes *hash) const { #ifdef _POSIX_MAPPED_FILES - MMap ed_cmds(MMap::ReadOnly); - if (Patch.gzFd() != NULL) { - unsigned long mapSize = Patch.Size(); - DynamicMMap* dyn = new DynamicMMap(0, mapSize, 0); - if (dyn->validData() == false) { - delete dyn; - return MMAP_FAILED; - } - dyn->AddSize(mapSize); - gzread(Patch.gzFd(), dyn->Data(), mapSize); - ed_cmds = *dyn; - } else - ed_cmds = MMap(Patch, MMap::ReadOnly); - + MMap ed_cmds(Patch, MMap::ReadOnly); MMap in_file(From, MMap::ReadOnly); - if (ed_cmds.Size() == 0 || in_file.Size() == 0) + unsigned long long const ed_size = ed_cmds.Size(); + unsigned long long const in_size = in_file.Size(); + if (ed_size == 0 || in_size == 0) return MMAP_FAILED; EdCommand* commands = 0; @@ -280,10 +261,10 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/ const char* begin = (char*) ed_cmds.Data(); const char* end = begin; - const char* ed_end = (char*) ed_cmds.Data() + ed_cmds.Size(); + const char* ed_end = (char*) ed_cmds.Data() + ed_size; const char* input = (char*) in_file.Data(); - const char* input_end = (char*) in_file.Data() + in_file.Size(); + const char* input_end = (char*) in_file.Data() + in_size; size_t i; @@ -367,7 +348,12 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/ } if(command_count == command_alloc) { command_alloc = (command_alloc + 64) * 3 / 2; - commands = (EdCommand*) realloc(commands, command_alloc * sizeof(EdCommand)); + EdCommand* newCommands = (EdCommand*) realloc(commands, command_alloc * sizeof(EdCommand)); + if (newCommands == NULL) { + free(commands); + return MMAP_FAILED; + } + commands = newCommands; } commands[command_count++] = cmd; } @@ -406,7 +392,7 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/ hash->Add((const unsigned char*) begin, input - begin); if(++iov_size == IOV_COUNT) { - writev(out_file.Fd(), iov, IOV_COUNT); + retry_writev(out_file.Fd(), iov, IOV_COUNT); iov_size = 0; } } @@ -431,7 +417,7 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/ iov[iov_size].iov_len); if(++iov_size == IOV_COUNT) { - writev(out_file.Fd(), iov, IOV_COUNT); + retry_writev(out_file.Fd(), iov, IOV_COUNT); iov_size = 0; } } @@ -446,15 +432,15 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/ } if(iov_size) { - writev(out_file.Fd(), iov, iov_size); + retry_writev(out_file.Fd(), iov, iov_size); iov_size = 0; } for(i = 0; i < iov_size; i += IOV_COUNT) { if(iov_size - i < IOV_COUNT) - writev(out_file.Fd(), iov + i, iov_size - i); + retry_writev(out_file.Fd(), iov + i, iov_size - i); else - writev(out_file.Fd(), iov + i, IOV_COUNT); + retry_writev(out_file.Fd(), iov + i, IOV_COUNT); } delete [] iov; @@ -470,7 +456,7 @@ bool RredMethod::Fetch(FetchItem *Itm) /*{{{*/ { Debug = _config->FindB("Debug::pkgAcquire::RRed", false); URI Get = Itm->Uri; - string Path = Get.Host + Get.Path; // To account for relative paths + std::string Path = Get.Host + Get.Path; // To account for relative paths FetchResult Res; Res.Filename = Itm->DestFile; @@ -486,7 +472,7 @@ bool RredMethod::Fetch(FetchItem *Itm) /*{{{*/ // Open the source and destination files (the d'tor of FileFd will do // the cleanup/closing of the fds) FileFd From(Path,FileFd::ReadOnly); - FileFd Patch(Path+".ed",FileFd::ReadOnlyGzip); + FileFd Patch(Path+".ed",FileFd::ReadOnly, FileFd::Gzip); FileFd To(Itm->DestFile,FileFd::WriteAtomic); To.EraseOnFailure(); if (_error->PendingError() == true) @@ -523,7 +509,7 @@ bool RredMethod::Fetch(FetchItem *Itm) /*{{{*/ and use the access time from the "old" file */ struct stat BufBase, BufPatch; if (stat(Path.c_str(),&BufBase) != 0 || - stat(string(Path+".ed").c_str(),&BufPatch) != 0) + stat(std::string(Path+".ed").c_str(),&BufPatch) != 0) return _error->Errno("stat",_("Failed to stat")); struct utimbuf TimeBuf; diff --git a/methods/rsh.cc b/methods/rsh.cc index 21f0d0a22..fb3782314 100644 --- a/methods/rsh.cc +++ b/methods/rsh.cc @@ -11,8 +11,12 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ -#include "rsh.h" +#include <config.h> + #include <apt-pkg/error.h> +#include <apt-pkg/fileutl.h> +#include <apt-pkg/hashes.h> +#include <apt-pkg/configuration.h> #include <sys/stat.h> #include <sys/time.h> @@ -22,6 +26,8 @@ #include <stdio.h> #include <errno.h> #include <stdarg.h> +#include "rsh.h" + #include <apti18n.h> /*}}}*/ @@ -29,14 +35,16 @@ const char *Prog; unsigned long TimeOut = 120; Configuration::Item const *RshOptions = 0; time_t RSHMethod::FailTime = 0; -string RSHMethod::FailFile; +std::string RSHMethod::FailFile; int RSHMethod::FailFd = -1; // RSHConn::RSHConn - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ RSHConn::RSHConn(URI Srv) : Len(0), WriteFd(-1), ReadFd(-1), - ServerName(Srv), Process(-1) {} + ServerName(Srv), Process(-1) { + Buffer[0] = '\0'; +} /*}}}*/ // RSHConn::RSHConn - Destructor /*{{{*/ // --------------------------------------------------------------------- @@ -82,7 +90,7 @@ bool RSHConn::Open() // RSHConn::Connect - Fire up rsh and connect /*{{{*/ // --------------------------------------------------------------------- /* */ -bool RSHConn::Connect(string Host, string User) +bool RSHConn::Connect(std::string Host, std::string User) { // Create the pipes int Pipes[4] = {-1,-1,-1,-1}; @@ -151,7 +159,7 @@ bool RSHConn::Connect(string Host, string User) // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/ // --------------------------------------------------------------------- /* */ -bool RSHConn::ReadLine(string &Text) +bool RSHConn::ReadLine(std::string &Text) { if (Process == -1 || ReadFd == -1) return false; @@ -171,7 +179,7 @@ bool RSHConn::ReadLine(string &Text) continue; I++; - Text = string(Buffer,I); + Text = std::string(Buffer,I); memmove(Buffer,Buffer+I,Len - I); Len -= I; return true; @@ -202,7 +210,7 @@ bool RSHConn::ReadLine(string &Text) // --------------------------------------------------------------------- /* The remote sync flag appends a || echo which will insert blank line once the command completes. */ -bool RSHConn::WriteMsg(string &Text,bool Sync,const char *Fmt,...) +bool RSHConn::WriteMsg(std::string &Text,bool Sync,const char *Fmt,...) { va_list args; va_start(args,Fmt); @@ -248,10 +256,10 @@ bool RSHConn::WriteMsg(string &Text,bool Sync,const char *Fmt,...) // --------------------------------------------------------------------- /* Right now for successfull transfer the file size must be known in advance. */ -bool RSHConn::Size(const char *Path,unsigned long &Size) +bool RSHConn::Size(const char *Path,unsigned long long &Size) { // Query the size - string Msg; + std::string Msg; Size = 0; if (WriteMsg(Msg,true,"find %s -follow -printf '%%s\\n'",Path) == false) @@ -260,7 +268,7 @@ bool RSHConn::Size(const char *Path,unsigned long &Size) // FIXME: Sense if the bad reply is due to a File Not Found. char *End; - Size = strtoul(Msg.c_str(),&End,10); + Size = strtoull(Msg.c_str(),&End,10); if (End == Msg.c_str()) return _error->Error(_("File not found")); return true; @@ -273,7 +281,7 @@ bool RSHConn::ModTime(const char *Path, time_t &Time) { Time = time(&Time); // Query the mod time - string Msg; + std::string Msg; if (WriteMsg(Msg,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path) == false) return false; @@ -285,8 +293,8 @@ bool RSHConn::ModTime(const char *Path, time_t &Time) // RSHConn::Get - Get a file /*{{{*/ // --------------------------------------------------------------------- /* */ -bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume, - Hashes &Hash,bool &Missing, unsigned long Size) +bool RSHConn::Get(const char *Path,FileFd &To,unsigned long long Resume, + Hashes &Hash,bool &Missing, unsigned long long Size) { Missing = false; @@ -299,19 +307,19 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume, return false; if (Resume != 0) { - if (Hash.AddFD(To.Fd(),Resume) == false) { + if (Hash.AddFD(To,Resume) == false) { _error->Errno("read",_("Problem hashing file")); return false; } } // FIXME: Detect file-not openable type errors. - string Jnk; + std::string Jnk; if (WriteMsg(Jnk,false,"dd if=%s bs=2048 skip=%u", Path, Resume / 2048) == false) return false; // Copy loop - unsigned int MyLen = Resume; + unsigned long long MyLen = Resume; unsigned char Buffer[4096]; while (MyLen < Size) { @@ -363,7 +371,7 @@ RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig) /*}}}*/ // RSHMethod::Configuration - Handle a configuration message /*{{{*/ // --------------------------------------------------------------------- -bool RSHMethod::Configuration(string Message) +bool RSHMethod::Configuration(std::string Message) { char ProgStr[100]; @@ -425,7 +433,7 @@ bool RSHMethod::Fetch(FetchItem *Itm) Status(_("Connecting to %s"), Get.Host.c_str()); // Get the files information - unsigned long Size; + unsigned long long Size; if (Server->Size(File,Size) == false || Server->ModTime(File,FailTime) == false) { @@ -446,7 +454,7 @@ bool RSHMethod::Fetch(FetchItem *Itm) // See if the file exists struct stat Buf; if (stat(Itm->DestFile.c_str(),&Buf) == 0) { - if (Size == (unsigned)Buf.st_size && FailTime == Buf.st_mtime) { + if (Size == (unsigned long long)Buf.st_size && FailTime == Buf.st_mtime) { Res.Size = Buf.st_size; Res.LastModified = Buf.st_mtime; Res.ResumePoint = Buf.st_size; @@ -455,7 +463,7 @@ bool RSHMethod::Fetch(FetchItem *Itm) } // Resume? - if (FailTime == Buf.st_mtime && Size > (unsigned)Buf.st_size) + if (FailTime == Buf.st_mtime && Size > (unsigned long long)Buf.st_size) Res.ResumePoint = Buf.st_size; } diff --git a/methods/rsh.h b/methods/rsh.h index 98ca6a88c..d7efa3f06 100644 --- a/methods/rsh.h +++ b/methods/rsh.h @@ -12,9 +12,9 @@ #include <string> #include <apt-pkg/strutl.h> -#include <apt-pkg/hashes.h> -#include <apt-pkg/acquire-method.h> -#include <apt-pkg/fileutl.h> + +class Hashes; +class FileFd; class RSHConn { @@ -25,15 +25,15 @@ class RSHConn URI ServerName; // Private helper functions - bool ReadLine(string &Text); + bool ReadLine(std::string &Text); public: pid_t Process; // Raw connection IO - bool WriteMsg(string &Text,bool Sync,const char *Fmt,...); - bool Connect(string Host, string User); + bool WriteMsg(std::string &Text,bool Sync,const char *Fmt,...); + bool Connect(std::string Host, std::string User); bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;}; // Connection control @@ -41,23 +41,25 @@ class RSHConn void Close(); // Query - bool Size(const char *Path,unsigned long &Size); + bool Size(const char *Path,unsigned long long &Size); bool ModTime(const char *Path, time_t &Time); - bool Get(const char *Path,FileFd &To,unsigned long Resume, - Hashes &Hash,bool &Missing, unsigned long Size); + bool Get(const char *Path,FileFd &To,unsigned long long Resume, + Hashes &Hash,bool &Missing, unsigned long long Size); RSHConn(URI Srv); ~RSHConn(); }; +#include <apt-pkg/acquire-method.h> + class RSHMethod : public pkgAcqMethod { virtual bool Fetch(FetchItem *Itm); - virtual bool Configuration(string Message); + virtual bool Configuration(std::string Message); RSHConn *Server; - static string FailFile; + static std::string FailFile; static int FailFd; static time_t FailTime; static void SigTerm(int); |