]>
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) {}
47 // RSHConn::RSHConn - Destructor /*{{{*/
48 // ---------------------------------------------------------------------
55 // RSHConn::Close - Forcibly terminate the connection /*{{{*/
56 // ---------------------------------------------------------------------
57 /* Often this is called when things have gone wrong to indicate that the
58 connection is no longer usable. */
67 ExecWait(Process
,"",true);
73 // RSHConn::Open - Connect to a host /*{{{*/
74 // ---------------------------------------------------------------------
78 // Use the already open connection if possible.
82 if (Connect(ServerName
.Host
,ServerName
.User
) == false)
88 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
89 // ---------------------------------------------------------------------
91 bool RSHConn::Connect(std::string Host
, std::string User
)
94 int Pipes
[4] = {-1,-1,-1,-1};
95 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
97 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
98 for (int I
= 0; I
!= 4; I
++)
102 for (int I
= 0; I
!= 4; I
++)
103 SetCloseExec(Pipes
[I
],true);
105 Process
= ExecFork();
110 const char *Args
[400];
113 dup2(Pipes
[1],STDOUT_FILENO
);
114 dup2(Pipes
[2],STDIN_FILENO
);
116 // Probably should do
117 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
121 // Insert user-supplied command line options
122 Configuration::Item
const *Opts
= RshOptions
;
126 for (; Opts
!= 0; Opts
= Opts
->Next
)
128 if (Opts
->Value
.empty() == true)
130 Args
[i
++] = Opts
->Value
.c_str();
134 if (User
.empty() == false) {
136 Args
[i
++] = User
.c_str();
138 if (Host
.empty() == false) {
139 Args
[i
++] = Host
.c_str();
141 Args
[i
++] = "/bin/sh";
143 execvp(Args
[0],(char **)Args
);
149 SetNonBlock(Pipes
[0],true);
150 SetNonBlock(Pipes
[3],true);
157 // RSHConn::ReadLine - Very simple buffered read with timeout /*{{{*/
158 // ---------------------------------------------------------------------
160 bool RSHConn::ReadLine(std::string
&Text
)
162 if (Process
== -1 || ReadFd
== -1)
166 while (Len
< sizeof(Buffer
))
168 // Scan the buffer for a new line
169 for (unsigned int I
= 0; I
!= Len
; I
++)
171 // Escape some special chars
176 if (Buffer
[I
] != '\n')
180 Text
= std::string(Buffer
,I
);
181 memmove(Buffer
,Buffer
+I
,Len
- I
);
186 // Wait for some data..
187 if (WaitFd(ReadFd
,false,TimeOut
) == false)
190 return _error
->Error(_("Connection timeout"));
194 int Res
= read(ReadFd
,Buffer
+ Len
,sizeof(Buffer
) - Len
);
197 _error
->Errno("read",_("Read error"));
204 return _error
->Error(_("A response overflowed the buffer."));
207 // RSHConn::WriteMsg - Send a message with optional remote sync. /*{{{*/
208 // ---------------------------------------------------------------------
209 /* The remote sync flag appends a || echo which will insert blank line
210 once the command completes. */
211 bool RSHConn::WriteMsg(std::string
&Text
,bool Sync
,const char *Fmt
,...)
216 // sprintf the description
218 vsnprintf(S
,sizeof(S
) - 4,Fmt
,args
);
220 strcat(S
," 2> /dev/null || echo\n");
222 strcat(S
," 2> /dev/null\n");
225 unsigned long Len
= strlen(S
);
226 unsigned long Start
= 0;
229 if (WaitFd(WriteFd
,true,TimeOut
) == false)
233 return _error
->Error(_("Connection timeout"));
236 int Res
= write(WriteFd
,S
+ Start
,Len
);
239 _error
->Errno("write",_("Write error"));
249 return ReadLine(Text
);
253 // RSHConn::Size - Return the size of the file /*{{{*/
254 // ---------------------------------------------------------------------
255 /* Right now for successfull transfer the file size must be known in
257 bool RSHConn::Size(const char *Path
,unsigned long long &Size
)
263 if (WriteMsg(Msg
,true,"find %s -follow -printf '%%s\\n'",Path
) == false)
266 // FIXME: Sense if the bad reply is due to a File Not Found.
269 Size
= strtoull(Msg
.c_str(),&End
,10);
270 if (End
== Msg
.c_str())
271 return _error
->Error(_("File not found"));
275 // RSHConn::ModTime - Get the modification time in UTC /*{{{*/
276 // ---------------------------------------------------------------------
278 bool RSHConn::ModTime(const char *Path
, time_t &Time
)
281 // Query the mod time
284 if (WriteMsg(Msg
,true,"TZ=UTC find %s -follow -printf '%%TY%%Tm%%Td%%TH%%TM%%TS\\n'",Path
) == false)
288 return FTPMDTMStrToTime(Msg
.c_str(), Time
);
291 // RSHConn::Get - Get a file /*{{{*/
292 // ---------------------------------------------------------------------
294 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long long Resume
,
295 Hashes
&Hash
,bool &Missing
, unsigned long long Size
)
299 // Round to a 2048 byte block
300 Resume
= Resume
- (Resume
% 2048);
302 if (To
.Truncate(Resume
) == false)
304 if (To
.Seek(0) == false)
308 if (Hash
.AddFD(To
.Fd(),Resume
) == false) {
309 _error
->Errno("read",_("Problem hashing file"));
314 // FIXME: Detect file-not openable type errors.
316 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
320 unsigned long long MyLen
= Resume
;
321 unsigned char Buffer
[4096];
324 // Wait for some data..
325 if (WaitFd(ReadFd
,false,TimeOut
) == false)
328 return _error
->Error(_("Data socket timed out"));
332 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
336 return _error
->Error(_("Connection closed prematurely"));
347 Hash
.Add(Buffer
,Res
);
348 if (To
.Write(Buffer
,Res
) == false)
359 // RSHMethod::RSHMethod - Constructor /*{{{*/
360 // ---------------------------------------------------------------------
362 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig
)
364 signal(SIGTERM
,SigTerm
);
365 signal(SIGINT
,SigTerm
);
370 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
371 // ---------------------------------------------------------------------
372 bool RSHMethod::Configuration(std::string Message
)
376 if (pkgAcqMethod::Configuration(Message
) == false)
379 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Timeout", Prog
);
380 TimeOut
= _config
->FindI(ProgStr
,TimeOut
);
381 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Options", Prog
);
382 RshOptions
= _config
->Tree(ProgStr
);
387 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
388 // ---------------------------------------------------------------------
390 void RSHMethod::SigTerm(int sig
)
398 UBuf
.actime
= FailTime
;
399 UBuf
.modtime
= FailTime
;
400 utime(FailFile
.c_str(),&UBuf
);
405 // RSHMethod::Fetch - Fetch a URI /*{{{*/
406 // ---------------------------------------------------------------------
408 bool RSHMethod::Fetch(FetchItem
*Itm
)
411 const char *File
= Get
.Path
.c_str();
413 Res
.Filename
= Itm
->DestFile
;
416 // Connect to the server
417 if (Server
== 0 || Server
->Comp(Get
) == false) {
419 Server
= new RSHConn(Get
);
422 // Could not connect is a transient error..
423 if (Server
->Open() == false) {
429 // We say this mainly because the pause here is for the
430 // ssh connection that is still going
431 Status(_("Connecting to %s"), Get
.Host
.c_str());
433 // Get the files information
434 unsigned long long Size
;
435 if (Server
->Size(File
,Size
) == false ||
436 Server
->ModTime(File
,FailTime
) == false)
439 //_error->Error(_("File not found")); // Will be handled by Size
444 // See if it is an IMS hit
445 if (Itm
->LastModified
== FailTime
) {
452 // See if the file exists
454 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
455 if (Size
== (unsigned long long)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
456 Res
.Size
= Buf
.st_size
;
457 Res
.LastModified
= Buf
.st_mtime
;
458 Res
.ResumePoint
= Buf
.st_size
;
464 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned long long)Buf
.st_size
)
465 Res
.ResumePoint
= Buf
.st_size
;
471 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
472 if (_error
->PendingError() == true)
477 FailFile
= Itm
->DestFile
;
478 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
482 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
488 UBuf
.actime
= FailTime
;
489 UBuf
.modtime
= FailTime
;
490 utime(FailFile
.c_str(),&UBuf
);
492 // If the file is missing we hard fail otherwise transient fail
499 Res
.Size
= Fd
.Size();
502 Res
.LastModified
= FailTime
;
503 Res
.TakeHashes(Hash
);
507 UBuf
.actime
= FailTime
;
508 UBuf
.modtime
= FailTime
;
509 utime(Queue
->DestFile
.c_str(),&UBuf
);
518 int main(int argc
, const char *argv
[])
520 setlocale(LC_ALL
, "");
523 Prog
= strrchr(argv
[0],'/');