methods/rsh.cc: replace strcat with std::string
[ntk/apt.git] / methods / rsh.cc
index 5326e50..0e94916 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: rsh.cc,v 1.3 2001/03/06 07:15:29 jgg 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
 
    ##################################################################### */
                                                                        /*}}}*/
-// Iclude Files                                                                /*{{{*/
-#include "rsh.h"
-#include <apt-pkg/error.h>
+// Include Files                                                       /*{{{*/
+#include <config.h>
 
+#include <apt-pkg/error.h>
+#include <apt-pkg/fileutl.h>
+#include <apt-pkg/hashes.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/acquire-method.h>
+#include <apt-pkg/strutl.h>
+
+#include <stdlib.h>
+#include <string.h>
 #include <sys/stat.h>
 #include <sys/time.h>
-#include <utime.h>
 #include <unistd.h>
 #include <signal.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                                       /*{{{*/
 // ---------------------------------------------------------------------
@@ -80,13 +93,13 @@ 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};
    if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
    {
-      _error->Errno("pipe","Failed to create IPC pipe to subprocess");
+      _error->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
       for (int I = 0; I != 4; I++)
         close(Pipes[I]);
       return false;
@@ -99,8 +112,8 @@ bool RSHConn::Connect(string Host, string User)
    // The child
    if (Process == 0)
    {
-      const char *Args[6];
-      int i = 0;
+      const char *Args[400];
+      unsigned int i = 0;
 
       dup2(Pipes[1],STDOUT_FILENO);
       dup2(Pipes[2],STDIN_FILENO);
@@ -109,6 +122,20 @@ bool RSHConn::Connect(string Host, string User)
       // 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)
+      {
+         Opts = Opts->Child;
+        for (; Opts != 0; Opts = Opts->Next)
+         {
+            if (Opts->Value.empty() == true)
+               continue;
+            Args[i++] = Opts->Value.c_str();
+         }
+      }
+
       if (User.empty() == false) {
          Args[i++] = "-l";
         Args[i++] = User.c_str();
@@ -135,7 +162,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;
@@ -155,7 +182,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;
@@ -165,41 +192,46 @@ bool RSHConn::ReadLine(string &Text)
       if (WaitFd(ReadFd,false,TimeOut) == false)
       {
          Close();
-         return _error->Error("Connection timeout");
+         return _error->Error(_("Connection timeout"));
       }
 
       // Suck it back
       int Res = read(ReadFd,Buffer + Len,sizeof(Buffer) - Len);
       if (Res <= 0)
       {
-         _error->Errno("read","Read error");
+         _error->Errno("read",_("Read error"));
          Close();
          return false;
       }
       Len += Res;
    }
 
-   return _error->Error("A response overflowed the buffer.");
+   return _error->Error(_("A response overflowed the buffer."));
 }
                                                                        /*}}}*/
 // RSHConn::WriteMsg - Send a message with optional remote sync.       /*{{{*/
 // ---------------------------------------------------------------------
 /* 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);
 
-   // sprintf the description
-   char S[512];
-   vsnprintf(S,sizeof(S) - 4,Fmt,args);
+   // sprintf into a buffer
+   char Tmp[1024];
+   vsnprintf(Tmp,sizeof(Tmp),Fmt,args);
+   va_end(args);
+
+   // concat to create the real msg
+   std::string Msg;
    if (Sync == true)
-      strcat(S," 2> /dev/null || echo\n");
+      Msg = std::string(Tmp) + " 2> /dev/null || echo\n";
    else
-      strcat(S," 2> /dev/null\n");
+      Msg = std::string(Tmp) + " 2> /dev/null\n";
 
    // Send it off
+   const char *S = Msg.c_str();
    unsigned long Len = strlen(S);
    unsigned long Start = 0;
    while (Len != 0)
@@ -208,13 +240,13 @@ bool RSHConn::WriteMsg(string &Text,bool Sync,const char *Fmt,...)
       {
         
         Close();
-        return _error->Error("Connection timeout");
+        return _error->Error(_("Connection timeout"));
       }      
       
       int Res = write(WriteFd,S + Start,Len);
       if (Res <= 0)
       {
-         _error->Errno("write","Write Error");
+         _error->Errno("write",_("Write error"));
          Close();
          return false;
       }
@@ -230,12 +262,12 @@ bool RSHConn::WriteMsg(string &Text,bool Sync,const char *Fmt,...)
                                                                        /*}}}*/
 // RSHConn::Size - Return the size of the file                         /*{{{*/
 // ---------------------------------------------------------------------
-/* Right now for successfull transfer the file size must be known in 
+/* Right now for successful 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)
@@ -244,9 +276,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;
 }
                                                                        /*}}}*/
@@ -257,21 +289,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;
 
@@ -284,19 +315,19 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume,
       return false;
 
    if (Resume != 0) {
-      if (Hash.AddFD(To.Fd(),Resume) == false) {
-        _error->Errno("read","Problem hashing file");
+      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)
    {
@@ -304,7 +335,7 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume,
       if (WaitFd(ReadFd,false,TimeOut) == false)
       {
          Close();
-         return _error->Error("Data socket timed out");
+         return _error->Error(_("Data socket timed out"));
       }
 
       // Read the data..
@@ -312,7 +343,7 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume,
       if (Res == 0)
       {
         Close();
-        return _error->Error("Connection closed prematurely");
+        return _error->Error(_("Connection closed prematurely"));
       }
       
       if (Res < 0)
@@ -338,28 +369,46 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long Resume,
 // RSHMethod::RSHMethod - Constructor                                  /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-RSHMethod::RSHMethod() : pkgAcqMethod("1.0")
+RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig)
 {
    signal(SIGTERM,SigTerm);
    signal(SIGINT,SigTerm);
    Server = 0;
    FailFd = -1;
-};
+}
+                                                                       /*}}}*/
+// RSHMethod::Configuration - Handle a configuration message           /*{{{*/
+// ---------------------------------------------------------------------
+bool RSHMethod::Configuration(std::string Message)
+{
+   char ProgStr[100];
+  
+   if (pkgAcqMethod::Configuration(Message) == false)
+      return false;
+
+   snprintf(ProgStr, sizeof ProgStr, "Acquire::%s::Timeout", Prog);
+   TimeOut = _config->FindI(ProgStr,TimeOut);
+   snprintf(ProgStr, sizeof ProgStr, "Acquire::%s::Options", Prog);
+   RshOptions = _config->Tree(ProgStr);
+
+   return true;
+}
                                                                        /*}}}*/
 // RSHMethod::SigTerm - Clean up and timestamp the files on exit       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-void RSHMethod::SigTerm(int sig)
+void RSHMethod::SigTerm(int)
 {
    if (FailFd == -1)
       _exit(100);
-   close(FailFd);
 
-   // Timestamp
-   struct utimbuf UBuf;
-   UBuf.actime = FailTime;
-   UBuf.modtime = FailTime;
-   utime(FailFile.c_str(),&UBuf);
+   // Transfer the modification times
+   struct timeval times[2];
+   times[0].tv_sec = FailTime;
+   times[1].tv_sec = FailTime;
+   times[0].tv_usec = times[1].tv_usec = 0;
+   utimes(FailFile.c_str(), times);
+   close(FailFd);
 
    _exit(100);
 }
@@ -390,15 +439,15 @@ bool RSHMethod::Fetch(FetchItem *Itm)
 
    // We say this mainly because the pause here is for the
    // ssh connection that is still going
-   Status("Connecting to %s", Get.Host.c_str());
+   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;
@@ -414,7 +463,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;
@@ -423,7 +472,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;
    }
 
@@ -446,10 +495,11 @@ bool RSHMethod::Fetch(FetchItem *Itm)
         Fd.Close();
 
         // Timestamp
-        struct utimbuf UBuf;
-        UBuf.actime = FailTime;
-        UBuf.modtime = FailTime;
-        utime(FailFile.c_str(),&UBuf);
+        struct timeval times[2];
+        times[0].tv_sec = FailTime;
+        times[1].tv_sec = FailTime;
+        times[0].tv_usec = times[1].tv_usec = 0;
+        utimes(FailFile.c_str(), times);
 
         // If the file is missing we hard fail otherwise transient fail
         if (Missing == true)
@@ -459,17 +509,16 @@ bool RSHMethod::Fetch(FetchItem *Itm)
       }
 
       Res.Size = Fd.Size();
+      struct timeval times[2];
+      times[0].tv_sec = FailTime;
+      times[1].tv_sec = FailTime;
+      times[0].tv_usec = times[1].tv_usec = 0;
+      utimes(Fd.Name().c_str(), times);
+      FailFd = -1;
    }
 
    Res.LastModified = FailTime;
-   Res.MD5Sum = Hash.MD5.Result();
-
-   // Timestamp
-   struct utimbuf UBuf;
-   UBuf.actime = FailTime;
-   UBuf.modtime = FailTime;
-   utime(Queue->DestFile.c_str(),&UBuf);
-   FailFd = -1;
+   Res.TakeHashes(Hash);
 
    URIDone(Res);
 
@@ -477,8 +526,10 @@ bool RSHMethod::Fetch(FetchItem *Itm)
 }
                                                                        /*}}}*/
 
-int main(int argc, const char *argv[])
+int main(int, const char *argv[])
 {
+   setlocale(LC_ALL, "");
+
    RSHMethod Mth;
    Prog = strrchr(argv[0],'/');
    Prog++;