]>
git.saurik.com Git - apt.git/blob - methods/rsh.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: rsh.cc,v 1.6.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 RSH method - Transfer files via rsh compatible program
8 Written by Ben Collins <bcollins@debian.org>, Copyright (c) 2000
9 Licensed under the GNU General Public License v2 [no exception clauses]
11 ##################################################################### */
13 // Include Files /*{{{*/
15 #include <apt-pkg/error.h>
29 unsigned long TimeOut
= 120;
30 Configuration::Item
const *RshOptions
= 0;
31 time_t RSHMethod::FailTime
= 0;
32 string
RSHMethod::FailFile
;
33 int RSHMethod::FailFd
= -1;
35 // RSHConn::RSHConn - Constructor /*{{{*/
36 // ---------------------------------------------------------------------
38 RSHConn::RSHConn(URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
39 ServerName(Srv
), Process(-1) {}
41 // RSHConn::RSHConn - Destructor /*{{{*/
42 // ---------------------------------------------------------------------
49 // RSHConn::Close - Forcibly terminate the connection /*{{{*/
50 // ---------------------------------------------------------------------
51 /* Often this is called when things have gone wrong to indicate that the
52 connection is no longer usable. */
61 ExecWait(Process
,"",true);
67 // RSHConn::Open - Connect to a host /*{{{*/
68 // ---------------------------------------------------------------------
72 // Use the already open connection if possible.
76 if (Connect(ServerName
.Host
,ServerName
.User
) == false)
82 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
83 // ---------------------------------------------------------------------
85 bool RSHConn::Connect(string Host
, string User
)
88 int Pipes
[4] = {-1,-1,-1,-1};
89 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
91 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
92 for (int I
= 0; I
!= 4; I
++)
96 for (int I
= 0; I
!= 4; I
++)
97 SetCloseExec(Pipes
[I
],true);
104 const char *Args
[400];
107 dup2(Pipes
[1],STDOUT_FILENO
);
108 dup2(Pipes
[2],STDIN_FILENO
);
110 // Probably should do
111 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
113 // Insert user-supplied command line options
114 Configuration::Item
const *Opts
= RshOptions
;
118 for (; Opts
!= 0; Opts
= Opts
->Next
)
120 if (Opts
->Value
.empty() == true)
122 Args
[i
++] = Opts
->Value
.c_str();
127 if (User
.empty() == false) {
129 Args
[i
++] = User
.c_str();
131 if (Host
.empty() == false) {
132 Args
[i
++] = Host
.c_str();
134 Args
[i
++] = "/bin/sh";
136 execvp(Args
[0],(char **)Args
);
142 SetNonBlock(Pipes
[0],true);
143 SetNonBlock(Pipes
[3],true);
150 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
151 // ---------------------------------------------------------------------
153 bool RSHConn::ReadLine(string
&Text
)
155 if (Process
== -1 || ReadFd
== -1)
159 while (Len
< sizeof(Buffer
))
161 // Scan the buffer for a new line
162 for (unsigned int I
= 0; I
!= Len
; I
++)
164 // Escape some special chars
169 if (Buffer
[I
] != '\n')
173 Text
= string(Buffer
,I
);
174 memmove(Buffer
,Buffer
+I
,Len
- I
);
179 // Wait for some data..
180 if (WaitFd(ReadFd
,false,TimeOut
) == false)
183 return _error
->Error(_("Connection timeout"));
187 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
190 _error
->Errno("read",_("Read error"));
197 return _error
->Error(_("A response overflowed the buffer."));
200 // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
201 // ---------------------------------------------------------------------
202 /* The remote sync flag appends a || echo which will insert blank line
203 once the command completes. */
204 bool RSHConn::WriteMsg(string
&Text
,bool Sync
,const char *Fmt
,...)
209 // sprintf the description
211 vsnprintf(S
,sizeof(S
) - 4,Fmt
,args
);
213 strcat(S
," 2> /dev/null || echo\n");
215 strcat(S
," 2> /dev/null\n");
218 unsigned long Len
= strlen(S
);
219 unsigned long Start
= 0;
222 if (WaitFd(WriteFd
,true,TimeOut
) == false)
226 return _error
->Error(_("Connection timeout"));
229 int Res
= write(WriteFd
,S
+ Start
,Len
);
232 _error
->Errno("write",_("Write error"));
242 return ReadLine(Text
);
246 // RSHConn::Size - Return the size of the file /*{{{*/
247 // ---------------------------------------------------------------------
248 /* Right now for successfull transfer the file size must be known in
250 bool RSHConn::Size(const char *Path
,unsigned long &Size
)
256 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
259 // FIXME: Sense if the bad reply is due to a File Not Found.
262 Size
= strtoul(Msg
.c_str(),&End
,10);
263 if (End
== Msg
.c_str())
264 return _error
->Error(_("File not found"));
268 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
269 // ---------------------------------------------------------------------
271 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
274 // Query the mod time
277 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
281 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
284 // RSHConn::Get - Get a file /*{{{*/
285 // ---------------------------------------------------------------------
287 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long Resume
,
288 Hashes
&Hash
,bool &Missing
, unsigned long Size
)
292 // Round to a 2048 byte block
293 Resume
= Resume
- (Resume
% 2048);
295 if (To
.Truncate(Resume
) == false)
297 if (To
.Seek(0) == false)
301 if (Hash
.AddFD(To
.Fd(),Resume
) == false) {
302 _error
->Errno("read",_("Problem hashing file"));
307 // FIXME: Detect file-not openable type errors.
309 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
313 unsigned int MyLen
= Resume
;
314 unsigned char Buffer
[4096];
317 // Wait for some data..
318 if (WaitFd(ReadFd
,false,TimeOut
) == false)
321 return _error
->Error(_("Data socket timed out"));
325 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
329 return _error
->Error(_("Connection closed prematurely"));
340 Hash
.Add(Buffer
,Res
);
341 if (To
.Write(Buffer
,Res
) == false)
352 // RSHMethod::RSHMethod - Constructor /*{{{*/
353 // ---------------------------------------------------------------------
355 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig
)
357 signal(SIGTERM
,SigTerm
);
358 signal(SIGINT
,SigTerm
);
363 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
364 // ---------------------------------------------------------------------
365 bool RSHMethod::Configuration(string Message
)
369 if (pkgAcqMethod::Configuration(Message
) == false)
372 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Timeout", Prog
);
373 TimeOut
= _config
->FindI(ProgStr
,TimeOut
);
374 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Options", Prog
);
375 RshOptions
= _config
->Tree(ProgStr
);
380 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
381 // ---------------------------------------------------------------------
383 void RSHMethod::SigTerm(int sig
)
391 UBuf
.actime
= FailTime
;
392 UBuf
.modtime
= FailTime
;
393 utime(FailFile
.c_str(),&UBuf
);
398 // RSHMethod::Fetch - Fetch a URI /*{{{*/
399 // ---------------------------------------------------------------------
401 bool RSHMethod::Fetch(FetchItem
*Itm
)
404 const char *File
= Get
.Path
.c_str();
406 Res
.Filename
= Itm
->DestFile
;
409 // Connect to the server
410 if (Server
== 0 || Server
->Comp(Get
) == false) {
412 Server
= new RSHConn(Get
);
415 // Could not connect is a transient error..
416 if (Server
->Open() == false) {
422 // We say this mainly because the pause here is for the
423 // ssh connection that is still going
424 Status(_("Connecting to %s"), Get
.Host
.c_str());
426 // Get the files information
428 if (Server
->Size(File
,Size
) == false ||
429 Server
->ModTime(File
,FailTime
) == false)
432 //_error->Error(_("File not found")); // Will be handled by Size
437 // See if it is an IMS hit
438 if (Itm
->LastModified
== FailTime
) {
445 // See if the file exists
447 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
448 if (Size
== (unsigned)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
449 Res
.Size
= Buf
.st_size
;
450 Res
.LastModified
= Buf
.st_mtime
;
451 Res
.ResumePoint
= Buf
.st_size
;
457 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned)Buf
.st_size
)
458 Res
.ResumePoint
= Buf
.st_size
;
464 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
465 if (_error
->PendingError() == true)
470 FailFile
= Itm
->DestFile
;
471 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
475 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
481 UBuf
.actime
= FailTime
;
482 UBuf
.modtime
= FailTime
;
483 utime(FailFile
.c_str(),&UBuf
);
485 // If the file is missing we hard fail otherwise transient fail
492 Res
.Size
= Fd
.Size();
495 Res
.LastModified
= FailTime
;
496 Res
.TakeHashes(Hash
);
500 UBuf
.actime
= FailTime
;
501 UBuf
.modtime
= FailTime
;
502 utime(Queue
->DestFile
.c_str(),&UBuf
);
511 int main(int argc
, const char *argv
[])
513 setlocale(LC_ALL
, "");
516 Prog
= strrchr(argv
[0],'/');