]>
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 /*{{{*/
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/hashes.h>
19 #include <apt-pkg/configuration.h>
35 unsigned long TimeOut
= 120;
36 Configuration::Item
const *RshOptions
= 0;
37 time_t RSHMethod::FailTime
= 0;
38 std::string
RSHMethod::FailFile
;
39 int RSHMethod::FailFd
= -1;
41 // RSHConn::RSHConn - Constructor /*{{{*/
42 // ---------------------------------------------------------------------
44 RSHConn::RSHConn(URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
45 ServerName(Srv
), Process(-1) {
49 // RSHConn::RSHConn - Destructor /*{{{*/
50 // ---------------------------------------------------------------------
57 // RSHConn::Close - Forcibly terminate the connection /*{{{*/
58 // ---------------------------------------------------------------------
59 /* Often this is called when things have gone wrong to indicate that the
60 connection is no longer usable. */
69 ExecWait(Process
,"",true);
75 // RSHConn::Open - Connect to a host /*{{{*/
76 // ---------------------------------------------------------------------
80 // Use the already open connection if possible.
84 if (Connect(ServerName
.Host
,ServerName
.User
) == false)
90 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
91 // ---------------------------------------------------------------------
93 bool RSHConn::Connect(std::string Host
, std::string User
)
96 int Pipes
[4] = {-1,-1,-1,-1};
97 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
99 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
100 for (int I
= 0; I
!= 4; I
++)
104 for (int I
= 0; I
!= 4; I
++)
105 SetCloseExec(Pipes
[I
],true);
107 Process
= ExecFork();
112 const char *Args
[400];
115 dup2(Pipes
[1],STDOUT_FILENO
);
116 dup2(Pipes
[2],STDIN_FILENO
);
118 // Probably should do
119 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
123 // Insert user-supplied command line options
124 Configuration::Item
const *Opts
= RshOptions
;
128 for (; Opts
!= 0; Opts
= Opts
->Next
)
130 if (Opts
->Value
.empty() == true)
132 Args
[i
++] = Opts
->Value
.c_str();
136 if (User
.empty() == false) {
138 Args
[i
++] = User
.c_str();
140 if (Host
.empty() == false) {
141 Args
[i
++] = Host
.c_str();
143 Args
[i
++] = "/bin/sh";
145 execvp(Args
[0],(char **)Args
);
151 SetNonBlock(Pipes
[0],true);
152 SetNonBlock(Pipes
[3],true);
159 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
160 // ---------------------------------------------------------------------
162 bool RSHConn::ReadLine(std::string
&Text
)
164 if (Process
== -1 || ReadFd
== -1)
168 while (Len
< sizeof(Buffer
))
170 // Scan the buffer for a new line
171 for (unsigned int I
= 0; I
!= Len
; I
++)
173 // Escape some special chars
178 if (Buffer
[I
] != '\n')
182 Text
= std::string(Buffer
,I
);
183 memmove(Buffer
,Buffer
+I
,Len
- I
);
188 // Wait for some data..
189 if (WaitFd(ReadFd
,false,TimeOut
) == false)
192 return _error
->Error(_("Connection timeout"));
196 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
199 _error
->Errno("read",_("Read error"));
206 return _error
->Error(_("A response overflowed the buffer."));
209 // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
210 // ---------------------------------------------------------------------
211 /* The remote sync flag appends a || echo which will insert blank line
212 once the command completes. */
213 bool RSHConn::WriteMsg(std::string
&Text
,bool Sync
,const char *Fmt
,...)
218 // sprintf the description
220 vsnprintf(S
,sizeof(S
) - 4,Fmt
,args
);
222 strcat(S
," 2> /dev/null || echo\n");
224 strcat(S
," 2> /dev/null\n");
227 unsigned long Len
= strlen(S
);
228 unsigned long Start
= 0;
231 if (WaitFd(WriteFd
,true,TimeOut
) == false)
235 return _error
->Error(_("Connection timeout"));
238 int Res
= write(WriteFd
,S
+ Start
,Len
);
241 _error
->Errno("write",_("Write error"));
251 return ReadLine(Text
);
255 // RSHConn::Size - Return the size of the file /*{{{*/
256 // ---------------------------------------------------------------------
257 /* Right now for successfull transfer the file size must be known in
259 bool RSHConn::Size(const char *Path
,unsigned long long &Size
)
265 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
268 // FIXME: Sense if the bad reply is due to a File Not Found.
271 Size
= strtoull(Msg
.c_str(),&End
,10);
272 if (End
== Msg
.c_str())
273 return _error
->Error(_("File not found"));
277 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
278 // ---------------------------------------------------------------------
280 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
283 // Query the mod time
286 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
290 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
293 // RSHConn::Get - Get a file /*{{{*/
294 // ---------------------------------------------------------------------
296 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long long Resume
,
297 Hashes
&Hash
,bool &Missing
, unsigned long long Size
)
301 // Round to a 2048 byte block
302 Resume
= Resume
- (Resume
% 2048);
304 if (To
.Truncate(Resume
) == false)
306 if (To
.Seek(0) == false)
310 if (Hash
.AddFD(To
,Resume
) == false) {
311 _error
->Errno("read",_("Problem hashing file"));
316 // FIXME: Detect file-not openable type errors.
318 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
322 unsigned long long MyLen
= Resume
;
323 unsigned char Buffer
[4096];
326 // Wait for some data..
327 if (WaitFd(ReadFd
,false,TimeOut
) == false)
330 return _error
->Error(_("Data socket timed out"));
334 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
338 return _error
->Error(_("Connection closed prematurely"));
349 Hash
.Add(Buffer
,Res
);
350 if (To
.Write(Buffer
,Res
) == false)
361 // RSHMethod::RSHMethod - Constructor /*{{{*/
362 // ---------------------------------------------------------------------
364 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig
)
366 signal(SIGTERM
,SigTerm
);
367 signal(SIGINT
,SigTerm
);
372 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
373 // ---------------------------------------------------------------------
374 bool RSHMethod::Configuration(std::string Message
)
378 if (pkgAcqMethod::Configuration(Message
) == false)
381 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Timeout", Prog
);
382 TimeOut
= _config
->FindI(ProgStr
,TimeOut
);
383 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Options", Prog
);
384 RshOptions
= _config
->Tree(ProgStr
);
389 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
390 // ---------------------------------------------------------------------
392 void RSHMethod::SigTerm(int sig
)
400 UBuf
.actime
= FailTime
;
401 UBuf
.modtime
= FailTime
;
402 utime(FailFile
.c_str(),&UBuf
);
407 // RSHMethod::Fetch - Fetch a URI /*{{{*/
408 // ---------------------------------------------------------------------
410 bool RSHMethod::Fetch(FetchItem
*Itm
)
413 const char *File
= Get
.Path
.c_str();
415 Res
.Filename
= Itm
->DestFile
;
418 // Connect to the server
419 if (Server
== 0 || Server
->Comp(Get
) == false) {
421 Server
= new RSHConn(Get
);
424 // Could not connect is a transient error..
425 if (Server
->Open() == false) {
431 // We say this mainly because the pause here is for the
432 // ssh connection that is still going
433 Status(_("Connecting to %s"), Get
.Host
.c_str());
435 // Get the files information
436 unsigned long long Size
;
437 if (Server
->Size(File
,Size
) == false ||
438 Server
->ModTime(File
,FailTime
) == false)
441 //_error->Error(_("File not found")); // Will be handled by Size
446 // See if it is an IMS hit
447 if (Itm
->LastModified
== FailTime
) {
454 // See if the file exists
456 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
457 if (Size
== (unsigned long long)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
458 Res
.Size
= Buf
.st_size
;
459 Res
.LastModified
= Buf
.st_mtime
;
460 Res
.ResumePoint
= Buf
.st_size
;
466 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned long long)Buf
.st_size
)
467 Res
.ResumePoint
= Buf
.st_size
;
473 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
474 if (_error
->PendingError() == true)
479 FailFile
= Itm
->DestFile
;
480 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
484 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
490 UBuf
.actime
= FailTime
;
491 UBuf
.modtime
= FailTime
;
492 utime(FailFile
.c_str(),&UBuf
);
494 // If the file is missing we hard fail otherwise transient fail
501 Res
.Size
= Fd
.Size();
504 Res
.LastModified
= FailTime
;
505 Res
.TakeHashes(Hash
);
509 UBuf
.actime
= FailTime
;
510 UBuf
.modtime
= FailTime
;
511 utime(Queue
->DestFile
.c_str(),&UBuf
);
520 int main(int argc
, const char *argv
[])
522 setlocale(LC_ALL
, "");
525 Prog
= strrchr(argv
[0],'/');