]>
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>
32 unsigned long TimeOut
= 120;
33 Configuration::Item
const *RshOptions
= 0;
34 time_t RSHMethod::FailTime
= 0;
35 string
RSHMethod::FailFile
;
36 int RSHMethod::FailFd
= -1;
38 // RSHConn::RSHConn - Constructor /*{{{*/
39 // ---------------------------------------------------------------------
41 RSHConn::RSHConn(URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
42 ServerName(Srv
), Process(-1) {}
44 // RSHConn::RSHConn - Destructor /*{{{*/
45 // ---------------------------------------------------------------------
52 // RSHConn::Close - Forcibly terminate the connection /*{{{*/
53 // ---------------------------------------------------------------------
54 /* Often this is called when things have gone wrong to indicate that the
55 connection is no longer usable. */
64 ExecWait(Process
,"",true);
70 // RSHConn::Open - Connect to a host /*{{{*/
71 // ---------------------------------------------------------------------
75 // Use the already open connection if possible.
79 if (Connect(ServerName
.Host
,ServerName
.User
) == false)
85 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
86 // ---------------------------------------------------------------------
88 bool RSHConn::Connect(string Host
, string User
)
91 int Pipes
[4] = {-1,-1,-1,-1};
92 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
94 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
95 for (int I
= 0; I
!= 4; I
++)
99 for (int I
= 0; I
!= 4; I
++)
100 SetCloseExec(Pipes
[I
],true);
102 Process
= ExecFork();
107 const char *Args
[400];
110 dup2(Pipes
[1],STDOUT_FILENO
);
111 dup2(Pipes
[2],STDIN_FILENO
);
113 // Probably should do
114 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
118 // Insert user-supplied command line options
119 Configuration::Item
const *Opts
= RshOptions
;
123 for (; Opts
!= 0; Opts
= Opts
->Next
)
125 if (Opts
->Value
.empty() == true)
127 Args
[i
++] = Opts
->Value
.c_str();
131 if (User
.empty() == false) {
133 Args
[i
++] = User
.c_str();
135 if (Host
.empty() == false) {
136 Args
[i
++] = Host
.c_str();
138 Args
[i
++] = "/bin/sh";
140 execvp(Args
[0],(char **)Args
);
146 SetNonBlock(Pipes
[0],true);
147 SetNonBlock(Pipes
[3],true);
154 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
155 // ---------------------------------------------------------------------
157 bool RSHConn::ReadLine(string
&Text
)
159 if (Process
== -1 || ReadFd
== -1)
163 while (Len
< sizeof(Buffer
))
165 // Scan the buffer for a new line
166 for (unsigned int I
= 0; I
!= Len
; I
++)
168 // Escape some special chars
173 if (Buffer
[I
] != '\n')
177 Text
= string(Buffer
,I
);
178 memmove(Buffer
,Buffer
+I
,Len
- I
);
183 // Wait for some data..
184 if (WaitFd(ReadFd
,false,TimeOut
) == false)
187 return _error
->Error(_("Connection timeout"));
191 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
194 _error
->Errno("read",_("Read error"));
201 return _error
->Error(_("A response overflowed the buffer."));
204 // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
205 // ---------------------------------------------------------------------
206 /* The remote sync flag appends a || echo which will insert blank line
207 once the command completes. */
208 bool RSHConn::WriteMsg(string
&Text
,bool Sync
,const char *Fmt
,...)
213 // sprintf the description
215 vsnprintf(S
,sizeof(S
) - 4,Fmt
,args
);
217 strcat(S
," 2> /dev/null || echo\n");
219 strcat(S
," 2> /dev/null\n");
222 unsigned long Len
= strlen(S
);
223 unsigned long Start
= 0;
226 if (WaitFd(WriteFd
,true,TimeOut
) == false)
230 return _error
->Error(_("Connection timeout"));
233 int Res
= write(WriteFd
,S
+ Start
,Len
);
236 _error
->Errno("write",_("Write error"));
246 return ReadLine(Text
);
250 // RSHConn::Size - Return the size of the file /*{{{*/
251 // ---------------------------------------------------------------------
252 /* Right now for successfull transfer the file size must be known in
254 bool RSHConn::Size(const char *Path
,unsigned long &Size
)
260 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
263 // FIXME: Sense if the bad reply is due to a File Not Found.
266 Size
= strtoul(Msg
.c_str(),&End
,10);
267 if (End
== Msg
.c_str())
268 return _error
->Error(_("File not found"));
272 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
273 // ---------------------------------------------------------------------
275 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
278 // Query the mod time
281 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
285 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
288 // RSHConn::Get - Get a file /*{{{*/
289 // ---------------------------------------------------------------------
291 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long Resume
,
292 Hashes
&Hash
,bool &Missing
, unsigned long Size
)
296 // Round to a 2048 byte block
297 Resume
= Resume
- (Resume
% 2048);
299 if (To
.Truncate(Resume
) == false)
301 if (To
.Seek(0) == false)
305 if (Hash
.AddFD(To
.Fd(),Resume
) == false) {
306 _error
->Errno("read",_("Problem hashing file"));
311 // FIXME: Detect file-not openable type errors.
313 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
317 unsigned int MyLen
= Resume
;
318 unsigned char Buffer
[4096];
321 // Wait for some data..
322 if (WaitFd(ReadFd
,false,TimeOut
) == false)
325 return _error
->Error(_("Data socket timed out"));
329 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
333 return _error
->Error(_("Connection closed prematurely"));
344 Hash
.Add(Buffer
,Res
);
345 if (To
.Write(Buffer
,Res
) == false)
356 // RSHMethod::RSHMethod - Constructor /*{{{*/
357 // ---------------------------------------------------------------------
359 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig
)
361 signal(SIGTERM
,SigTerm
);
362 signal(SIGINT
,SigTerm
);
367 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
368 // ---------------------------------------------------------------------
369 bool RSHMethod::Configuration(string Message
)
373 if (pkgAcqMethod::Configuration(Message
) == false)
376 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Timeout", Prog
);
377 TimeOut
= _config
->FindI(ProgStr
,TimeOut
);
378 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Options", Prog
);
379 RshOptions
= _config
->Tree(ProgStr
);
384 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
385 // ---------------------------------------------------------------------
387 void RSHMethod::SigTerm(int sig
)
395 UBuf
.actime
= FailTime
;
396 UBuf
.modtime
= FailTime
;
397 utime(FailFile
.c_str(),&UBuf
);
402 // RSHMethod::Fetch - Fetch a URI /*{{{*/
403 // ---------------------------------------------------------------------
405 bool RSHMethod::Fetch(FetchItem
*Itm
)
408 const char *File
= Get
.Path
.c_str();
410 Res
.Filename
= Itm
->DestFile
;
413 // Connect to the server
414 if (Server
== 0 || Server
->Comp(Get
) == false) {
416 Server
= new RSHConn(Get
);
419 // Could not connect is a transient error..
420 if (Server
->Open() == false) {
426 // We say this mainly because the pause here is for the
427 // ssh connection that is still going
428 Status(_("Connecting to %s"), Get
.Host
.c_str());
430 // Get the files information
432 if (Server
->Size(File
,Size
) == false ||
433 Server
->ModTime(File
,FailTime
) == false)
436 //_error->Error(_("File not found")); // Will be handled by Size
441 // See if it is an IMS hit
442 if (Itm
->LastModified
== FailTime
) {
449 // See if the file exists
451 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
452 if (Size
== (unsigned)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
453 Res
.Size
= Buf
.st_size
;
454 Res
.LastModified
= Buf
.st_mtime
;
455 Res
.ResumePoint
= Buf
.st_size
;
461 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned)Buf
.st_size
)
462 Res
.ResumePoint
= Buf
.st_size
;
468 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
469 if (_error
->PendingError() == true)
474 FailFile
= Itm
->DestFile
;
475 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
479 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
485 UBuf
.actime
= FailTime
;
486 UBuf
.modtime
= FailTime
;
487 utime(FailFile
.c_str(),&UBuf
);
489 // If the file is missing we hard fail otherwise transient fail
496 Res
.Size
= Fd
.Size();
499 Res
.LastModified
= FailTime
;
500 Res
.TakeHashes(Hash
);
504 UBuf
.actime
= FailTime
;
505 UBuf
.modtime
= FailTime
;
506 utime(Queue
->DestFile
.c_str(),&UBuf
);
515 int main(int argc
, const char *argv
[])
517 setlocale(LC_ALL
, "");
520 Prog
= strrchr(argv
[0],'/');