]> git.saurik.com Git - apt.git/blobdiff - cmdline/indexcopy.cc
Core correctness patches
[apt.git] / cmdline / indexcopy.cc
index a8243c085f4462f0fe172e26ca328db102edc965..73d9e507714363e5e3a0b6d18a29426d167883ba 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: indexcopy.cc,v 1.5 2000/05/10 06:02:26 jgg Exp $
+// $Id: indexcopy.cc,v 1.7 2001/03/13 05:23:42 jgg Exp $
 /* ######################################################################
 
    Index Copying - Aid for copying and verifying the index files
@@ -107,21 +107,24 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List)
         
         Pkg.Seek(0);
       }
-      pkgTagFile Parser(Pkg);
+      pkgTagFile Parser(&Pkg);
       if (_error->PendingError() == true)
         return false;
       
       // Open the output file
       char S[400];
-      sprintf(S,"cdrom:[%s]/%s%s",Name.c_str(),(*I).c_str() + CDROM.length(),
-             GetFileName());
+      snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",Name.c_str(),
+              (*I).c_str() + CDROM.length(),GetFileName());
       string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
       TargetF += URItoFileName(S);
       if (_config->FindB("APT::CDROM::NoAct",false) == true)
         TargetF = "/dev/null";
-      FileFd Target(TargetF,FileFd::WriteEmpty);      
+      FileFd Target(TargetF,FileFd::WriteEmpty);
+      FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
       if (_error->PendingError() == true)
         return false;
+      if (TargetFl == 0)
+        return _error->Errno("fdopen","Failed to reopen fd");
       
       // Setup the progress meter
       Progress.OverallProgress(CurrentSize,TotalSize,FileSize,
@@ -140,7 +143,10 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List)
         string File;
         unsigned long Size;
         if (GetFile(File,Size) == false)
+        {
+           fclose(TargetFl);
            return false;
+        }
         
         if (Chop != 0)
            File = OrigPath + ChopDirs(File,Chop);
@@ -202,21 +208,13 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List)
         Packages++;
         Hits++;
         
-        // Copy it to the target package file
-        if (Chop != 0 || Mangled == true)
+        if (RewriteEntry(TargetFl,File) == false)
         {
-           if (RewriteEntry(Target,File) == false)
-              continue;
+           fclose(TargetFl);
+           return false;
         }
-        else
-        {
-           const char *Start;
-           const char *Stop;
-           Section.GetSection(Start,Stop);
-           if (Target.Write(Start,Stop-Start) == false)
-              return false;
-        }       
       }
+      fclose(TargetFl);
 
       if (Debug == true)
         cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl;
@@ -231,7 +229,8 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List)
            return _error->Errno("rename","Failed to rename");
 
         // Copy the release file
-        sprintf(S,"cdrom:[%s]/%sRelease",Name.c_str(),(*I).c_str() + CDROM.length());
+        snprintf(S,sizeof(S),"cdrom:[%s]/%sRelease",Name.c_str(),
+                 (*I).c_str() + CDROM.length());
         string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
         TargetF += URItoFileName(S);
         if (FileExists(*I + "Release") == true)
@@ -383,7 +382,7 @@ bool IndexCopy::ReconstructChop(unsigned long &Chop,string Dir,string File)
 void IndexCopy::ConvertToSourceList(string CD,string &Path)
 {
    char S[300];
-   sprintf(S,"binary-%s",_config->Find("Apt::Architecture").c_str());
+   snprintf(S,sizeof(S),"binary-%s",_config->Find("Apt::Architecture").c_str());
    
    // Strip the cdrom base path
    Path = string(Path,CD.length());
@@ -448,44 +447,6 @@ bool IndexCopy::GrabFirst(string Path,string &To,unsigned int Depth)
    return true;
 }
                                                                        /*}}}*/
-// IndexCopy::CopyWithReplace - Copy a section and replace text                /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool IndexCopy::CopyWithReplace(FileFd &Target,const char *Tag,string New)
-{
-   // Mangle the output filename
-   const char *Start;
-   const char *Stop;
-   const char *Filename;
-   Section->Find(Tag,Filename,Stop);
-   
-   /* We need to rewrite the filename field so we emit
-      all fields except the filename file and rewrite that one */
-   for (unsigned int I = 0; I != Section->Count(); I++)
-   {
-      Section->Get(Start,Stop,I);
-      if (Start <= Filename && Stop > Filename)
-      {
-        char S[500];
-        sprintf(S,"%s: %s\n",Tag,New.c_str());
-        if (I + 1 == Section->Count())
-           strcat(S,"\n");
-        if (Target.Write(S,strlen(S)) == false)
-           return false;
-      }
-      else
-      {
-        if (Target.Write(Start,Stop-Start) == false)
-           return false;
-        if (Stop[-1] != '\n')
-           if (Target.Write("\n",1) == false)
-              return false;
-      }               
-   }
-   if (Target.Write("\n",1) == false)
-      return false;
-}
-                                                                       /*}}}*/
 // PackageCopy::GetFile - Get the file information from the section    /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -501,9 +462,15 @@ bool PackageCopy::GetFile(string &File,unsigned long &Size)
 // PackageCopy::RewriteEntry - Rewrite the entry with a new filename   /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool PackageCopy::RewriteEntry(FileFd &Target,string File)
+bool PackageCopy::RewriteEntry(FILE *Target,string File)
 {
-   return CopyWithReplace(Target,"Filename",File);
+   TFRewriteData Changes[] = {{"Filename",File.c_str()},
+                              {}};
+   
+   if (TFRewrite(Target,*Section,TFRewritePackageOrder,Changes) == false)
+      return false;
+   fputc('\n',Target);
+   return true;
 }
                                                                        /*}}}*/
 // SourceCopy::GetFile - Get the file information from the section     /*{{{*/
@@ -520,7 +487,7 @@ bool SourceCopy::GetFile(string &File,unsigned long &Size)
    if (Base.empty() == false && Base[Base.length()-1] != '/')
       Base += '/';
    
-   // Iterate over the entire list grabbing each triplet
+   // Read the first file triplet
    const char *C = Files.c_str();
    string sSize;
    string MD5Hash;
@@ -540,9 +507,15 @@ bool SourceCopy::GetFile(string &File,unsigned long &Size)
 // SourceCopy::RewriteEntry - Rewrite the entry with a new filename    /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool SourceCopy::RewriteEntry(FileFd &Target,string File)
+bool SourceCopy::RewriteEntry(FILE *Target,string File)
 {
-   return CopyWithReplace(Target,"Directory",
-                         string(File,0,File.rfind('/')));
+   string Dir(File,0,File.rfind('/'));
+   TFRewriteData Changes[] = {{"Directory",Dir.c_str()},
+                              {}};
+   
+   if (TFRewrite(Target,*Section,TFRewriteSourceOrder,Changes) == false)
+      return false;
+   fputc('\n',Target);
+   return true;
 }
                                                                        /*}}}*/