merged lp:~mvo/apt/add-missing-dsc-hashes
[ntk/apt.git] / methods / rsh.cc
index 6cd6182..fb37823 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: rsh.cc,v 1.6 2003/02/10 07:34:41 doogie Exp $
+// $Id: rsh.cc,v 1.6.2.1 2004/01/16 18:58:50 mdz Exp $
 /* ######################################################################
 
    RSH method - Transfer files via rsh compatible program
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#include <apti18n.h>
-#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>
 #include <stdio.h>
 #include <errno.h>
 #include <stdarg.h>
+#include "rsh.h"
+
+#include <apti18n.h>
                                                                        /*}}}*/
 
 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};
@@ -110,6 +118,8 @@ bool RSHConn::Connect(string Host, string User)
       // Probably should do
       // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
 
+      Args[i++] = Prog;
+
       // Insert user-supplied command line options
       Configuration::Item const *Opts = RshOptions;
       if (Opts != 0)
@@ -123,7 +133,6 @@ bool RSHConn::Connect(string Host, string User)
          }
       }
 
-      Args[i++] = Prog;
       if (User.empty() == false) {
          Args[i++] = "-l";
         Args[i++] = User.c_str();
@@ -150,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;
@@ -170,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;
@@ -201,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);
@@ -229,7 +238,7 @@ bool RSHConn::WriteMsg(string &Text,bool Sync,const char *Fmt,...)
       int Res = write(WriteFd,S + Start,Len);
       if (Res <= 0)
       {
-         _error->Errno("write",_("Write Error"));
+         _error->Errno("write",_("Write error"));
          Close();
          return false;
       }
@@ -247,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)
@@ -259,9 +268,9 @@ 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 _error->Error(_("File not found"));
    return true;
 }
                                                                        /*}}}*/
@@ -272,21 +281,20 @@ 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;
 
    // Parse it
-   StrToTime(Msg,Time);
-   return true;
+   return FTPMDTMStrToTime(Msg.c_str(), 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,12 +433,12 @@ 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)
    {
       //Fail(true);
-      //_error->Error(_("File Not Found")); // Will be handled by Size
+      //_error->Error(_("File not found")); // Will be handled by Size
       return false;
    }
    Res.Size = Size;
@@ -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;
    }
 
@@ -511,6 +519,8 @@ bool RSHMethod::Fetch(FetchItem *Itm)
 
 int main(int argc, const char *argv[])
 {
+   setlocale(LC_ALL, "");
+
    RSHMethod Mth;
    Prog = strrchr(argv[0],'/');
    Prog++;