]>
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>
20 #include <apt-pkg/acquire-method.h>
21 #include <apt-pkg/strutl.h>
38 unsigned long TimeOut
= 120;
39 Configuration::Item
const *RshOptions
= 0;
40 time_t RSHMethod::FailTime
= 0;
41 std::string
RSHMethod::FailFile
;
42 int RSHMethod::FailFd
= -1;
44 // RSHConn::RSHConn - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
47 RSHConn::RSHConn(URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
48 ServerName(Srv
), Process(-1) {
52 // RSHConn::RSHConn - Destructor /*{{{*/
53 // ---------------------------------------------------------------------
60 // RSHConn::Close - Forcibly terminate the connection /*{{{*/
61 // ---------------------------------------------------------------------
62 /* Often this is called when things have gone wrong to indicate that the
63 connection is no longer usable. */
72 ExecWait(Process
,"",true);
78 // RSHConn::Open - Connect to a host /*{{{*/
79 // ---------------------------------------------------------------------
83 // Use the already open connection if possible.
87 if (Connect(ServerName
.Host
,ServerName
.Port
,ServerName
.User
) == false)
93 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
94 // ---------------------------------------------------------------------
96 bool RSHConn::Connect(std::string Host
, unsigned int Port
, std::string User
)
101 if (asprintf (&PortStr
, "%d", Port
) == -1 || PortStr
== NULL
)
102 return _error
->Errno("asprintf", _("Failed"));
106 int Pipes
[4] = {-1,-1,-1,-1};
107 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
109 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
110 for (int I
= 0; I
!= 4; I
++)
114 for (int I
= 0; I
!= 4; I
++)
115 SetCloseExec(Pipes
[I
],true);
117 Process
= ExecFork();
122 const char *Args
[400];
125 dup2(Pipes
[1],STDOUT_FILENO
);
126 dup2(Pipes
[2],STDIN_FILENO
);
128 // Probably should do
129 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
133 // Insert user-supplied command line options
134 Configuration::Item
const *Opts
= RshOptions
;
138 for (; Opts
!= 0; Opts
= Opts
->Next
)
140 if (Opts
->Value
.empty() == true)
142 Args
[i
++] = Opts
->Value
.c_str();
146 if (User
.empty() == false) {
148 Args
[i
++] = User
.c_str();
150 if (PortStr
!= NULL
) {
154 if (Host
.empty() == false) {
155 Args
[i
++] = Host
.c_str();
157 Args
[i
++] = "/bin/sh";
159 execvp(Args
[0],(char **)Args
);
168 SetNonBlock(Pipes
[0],true);
169 SetNonBlock(Pipes
[3],true);
175 bool RSHConn::Connect(std::string Host
, std::string User
)
177 return Connect(Host
, 0, User
);
180 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
181 // ---------------------------------------------------------------------
183 bool RSHConn::ReadLine(std::string
&Text
)
185 if (Process
== -1 || ReadFd
== -1)
189 while (Len
< sizeof(Buffer
))
191 // Scan the buffer for a new line
192 for (unsigned int I
= 0; I
!= Len
; I
++)
194 // Escape some special chars
199 if (Buffer
[I
] != '\n')
203 Text
= std::string(Buffer
,I
);
204 memmove(Buffer
,Buffer
+I
,Len
- I
);
209 // Wait for some data..
210 if (WaitFd(ReadFd
,false,TimeOut
) == false)
213 return _error
->Error(_("Connection timeout"));
217 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
220 _error
->Errno("read",_("Read error"));
227 return _error
->Error(_("A response overflowed the buffer."));
230 // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
231 // ---------------------------------------------------------------------
232 /* The remote sync flag appends a || echo which will insert blank line
233 once the command completes. */
234 bool RSHConn::WriteMsg(std::string
&Text
,bool Sync
,const char *Fmt
,...)
239 // sprintf into a buffer
241 vsnprintf(Tmp
,sizeof(Tmp
),Fmt
,args
);
244 // concat to create the real msg
247 Msg
= std::string(Tmp
) + " 2> /dev/null || echo\n";
249 Msg
= std::string(Tmp
) + " 2> /dev/null\n";
252 const char *S
= Msg
.c_str();
253 unsigned long Len
= strlen(S
);
254 unsigned long Start
= 0;
257 if (WaitFd(WriteFd
,true,TimeOut
) == false)
261 return _error
->Error(_("Connection timeout"));
264 int Res
= write(WriteFd
,S
+ Start
,Len
);
267 _error
->Errno("write",_("Write error"));
277 return ReadLine(Text
);
281 // RSHConn::Size - Return the size of the file /*{{{*/
282 // ---------------------------------------------------------------------
283 /* Right now for successful transfer the file size must be known in
285 bool RSHConn::Size(const char *Path
,unsigned long long &Size
)
291 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
294 // FIXME: Sense if the bad reply is due to a File Not Found.
297 Size
= strtoull(Msg
.c_str(),&End
,10);
298 if (End
== Msg
.c_str())
299 return _error
->Error(_("File not found"));
303 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
304 // ---------------------------------------------------------------------
306 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
309 // Query the mod time
312 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
316 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
319 // RSHConn::Get - Get a file /*{{{*/
320 // ---------------------------------------------------------------------
322 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long long Resume
,
323 Hashes
&Hash
,bool &Missing
, unsigned long long Size
)
327 // Round to a 2048 byte block
328 Resume
= Resume
- (Resume
% 2048);
330 if (To
.Truncate(Resume
) == false)
332 if (To
.Seek(0) == false)
336 if (Hash
.AddFD(To
,Resume
) == false) {
337 _error
->Errno("read",_("Problem hashing file"));
342 // FIXME: Detect file-not openable type errors.
344 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
348 unsigned long long MyLen
= Resume
;
349 unsigned char Buffer
[4096];
352 // Wait for some data..
353 if (WaitFd(ReadFd
,false,TimeOut
) == false)
356 return _error
->Error(_("Data socket timed out"));
360 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
364 return _error
->Error(_("Connection closed prematurely"));
375 Hash
.Add(Buffer
,Res
);
376 if (To
.Write(Buffer
,Res
) == false)
387 // RSHMethod::RSHMethod - Constructor /*{{{*/
388 // ---------------------------------------------------------------------
390 RSHMethod::RSHMethod(std::string
const &pProg
) : aptMethod(pProg
.c_str(),"1.0",SendConfig
), Prog(pProg
)
392 signal(SIGTERM
,SigTerm
);
393 signal(SIGINT
,SigTerm
);
398 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
399 // ---------------------------------------------------------------------
400 bool RSHMethod::Configuration(std::string Message
)
402 if (aptMethod::Configuration(Message
) == false)
405 std::string
const timeconf
= std::string("Acquire::") + Prog
+ "::Timeout";
406 TimeOut
= _config
->FindI(timeconf
, TimeOut
);
407 std::string
const optsconf
= std::string("Acquire::") + Prog
+ "::Options";
408 RshOptions
= _config
->Tree(optsconf
.c_str());
413 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
414 // ---------------------------------------------------------------------
416 void RSHMethod::SigTerm(int)
421 // Transfer the modification times
422 struct timeval times
[2];
423 times
[0].tv_sec
= FailTime
;
424 times
[1].tv_sec
= FailTime
;
425 times
[0].tv_usec
= times
[1].tv_usec
= 0;
426 utimes(FailFile
.c_str(), times
);
432 // RSHMethod::Fetch - Fetch a URI /*{{{*/
433 // ---------------------------------------------------------------------
435 bool RSHMethod::Fetch(FetchItem
*Itm
)
438 const char *File
= Get
.Path
.c_str();
440 Res
.Filename
= Itm
->DestFile
;
443 // Connect to the server
444 if (Server
== 0 || Server
->Comp(Get
) == false) {
446 Server
= new RSHConn(Get
);
449 // Could not connect is a transient error..
450 if (Server
->Open() == false) {
456 // We say this mainly because the pause here is for the
457 // ssh connection that is still going
458 Status(_("Connecting to %s"), Get
.Host
.c_str());
460 // Get the files information
461 unsigned long long Size
;
462 if (Server
->Size(File
,Size
) == false ||
463 Server
->ModTime(File
,FailTime
) == false)
466 //_error->Error(_("File not found")); // Will be handled by Size
471 // See if it is an IMS hit
472 if (Itm
->LastModified
== FailTime
) {
479 // See if the file exists
481 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
482 if (Size
== (unsigned long long)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
483 Res
.Size
= Buf
.st_size
;
484 Res
.LastModified
= Buf
.st_mtime
;
485 Res
.ResumePoint
= Buf
.st_size
;
491 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned long long)Buf
.st_size
)
492 Res
.ResumePoint
= Buf
.st_size
;
496 Hashes
Hash(Itm
->ExpectedHashes
);
498 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
499 if (_error
->PendingError() == true)
504 FailFile
= Itm
->DestFile
;
505 FailFile
.c_str(); // Make sure we don't do a malloc in the signal handler
509 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
514 struct timeval times
[2];
515 times
[0].tv_sec
= FailTime
;
516 times
[1].tv_sec
= FailTime
;
517 times
[0].tv_usec
= times
[1].tv_usec
= 0;
518 utimes(FailFile
.c_str(), times
);
520 // If the file is missing we hard fail otherwise transient fail
527 Res
.Size
= Fd
.Size();
528 struct timeval times
[2];
529 times
[0].tv_sec
= FailTime
;
530 times
[1].tv_sec
= FailTime
;
531 times
[0].tv_usec
= times
[1].tv_usec
= 0;
532 utimes(Fd
.Name().c_str(), times
);
536 Res
.LastModified
= FailTime
;
537 Res
.TakeHashes(Hash
);
545 int main(int, const char *argv
[])
547 setlocale(LC_ALL
, "");
549 RSHMethod
Mth(flNotDir(argv
[0]));