]>
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
.User
) == false)
93 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
94 // ---------------------------------------------------------------------
96 bool RSHConn::Connect(std::string Host
, std::string User
)
99 int Pipes
[4] = {-1,-1,-1,-1};
100 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
102 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
103 for (int I
= 0; I
!= 4; I
++)
107 for (int I
= 0; I
!= 4; I
++)
108 SetCloseExec(Pipes
[I
],true);
110 Process
= ExecFork();
115 const char *Args
[400];
118 dup2(Pipes
[1],STDOUT_FILENO
);
119 dup2(Pipes
[2],STDIN_FILENO
);
121 // Probably should do
122 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
126 // Insert user-supplied command line options
127 Configuration::Item
const *Opts
= RshOptions
;
131 for (; Opts
!= 0; Opts
= Opts
->Next
)
133 if (Opts
->Value
.empty() == true)
135 Args
[i
++] = Opts
->Value
.c_str();
139 if (User
.empty() == false) {
141 Args
[i
++] = User
.c_str();
143 if (Host
.empty() == false) {
144 Args
[i
++] = Host
.c_str();
146 Args
[i
++] = "/bin/sh";
148 execvp(Args
[0],(char **)Args
);
154 SetNonBlock(Pipes
[0],true);
155 SetNonBlock(Pipes
[3],true);
162 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
163 // ---------------------------------------------------------------------
165 bool RSHConn::ReadLine(std::string
&Text
)
167 if (Process
== -1 || ReadFd
== -1)
171 while (Len
< sizeof(Buffer
))
173 // Scan the buffer for a new line
174 for (unsigned int I
= 0; I
!= Len
; I
++)
176 // Escape some special chars
181 if (Buffer
[I
] != '\n')
185 Text
= std::string(Buffer
,I
);
186 memmove(Buffer
,Buffer
+I
,Len
- I
);
191 // Wait for some data..
192 if (WaitFd(ReadFd
,false,TimeOut
) == false)
195 return _error
->Error(_("Connection timeout"));
199 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
202 _error
->Errno("read",_("Read error"));
209 return _error
->Error(_("A response overflowed the buffer."));
212 // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
213 // ---------------------------------------------------------------------
214 /* The remote sync flag appends a || echo which will insert blank line
215 once the command completes. */
216 bool RSHConn::WriteMsg(std::string
&Text
,bool Sync
,const char *Fmt
,...)
221 // sprintf into a buffer
223 vsnprintf(Tmp
,sizeof(Tmp
),Fmt
,args
);
226 // concat to create the real msg
229 Msg
= std::string(Tmp
) + " 2> /dev/null || echo\n";
231 Msg
= std::string(Tmp
) + " 2> /dev/null\n";
234 const char *S
= Msg
.c_str();
235 unsigned long Len
= strlen(S
);
236 unsigned long Start
= 0;
239 if (WaitFd(WriteFd
,true,TimeOut
) == false)
243 return _error
->Error(_("Connection timeout"));
246 int Res
= write(WriteFd
,S
+ Start
,Len
);
249 _error
->Errno("write",_("Write error"));
259 return ReadLine(Text
);
263 // RSHConn::Size - Return the size of the file /*{{{*/
264 // ---------------------------------------------------------------------
265 /* Right now for successful transfer the file size must be known in
267 bool RSHConn::Size(const char *Path
,unsigned long long &Size
)
273 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
276 // FIXME: Sense if the bad reply is due to a File Not Found.
279 Size
= strtoull(Msg
.c_str(),&End
,10);
280 if (End
== Msg
.c_str())
281 return _error
->Error(_("File not found"));
285 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
286 // ---------------------------------------------------------------------
288 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
291 // Query the mod time
294 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
298 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
301 // RSHConn::Get - Get a file /*{{{*/
302 // ---------------------------------------------------------------------
304 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long long Resume
,
305 Hashes
&Hash
,bool &Missing
, unsigned long long Size
)
309 // Round to a 2048 byte block
310 Resume
= Resume
- (Resume
% 2048);
312 if (To
.Truncate(Resume
) == false)
314 if (To
.Seek(0) == false)
318 if (Hash
.AddFD(To
,Resume
) == false) {
319 _error
->Errno("read",_("Problem hashing file"));
324 // FIXME: Detect file-not openable type errors.
326 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
330 unsigned long long MyLen
= Resume
;
331 unsigned char Buffer
[4096];
334 // Wait for some data..
335 if (WaitFd(ReadFd
,false,TimeOut
) == false)
338 return _error
->Error(_("Data socket timed out"));
342 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
346 return _error
->Error(_("Connection closed prematurely"));
357 Hash
.Add(Buffer
,Res
);
358 if (To
.Write(Buffer
,Res
) == false)
369 // RSHMethod::RSHMethod - Constructor /*{{{*/
370 // ---------------------------------------------------------------------
372 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig
)
374 signal(SIGTERM
,SigTerm
);
375 signal(SIGINT
,SigTerm
);
380 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
381 // ---------------------------------------------------------------------
382 bool RSHMethod::Configuration(std::string Message
)
386 if (pkgAcqMethod::Configuration(Message
) == false)
389 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Timeout", Prog
);
390 TimeOut
= _config
->FindI(ProgStr
,TimeOut
);
391 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Options", Prog
);
392 RshOptions
= _config
->Tree(ProgStr
);
397 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
398 // ---------------------------------------------------------------------
400 void RSHMethod::SigTerm(int)
405 // Transfer the modification times
406 struct timeval times
[2];
407 times
[0].tv_sec
= FailTime
;
408 times
[1].tv_sec
= FailTime
;
409 times
[0].tv_usec
= times
[1].tv_usec
= 0;
410 utimes(FailFile
.c_str(), times
);
416 // RSHMethod::Fetch - Fetch a URI /*{{{*/
417 // ---------------------------------------------------------------------
419 bool RSHMethod::Fetch(FetchItem
*Itm
)
422 const char *File
= Get
.Path
.c_str();
424 Res
.Filename
= Itm
->DestFile
;
427 // Connect to the server
428 if (Server
== 0 || Server
->Comp(Get
) == false) {
430 Server
= new RSHConn(Get
);
433 // Could not connect is a transient error..
434 if (Server
->Open() == false) {
440 // We say this mainly because the pause here is for the
441 // ssh connection that is still going
442 Status(_("Connecting to %s"), Get
.Host
.c_str());
444 // Get the files information
445 unsigned long long Size
;
446 if (Server
->Size(File
,Size
) == false ||
447 Server
->ModTime(File
,FailTime
) == false)
450 //_error->Error(_("File not found")); // Will be handled by Size
455 // See if it is an IMS hit
456 if (Itm
->LastModified
== FailTime
) {
463 // See if the file exists
465 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
466 if (Size
== (unsigned long long)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
467 Res
.Size
= Buf
.st_size
;
468 Res
.LastModified
= Buf
.st_mtime
;
469 Res
.ResumePoint
= Buf
.st_size
;
475 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned long long)Buf
.st_size
)
476 Res
.ResumePoint
= Buf
.st_size
;
482 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
483 if (_error
->PendingError() == true)
488 FailFile
= Itm
->DestFile
;
489 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
493 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
498 struct timeval times
[2];
499 times
[0].tv_sec
= FailTime
;
500 times
[1].tv_sec
= FailTime
;
501 times
[0].tv_usec
= times
[1].tv_usec
= 0;
502 utimes(FailFile
.c_str(), times
);
504 // If the file is missing we hard fail otherwise transient fail
511 Res
.Size
= Fd
.Size();
512 struct timeval times
[2];
513 times
[0].tv_sec
= FailTime
;
514 times
[1].tv_sec
= FailTime
;
515 times
[0].tv_usec
= times
[1].tv_usec
= 0;
516 utimes(Fd
.Name().c_str(), times
);
520 Res
.LastModified
= FailTime
;
521 Res
.TakeHashes(Hash
);
529 int main(int, const char *argv
[])
531 setlocale(LC_ALL
, "");
534 Prog
= strrchr(argv
[0],'/');