]>
git.saurik.com Git - apt-legacy.git/blob - methods/rsh.cc
2 #include <mach-o/nlist.h>
5 // -*- mode: cpp; mode: fold -*-
7 // $Id: rsh.cc,v 1.6.2.1 2004/01/16 18:58:50 mdz Exp $
8 /* ######################################################################
10 RSH method - Transfer files via rsh compatible program
12 Written by Ben Collins <bcollins@debian.org>, Copyright (c) 2000
13 Licensed under the GNU General Public License v2 [no exception clauses]
15 ##################################################################### */
17 // Include Files /*{{{*/
19 #include <apt-pkg/error.h>
33 unsigned long TimeOut
= 120;
34 Configuration::Item
const *RshOptions
= 0;
35 time_t RSHMethod::FailTime
= 0;
36 string
RSHMethod::FailFile
;
37 int RSHMethod::FailFd
= -1;
39 // RSHConn::RSHConn - Constructor /*{{{*/
40 // ---------------------------------------------------------------------
42 RSHConn::RSHConn(URI Srv
) : Len(0), WriteFd(-1), ReadFd(-1),
43 ServerName(Srv
), Process(-1) {}
45 // RSHConn::RSHConn - Destructor /*{{{*/
46 // ---------------------------------------------------------------------
53 // RSHConn::Close - Forcibly terminate the connection /*{{{*/
54 // ---------------------------------------------------------------------
55 /* Often this is called when things have gone wrong to indicate that the
56 connection is no longer usable. */
65 ExecWait(Process
,"",true);
71 // RSHConn::Open - Connect to a host /*{{{*/
72 // ---------------------------------------------------------------------
76 // Use the already open connection if possible.
80 if (Connect(ServerName
.Host
,ServerName
.User
) == false)
86 // RSHConn::Connect - Fire up rsh and connect /*{{{*/
87 // ---------------------------------------------------------------------
89 bool RSHConn::Connect(string Host
, string User
)
92 int Pipes
[4] = {-1,-1,-1,-1};
93 if (pipe(Pipes
) != 0 || pipe(Pipes
+2) != 0)
95 _error
->Errno("pipe",_("Failed to create IPC pipe to subprocess"));
96 for (int I
= 0; I
!= 4; I
++)
100 for (int I
= 0; I
!= 4; I
++)
101 SetCloseExec(Pipes
[I
],true);
103 Process
= ExecFork();
108 const char *Args
[400];
111 dup2(Pipes
[1],STDOUT_FILENO
);
112 dup2(Pipes
[2],STDIN_FILENO
);
114 // Probably should do
115 // dup2(open("/dev/null",O_RDONLY),STDERR_FILENO);
117 // Insert user-supplied command line options
118 Configuration::Item
const *Opts
= RshOptions
;
122 for (; Opts
!= 0; Opts
= Opts
->Next
)
124 if (Opts
->Value
.empty() == true)
126 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)
289 // RSHConn::Get - Get a file /*{{{*/
290 // ---------------------------------------------------------------------
292 bool RSHConn::Get(const char *Path
,FileFd
&To
,unsigned long Resume
,
293 Hashes
&Hash
,bool &Missing
, unsigned long Size
)
297 // Round to a 2048 byte block
298 Resume
= Resume
- (Resume
% 2048);
300 if (To
.Truncate(Resume
) == false)
302 if (To
.Seek(0) == false)
306 if (Hash
.AddFD(To
.Fd(),Resume
) == false) {
307 _error
->Errno("read",_("Problem hashing file"));
312 // FIXME: Detect file-not openable type errors.
314 if (WriteMsg(Jnk
,false,"dd if=%s bs=2048 skip=%u", Path
, Resume
/ 2048) == false)
318 unsigned int MyLen
= Resume
;
319 unsigned char Buffer
[4096];
322 // Wait for some data..
323 if (WaitFd(ReadFd
,false,TimeOut
) == false)
326 return _error
->Error(_("Data socket timed out"));
330 int Res
= read(ReadFd
,Buffer
,sizeof(Buffer
));
334 return _error
->Error(_("Connection closed prematurely"));
345 Hash
.Add(Buffer
,Res
);
346 if (To
.Write(Buffer
,Res
) == false)
357 // RSHMethod::RSHMethod - Constructor /*{{{*/
358 // ---------------------------------------------------------------------
360 RSHMethod::RSHMethod() : pkgAcqMethod("1.0",SendConfig
)
362 signal(SIGTERM
,SigTerm
);
363 signal(SIGINT
,SigTerm
);
368 // RSHMethod::Configuration - Handle a configuration message /*{{{*/
369 // ---------------------------------------------------------------------
370 bool RSHMethod::Configuration(string Message
)
374 if (pkgAcqMethod::Configuration(Message
) == false)
377 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Timeout", Prog
);
378 TimeOut
= _config
->FindI(ProgStr
,TimeOut
);
379 snprintf(ProgStr
, sizeof ProgStr
, "Acquire::%s::Options", Prog
);
380 RshOptions
= _config
->Tree(ProgStr
);
385 // RSHMethod::SigTerm - Clean up and timestamp the files on exit /*{{{*/
386 // ---------------------------------------------------------------------
388 void RSHMethod::SigTerm(int sig
)
396 UBuf
.actime
= FailTime
;
397 UBuf
.modtime
= FailTime
;
398 utime(FailFile
.c_str(),&UBuf
);
403 // RSHMethod::Fetch - Fetch a URI /*{{{*/
404 // ---------------------------------------------------------------------
406 bool RSHMethod::Fetch(FetchItem
*Itm
)
409 const char *File
= Get
.Path
.c_str();
411 Res
.Filename
= Itm
->DestFile
;
414 // Connect to the server
415 if (Server
== 0 || Server
->Comp(Get
) == false) {
417 Server
= new RSHConn(Get
);
420 // Could not connect is a transient error..
421 if (Server
->Open() == false) {
427 // We say this mainly because the pause here is for the
428 // ssh connection that is still going
429 Status(_("Connecting to %s"), Get
.Host
.c_str());
431 // Get the files information
433 if (Server
->Size(File
,Size
) == false ||
434 Server
->ModTime(File
,FailTime
) == false)
437 //_error->Error(_("File not found")); // Will be handled by Size
442 // See if it is an IMS hit
443 if (Itm
->LastModified
== FailTime
) {
450 // See if the file exists
452 if (stat(Itm
->DestFile
.c_str(),&Buf
) == 0) {
453 if (Size
== (unsigned)Buf
.st_size
&& FailTime
== Buf
.st_mtime
) {
454 Res
.Size
= Buf
.st_size
;
455 Res
.LastModified
= Buf
.st_mtime
;
456 Res
.ResumePoint
= Buf
.st_size
;
462 if (FailTime
== Buf
.st_mtime
&& Size
> (unsigned)Buf
.st_size
)
463 Res
.ResumePoint
= Buf
.st_size
;
469 FileFd
Fd(Itm
->DestFile
,FileFd::WriteAny
);
470 if (_error
->PendingError() == true)
475 FailFile
= Itm
->DestFile
;
476 FailFile
.c_str(); // Make sure we dont do a malloc in the signal handler
480 if (Server
->Get(File
,Fd
,Res
.ResumePoint
,Hash
,Missing
,Res
.Size
) == false)
486 UBuf
.actime
= FailTime
;
487 UBuf
.modtime
= FailTime
;
488 utime(FailFile
.c_str(),&UBuf
);
490 // If the file is missing we hard fail otherwise transient fail
497 Res
.Size
= Fd
.Size();
500 Res
.LastModified
= FailTime
;
501 Res
.TakeHashes(Hash
);
505 UBuf
.actime
= FailTime
;
506 UBuf
.modtime
= FailTime
;
507 utime(Queue
->DestFile
.c_str(),&UBuf
);
516 int main(int argc
, const char *argv
[])
518 #if !defined(__ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__) || __ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__ < 10200
520 memset(nl
, 0, sizeof(nl
));
521 nl
[0].n_un
.n_name
= (char *) "_useMDNSResponder";
522 nlist("/usr/lib/libc.dylib", nl
);
523 if (nl
[0].n_type
!= N_UNDF
)
524 *(int *) nl
[0].n_value
= 0;
527 setlocale(LC_ALL
, "");
530 Prog
= strrchr(argv
[0],'/');