]>
git.saurik.com Git - apt.git/blob - methods/gzip.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: gzip.cc,v 1.2 1998/10/26 07:11:53 jgg Exp $
4 /* ######################################################################
6 GZip method - Take a file URI in and decompress it into the target
9 ##################################################################### */
11 // Include Files /*{{{*/
12 #include <apt-pkg/fileutl.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/acquire-worker.h>
25 // Fail - Generate a failure message /*{{{*/
26 // ---------------------------------------------------------------------
30 string Err
= "Undetermined Error";
31 if (_error
->empty() == false)
32 _error
->PopMessage(Err
);
34 printf("400 URI Failure\n"
36 "Message: %s\n\n",URI
.c_str(),Err
.c_str());
44 SetNonBlock(STDIN_FILENO
,true);
46 printf("100 Capabilities\n"
49 "Send-Config: true\n\n");
51 vector
<string
> Messages
;
54 if (WaitFd(STDIN_FILENO
) == false ||
55 ReadMessages(STDIN_FILENO
,Messages
) == false)
58 while (Messages
.empty() == false)
60 string Message
= Messages
.front();
61 Messages
.erase(Messages
.begin());
63 // Fetch the message number
65 int Number
= strtol(Message
.c_str(),&End
,10);
66 if (End
== Message
.c_str())
68 cerr
<< "Malformed message!" << endl
;
72 // 601 configuration message
75 pkgInjectConfiguration(Message
,*_config
);
79 // 600 URI Fetch message
84 string URI
= LookupTag(Message
,"URI");
85 string Target
= LookupTag(Message
,"Filename");
88 string::size_type Pos
= URI
.find(':');
89 if (Pos
== string::npos
)
91 _error
->Error("Invalid message");
95 string File
= string(URI
,Pos
+1);
97 // Start the reply message
98 string Result
= "201 URI Done";
99 Result
+= "\nURI: " + URI
;
100 Result
+= "\nFileName: " + Target
;
102 // See if the file exists
103 FileFd
From(File
,FileFd::ReadOnly
);
104 FileFd
To(Target
,FileFd::WriteEmpty
);
106 if (_error
->PendingError() == true)
113 int Process
= fork();
116 _error
->Errno("fork","Couldn't fork gzip");
124 dup2(From
.Fd(),STDIN_FILENO
);
125 dup2(To
.Fd(),STDOUT_FILENO
);
128 SetCloseExec(STDIN_FILENO
,false);
129 SetCloseExec(STDOUT_FILENO
,false);
132 Args
[0] = _config
->Find("Dir::bin::gzip","gzip").c_str();
135 execvp(Args
[0],(char **)Args
);
140 // Wait for gzip to finish
142 if (waitpid(Process
,&Status
,0) != Process
)
145 _error
->Errno("wait","Waiting for gzip failed");
150 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
153 _error
->Error("gzip failed, perhaps the disk is full or the directory permissions are wrong.");
160 // Transfer the modification times
162 if (stat(File
.c_str(),&Buf
) != 0)
164 _error
->Errno("stat","Failed to stat");
168 struct utimbuf TimeBuf
;
169 TimeBuf
.actime
= Buf
.st_atime
;
170 TimeBuf
.modtime
= Buf
.st_mtime
;
171 if (utime(Target
.c_str(),&TimeBuf
) != 0)
173 _error
->Errno("utime","Failed to set modification time");
180 if (write(STDOUT_FILENO
,Result
.begin(),Result
.length()) !=
181 (signed)Result
.length())