]>
git.saurik.com Git - apt.git/blob - cmdline/indexcopy.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: indexcopy.cc,v 1.6 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
6 Index Copying - Aid for copying and verifying the index files
8 This class helps apt-cache reconstruct a damaged index files.
10 ##################################################################### */
12 // Include Files /*{{{*/
13 #include "indexcopy.h"
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/progress.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/tagfile.h>
28 // IndexCopy::CopyPackages - Copy the package files from the CD /*{{{*/
29 // ---------------------------------------------------------------------
31 bool IndexCopy::CopyPackages(string CDROM
,string Name
,vector
<string
> &List
)
36 OpTextProgress Progress
;
38 bool NoStat
= _config
->FindB("APT::CDROM::Fast",false);
39 bool Debug
= _config
->FindB("Debug::aptcdrom",false);
41 // Prepare the progress indicator
42 unsigned long TotalSize
= 0;
43 for (vector
<string
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
46 if (stat(string(*I
+ GetFileName()).c_str(),&Buf
) != 0 &&
47 stat(string(*I
+ GetFileName() + ".gz").c_str(),&Buf
) != 0)
48 return _error
->Errno("stat","Stat failed for %s",
49 string(*I
+ GetFileName()).c_str());
50 TotalSize
+= Buf
.st_size
;
53 unsigned long CurrentSize
= 0;
54 unsigned int NotFound
= 0;
55 unsigned int WrongSize
= 0;
56 unsigned int Packages
= 0;
57 for (vector
<string
>::iterator I
= List
.begin(); I
!= List
.end(); I
++)
59 string OrigPath
= string(*I
,CDROM
.length());
60 unsigned long FileSize
= 0;
62 // Open the package file
64 if (FileExists(*I
+ GetFileName()) == true)
66 Pkg
.Open(*I
+ GetFileName(),FileFd::ReadOnly
);
67 FileSize
= Pkg
.Size();
71 FileFd
From(*I
+ GetFileName() + ".gz",FileFd::ReadOnly
);
72 if (_error
->PendingError() == true)
74 FileSize
= From
.Size();
77 FILE *tmp
= tmpfile();
79 return _error
->Errno("tmpfile","Unable to create a tmp file");
80 Pkg
.Fd(dup(fileno(tmp
)));
86 return _error
->Errno("fork","Couldn't fork gzip");
91 dup2(From
.Fd(),STDIN_FILENO
);
92 dup2(Pkg
.Fd(),STDOUT_FILENO
);
93 SetCloseExec(STDIN_FILENO
,false);
94 SetCloseExec(STDOUT_FILENO
,false);
97 Args
[0] = _config
->Find("Dir::bin::gzip","gzip").c_str();
100 execvp(Args
[0],(char **)Args
);
104 // Wait for gzip to finish
105 if (ExecWait(Process
,_config
->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
106 return _error
->Error("gzip failed, perhaps the disk is full.");
110 pkgTagFile
Parser(&Pkg
);
111 if (_error
->PendingError() == true)
114 // Open the output file
116 sprintf(S
,"cdrom:[%s]/%s%s",Name
.c_str(),(*I
).c_str() + CDROM
.length(),
118 string TargetF
= _config
->FindDir("Dir::State::lists") + "partial/";
119 TargetF
+= URItoFileName(S
);
120 if (_config
->FindB("APT::CDROM::NoAct",false) == true)
121 TargetF
= "/dev/null";
122 FileFd
Target(TargetF
,FileFd::WriteEmpty
);
123 FILE *TargetFl
= fdopen(dup(Target
.Fd()),"w");
124 if (_error
->PendingError() == true)
127 return _error
->Errno("fdopen","Failed to reopen fd");
129 // Setup the progress meter
130 Progress
.OverallProgress(CurrentSize
,TotalSize
,FileSize
,
131 string("Reading ") + Type() + " Indexes");
134 Progress
.SubProgress(Pkg
.Size());
135 pkgTagSection Section
;
136 this->Section
= &Section
;
138 unsigned long Hits
= 0;
139 unsigned long Chop
= 0;
140 while (Parser
.Step(Section
) == true)
142 Progress
.Progress(Parser
.Offset());
145 if (GetFile(File
,Size
) == false)
152 File
= OrigPath
+ ChopDirs(File
,Chop
);
154 // See if the file exists
155 bool Mangled
= false;
156 if (NoStat
== false || Hits
< 10)
158 // Attempt to fix broken structure
161 if (ReconstructPrefix(Prefix
,OrigPath
,CDROM
,File
) == false &&
162 ReconstructChop(Chop
,*I
,File
) == false)
165 clog
<< "Missed: " << File
<< endl
;
170 File
= OrigPath
+ ChopDirs(File
,Chop
);
175 if (stat(string(CDROM
+ Prefix
+ File
).c_str(),&Buf
) != 0 ||
178 // Attempt to fix busted symlink support for one instance
179 string OrigFile
= File
;
180 string::size_type Start
= File
.find("binary-");
181 string::size_type End
= File
.find("/",Start
+3);
182 if (Start
!= string::npos
&& End
!= string::npos
)
184 File
.replace(Start
,End
-Start
,"binary-all");
188 if (Mangled
== false ||
189 stat(string(CDROM
+ Prefix
+ File
).c_str(),&Buf
) != 0)
192 clog
<< "Missed(2): " << OrigFile
<< endl
;
199 if ((unsigned)Buf
.st_size
!= Size
)
202 clog
<< "Wrong Size: " << File
<< endl
;
211 if (RewriteEntry(TargetFl
,File
) == false)
220 cout
<< " Processed by using Prefix '" << Prefix
<< "' and chop " << Chop
<< endl
;
222 if (_config
->FindB("APT::CDROM::NoAct",false) == false)
224 // Move out of the partial directory
226 string FinalF
= _config
->FindDir("Dir::State::lists");
227 FinalF
+= URItoFileName(S
);
228 if (rename(TargetF
.c_str(),FinalF
.c_str()) != 0)
229 return _error
->Errno("rename","Failed to rename");
231 // Copy the release file
232 sprintf(S
,"cdrom:[%s]/%sRelease",Name
.c_str(),(*I
).c_str() + CDROM
.length());
233 string TargetF
= _config
->FindDir("Dir::State::lists") + "partial/";
234 TargetF
+= URItoFileName(S
);
235 if (FileExists(*I
+ "Release") == true)
237 FileFd
Target(TargetF
,FileFd::WriteEmpty
);
238 FileFd
Rel(*I
+ "Release",FileFd::ReadOnly
);
239 if (_error
->PendingError() == true)
242 if (CopyFile(Rel
,Target
) == false)
247 // Empty release file
248 FileFd
Target(TargetF
,FileFd::WriteEmpty
);
251 // Rename the release file
252 FinalF
= _config
->FindDir("Dir::State::lists");
253 FinalF
+= URItoFileName(S
);
254 if (rename(TargetF
.c_str(),FinalF
.c_str()) != 0)
255 return _error
->Errno("rename","Failed to rename");
258 /* Mangle the source to be in the proper notation with
259 prefix dist [component] */
260 *I
= string(*I
,Prefix
.length());
261 ConvertToSourceList(CDROM
,*I
);
262 *I
= Prefix
+ ' ' + *I
;
264 CurrentSize
+= FileSize
;
269 cout
<< "Wrote " << Packages
<< " records" ;
271 cout
<< " with " << NotFound
<< " missing files";
272 if (NotFound
!= 0 && WrongSize
!= 0)
275 cout
<< " with " << WrongSize
<< " mismatched files";
279 return _error
->Warning("No valid records were found.");
281 if (NotFound
+ WrongSize
> 10)
282 cout
<< "Alot of entries were discarded, something may be wrong." << endl
;
287 // IndexCopy::ChopDirs - Chop off the leading directory components /*{{{*/
288 // ---------------------------------------------------------------------
290 string
IndexCopy::ChopDirs(string Path
,unsigned int Depth
)
292 string::size_type I
= 0;
295 I
= Path
.find('/',I
+1);
298 while (I
!= string::npos
&& Depth
!= 0);
300 if (I
== string::npos
)
303 return string(Path
,I
+1);
306 // IndexCopy::ReconstructPrefix - Fix strange prefixing /*{{{*/
307 // ---------------------------------------------------------------------
308 /* This prepends dir components from the path to the package files to
309 the path to the deb until it is found */
310 bool IndexCopy::ReconstructPrefix(string
&Prefix
,string OrigPath
,string CD
,
313 bool Debug
= _config
->FindB("Debug::aptcdrom",false);
314 unsigned int Depth
= 1;
315 string MyPrefix
= Prefix
;
319 if (stat(string(CD
+ MyPrefix
+ File
).c_str(),&Buf
) != 0)
322 cout
<< "Failed, " << CD
+ MyPrefix
+ File
<< endl
;
323 if (GrabFirst(OrigPath
,MyPrefix
,Depth
++) == true)
337 // IndexCopy::ReconstructChop - Fixes bad source paths /*{{{*/
338 // ---------------------------------------------------------------------
339 /* This removes path components from the filename and prepends the location
340 of the package files until a file is found */
341 bool IndexCopy::ReconstructChop(unsigned long &Chop
,string Dir
,string File
)
343 // Attempt to reconstruct the filename
344 unsigned long Depth
= 0;
348 if (stat(string(Dir
+ File
).c_str(),&Buf
) != 0)
350 File
= ChopDirs(File
,1);
352 if (File
.empty() == false)
365 // IndexCopy::ConvertToSourceList - Convert a Path to a sourcelist /*{{{*/
366 // ---------------------------------------------------------------------
367 /* We look for things in dists/ notation and convert them to
368 <dist> <component> form otherwise it is left alone. This also strips
371 This implements a regex sort of like:
372 (.*)/dists/([^/]*)/(.*)/binary-*
374 | |-------- Distribution
375 |------------------- Path
377 It was deciced to use only a single word for dist (rather than say
378 unstable/non-us) to increase the chance that each CD gets a single
379 line in sources.list.
381 void IndexCopy::ConvertToSourceList(string CD
,string
&Path
)
384 sprintf(S
,"binary-%s",_config
->Find("Apt::Architecture").c_str());
386 // Strip the cdrom base path
387 Path
= string(Path
,CD
.length());
388 if (Path
.empty() == true)
391 // Too short to be a dists/ type
392 if (Path
.length() < strlen("dists/"))
396 if (stringcmp(Path
.begin(),Path
.begin()+strlen("dists/"),"dists/") != 0)
400 string::size_type Slash
= strlen("dists/");
401 string::size_type Slash2
= Path
.find('/',Slash
+ 1);
402 if (Slash2
== string::npos
|| Slash2
+ 2 >= Path
.length())
404 string Dist
= string(Path
,Slash
,Slash2
- Slash
);
406 // Isolate the component
408 for (unsigned I
= 0; I
!= 10; I
++)
410 Slash
= Path
.find('/',Slash
+1);
411 if (Slash
== string::npos
|| Slash
+ 2 >= Path
.length())
413 string Comp
= string(Path
,Slash2
+1,Slash
- Slash2
-1);
415 // Verify the trailing binary- bit
416 string::size_type BinSlash
= Path
.find('/',Slash
+ 1);
417 if (Slash
== string::npos
)
419 string Binary
= string(Path
,Slash
+1,BinSlash
- Slash
-1);
421 if (Binary
!= S
&& Binary
!= "source")
424 Path
= Dist
+ ' ' + Comp
;
429 // IndexCopy::GrabFirst - Return the first Depth path components /*{{{*/
430 // ---------------------------------------------------------------------
432 bool IndexCopy::GrabFirst(string Path
,string
&To
,unsigned int Depth
)
434 string::size_type I
= 0;
437 I
= Path
.find('/',I
+1);
440 while (I
!= string::npos
&& Depth
!= 0);
442 if (I
== string::npos
)
445 To
= string(Path
,0,I
+1);
449 // PackageCopy::GetFile - Get the file information from the section /*{{{*/
450 // ---------------------------------------------------------------------
452 bool PackageCopy::GetFile(string
&File
,unsigned long &Size
)
454 File
= Section
->FindS("Filename");
455 Size
= Section
->FindI("Size");
456 if (File
.empty() || Size
== 0)
457 return _error
->Error("Cannot find filename or size tag");
461 // PackageCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/
462 // ---------------------------------------------------------------------
464 bool PackageCopy::RewriteEntry(FILE *Target
,string File
)
466 TFRewriteData Changes
[] = {{"Filename",File
.c_str()},
469 if (TFRewrite(Target
,*Section
,TFRewritePackageOrder
,Changes
) == false)
475 // SourceCopy::GetFile - Get the file information from the section /*{{{*/
476 // ---------------------------------------------------------------------
478 bool SourceCopy::GetFile(string
&File
,unsigned long &Size
)
480 string Files
= Section
->FindS("Files");
481 if (Files
.empty() == true)
484 // Stash the / terminated directory prefix
485 string Base
= Section
->FindS("Directory");
486 if (Base
.empty() == false && Base
[Base
.length()-1] != '/')
489 // Read the first file triplet
490 const char *C
= Files
.c_str();
494 // Parse each of the elements
495 if (ParseQuoteWord(C
,MD5Hash
) == false ||
496 ParseQuoteWord(C
,sSize
) == false ||
497 ParseQuoteWord(C
,File
) == false)
498 return _error
->Error("Error parsing file record");
500 // Parse the size and append the directory
501 Size
= atoi(sSize
.c_str());
506 // SourceCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/
507 // ---------------------------------------------------------------------
509 bool SourceCopy::RewriteEntry(FILE *Target
,string File
)
511 string
Dir(File
,0,File
.rfind('/'));
512 TFRewriteData Changes
[] = {{"Directory",Dir
.c_str()},
515 if (TFRewrite(Target
,*Section
,TFRewriteSourceOrder
,Changes
) == false)