]>
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>
34 unsigned long TimeOut
= 120;
35 Configuration::Item
const *RshOptions
= 0;
36 time_t RSHMethod::FailTime
= 0;
37 std::string
RSHMethod::FailFile
;
38 int RSHMethod::FailFd
= -1;
40 // RSHConn::RSHConn - Constructor /*{{{*/
41 // ---------------------------------------------------------------------
43 RSHConn::RSHConn(URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
44 ServerName(Srv
), Process(-1) {
48 // RSHConn::RSHConn - Destructor /*{{{*/
49 // ---------------------------------------------------------------------
56 // RSHConn::Close - Forcibly terminate the connection /*{{{*/
57 // ---------------------------------------------------------------------
58 /* Often this is called when things have gone wrong to indicate that the
59 connection is no longer usable. */
68 ExecWait(Process
,"",true);
74 // RSHConn::Open - Connect to a host /*{{{*/
75 // ---------------------------------------------------------------------
79 // Use the already open connection if possible.
83 if (Connect(ServerName
.Host
,ServerName
.User
) == false)
89 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
90 // ---------------------------------------------------------------------
92 bool RSHConn::Connect(std::string Host
, std::string User
)
95 int Pipes
[4] = {-1,-1,-1,-1};
96 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
98 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
99 for (int I
= 0; I
!= 4; I
++)
103 for (int I
= 0; I
!= 4; I
++)
104 SetCloseExec(Pipes
[I
],true);
106 Process
= ExecFork();
111 const char *Args
[400];
114 dup2(Pipes
[1],STDOUT_FILENO
);
115 dup2(Pipes
[2],STDIN_FILENO
);
117 // Probably should do
118 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
122 // Insert user-supplied command line options
123 Configuration::Item
const *Opts
= RshOptions
;
127 for (; Opts
!= 0; Opts
= Opts
->Next
)
129 if (Opts
->Value
.empty() == true)
131 Args
[i
++] = Opts
->Value
.c_str();
135 if (User
.empty() == false) {
137 Args
[i
++] = User
.c_str();
139 if (Host
.empty() == false) {
140 Args
[i
++] = Host
.c_str();
142 Args
[i
++] = "/bin/sh";
144 execvp(Args
[0],(char **)Args
);
150 SetNonBlock(Pipes
[0],true);
151 SetNonBlock(Pipes
[3],true);
158 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
159 // ---------------------------------------------------------------------
161 bool RSHConn::ReadLine(std::string
&Text
)
163 if (Process
== -1 || ReadFd
== -1)
167 while (Len
< sizeof(Buffer
))
169 // Scan the buffer for a new line
170 for (unsigned int I
= 0; I
!= Len
; I
++)
172 // Escape some special chars
177 if (Buffer
[I
] != '\n')
181 Text
= std::string(Buffer
,I
);
182 memmove(Buffer
,Buffer
+I
,Len
- I
);
187 // Wait for some data..
188 if (WaitFd(ReadFd
,false,TimeOut
) == false)
191 return _error
->Error(_("Connection timeout"));
195 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
198 _error
->Errno("read",_("Read error"));
205 return _error
->Error(_("A response overflowed the buffer."));
208 // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
209 // ---------------------------------------------------------------------
210 /* The remote sync flag appends a || echo which will insert blank line
211 once the command completes. */
212 bool RSHConn::WriteMsg(std::string
&Text
,bool Sync
,const char *Fmt
,...)
217 // sprintf the description
219 vsnprintf(S
,sizeof(S
) - 4,Fmt
,args
);
223 strcat(S
," 2> /dev/null || echo\n");
225 strcat(S
," 2> /dev/null\n");
228 unsigned long Len
= strlen(S
);
229 unsigned long Start
= 0;
232 if (WaitFd(WriteFd
,true,TimeOut
) == false)
236 return _error
->Error(_("Connection timeout"));
239 int Res
= write(WriteFd
,S
+ Start
,Len
);
242 _error
->Errno("write",_("Write error"));
252 return ReadLine(Text
);
256 // RSHConn::Size - Return the size of the file /*{{{*/
257 // ---------------------------------------------------------------------
258 /* Right now for successful transfer the file size must be known in
260 bool RSHConn::Size(const char *Path
,unsigned long long &Size
)
266 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
269 // FIXME: Sense if the bad reply is due to a File Not Found.
272 Size
= strtoull(Msg
.c_str(),&End
,10);
273 if (End
== Msg
.c_str())
274 return _error
->Error(_("File not found"));
278 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
279 // ---------------------------------------------------------------------
281 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
284 // Query the mod time
287 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
291 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
294 // RSHConn::Get - Get a file /*{{{*/
295 // ---------------------------------------------------------------------
297 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long long Resume
,
298 Hashes
&Hash
,bool &Missing
, unsigned long long Size
)
302 // Round to a 2048 byte block
303 Resume
= Resume
- (Resume
% 2048);
305 if (To
.Truncate(Resume
) == false)
307 if (To
.Seek(0) == false)
311 if (Hash
.AddFD(To
,Resume
) == false) {
312 _error
->Errno("read",_("Problem hashing file"));
317 // FIXME: Detect file-not openable type errors.
319 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
323 unsigned long long MyLen
= Resume
;
324 unsigned char Buffer
[4096];
327 // Wait for some data..
328 if (WaitFd(ReadFd
,false,TimeOut
) == false)
331 return _error
->Error(_("Data socket timed out"));
335 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
339 return _error
->Error(_("Connection closed prematurely"));
350 Hash
.Add(Buffer
,Res
);
351 if (To
.Write(Buffer
,Res
) == false)
362 // RSHMethod::RSHMethod - Constructor /*{{{*/
363 // ---------------------------------------------------------------------
365 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig
)
367 signal(SIGTERM
,SigTerm
);
368 signal(SIGINT
,SigTerm
);
373 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
374 // ---------------------------------------------------------------------
375 bool RSHMethod::Configuration(std::string Message
)
379 if (pkgAcqMethod::Configuration(Message
) == false)
382 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Timeout", Prog
);
383 TimeOut
= _config
->FindI(ProgStr
,TimeOut
);
384 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Options", Prog
);
385 RshOptions
= _config
->Tree(ProgStr
);
390 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
391 // ---------------------------------------------------------------------
393 void RSHMethod::SigTerm(int sig
)
398 // Transfer the modification times
399 struct timeval times
[2];
400 times
[0].tv_sec
= FailTime
;
401 times
[1].tv_sec
= FailTime
;
402 times
[0].tv_usec
= times
[1].tv_usec
= 0;
403 utimes(FailFile
.c_str(), times
);
409 // RSHMethod::Fetch - Fetch a URI /*{{{*/
410 // ---------------------------------------------------------------------
412 bool RSHMethod::Fetch(FetchItem
*Itm
)
415 const char *File
= Get
.Path
.c_str();
417 Res
.Filename
= Itm
->DestFile
;
420 // Connect to the server
421 if (Server
== 0 || Server
->Comp(Get
) == false) {
423 Server
= new RSHConn(Get
);
426 // Could not connect is a transient error..
427 if (Server
->Open() == false) {
433 // We say this mainly because the pause here is for the
434 // ssh connection that is still going
435 Status(_("Connecting to %s"), Get
.Host
.c_str());
437 // Get the files information
438 unsigned long long Size
;
439 if (Server
->Size(File
,Size
) == false ||
440 Server
->ModTime(File
,FailTime
) == false)
443 //_error->Error(_("File not found")); // Will be handled by Size
448 // See if it is an IMS hit
449 if (Itm
->LastModified
== FailTime
) {
456 // See if the file exists
458 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
459 if (Size
== (unsigned long long)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
460 Res
.Size
= Buf
.st_size
;
461 Res
.LastModified
= Buf
.st_mtime
;
462 Res
.ResumePoint
= Buf
.st_size
;
468 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned long long)Buf
.st_size
)
469 Res
.ResumePoint
= Buf
.st_size
;
475 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
476 if (_error
->PendingError() == true)
481 FailFile
= Itm
->DestFile
;
482 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
486 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
491 struct timeval times
[2];
492 times
[0].tv_sec
= FailTime
;
493 times
[1].tv_sec
= FailTime
;
494 times
[0].tv_usec
= times
[1].tv_usec
= 0;
495 utimes(FailFile
.c_str(), times
);
497 // If the file is missing we hard fail otherwise transient fail
504 Res
.Size
= Fd
.Size();
505 struct timeval times
[2];
506 times
[0].tv_sec
= FailTime
;
507 times
[1].tv_sec
= FailTime
;
508 times
[0].tv_usec
= times
[1].tv_usec
= 0;
509 utimes(Fd
.Name().c_str(), times
);
513 Res
.LastModified
= FailTime
;
514 Res
.TakeHashes(Hash
);
522 int main(int argc
, const char *argv
[])
524 setlocale(LC_ALL
, "");
527 Prog
= strrchr(argv
[0],'/');