]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/debindexfile.cc
stop depending on copy-on-write for std::string
[apt.git] / apt-pkg / deb / debindexfile.cc
index cecfe41a9d477759ba2df6cea8bee44e1ad1e89b..d672b4fd86afa7dd59c8aeeaf711c03256860cf6 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <stdio.h>
 #include <iostream>
 
 #include <stdio.h>
 #include <iostream>
+#include <sstream>
 #include <string>
 #include <sys/stat.h>
                                                                        /*}}}*/
 #include <string>
 #include <sys/stat.h>
                                                                        /*}}}*/
@@ -412,8 +413,8 @@ pkgCache::PkgFileIterator debPackagesIndex::FindInCache(pkgCache &Cache) const
 // TranslationsIndex::debTranslationsIndex - Contructor                        /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // TranslationsIndex::debTranslationsIndex - Contructor                        /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-debTranslationsIndex::debTranslationsIndex(string URI,string Dist,string Section,
-                                               char const * const Translation) :
+debTranslationsIndex::debTranslationsIndex(std::string const &URI, std::string const &Dist,
+                                               std::string const &Section, std::string const &Translation) :
                        pkgIndexFile(true), URI(URI), Dist(Dist), Section(Section),
                                Language(Translation)
 {}
                        pkgIndexFile(true), URI(URI), Dist(Dist), Section(Section),
                                Language(Translation)
 {}
@@ -457,13 +458,13 @@ string debTranslationsIndex::IndexURI(const char *Type) const
 /* This should help the user find the index in the sources.list and
    in the filesystem for problem solving */
 string debTranslationsIndex::Describe(bool Short) const
 /* This should help the user find the index in the sources.list and
    in the filesystem for problem solving */
 string debTranslationsIndex::Describe(bool Short) const
-{   
-   char S[300];
+{
+   std::string S;
    if (Short == true)
    if (Short == true)
-      snprintf(S,sizeof(S),"%s",Info(TranslationFile().c_str()).c_str());
+      strprintf(S,"%s",Info(TranslationFile().c_str()).c_str());
    else
    else
-      snprintf(S,sizeof(S),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
-              IndexFile(Language).c_str());
+      strprintf(S,"%s (%s)",Info(TranslationFile().c_str()).c_str(),
+              IndexFile(Language.c_str()).c_str());
    return S;
 }
                                                                        /*}}}*/
    return S;
 }
                                                                        /*}}}*/
@@ -487,7 +488,7 @@ string debTranslationsIndex::Info(const char *Type) const
                                                                        /*}}}*/
 bool debTranslationsIndex::HasPackages() const                         /*{{{*/
 {
                                                                        /*}}}*/
 bool debTranslationsIndex::HasPackages() const                         /*{{{*/
 {
-   return FileExists(IndexFile(Language));
+   return FileExists(IndexFile(Language.c_str()));
 }
                                                                        /*}}}*/
 // TranslationsIndex::Exists - Check if the index is available         /*{{{*/
 }
                                                                        /*}}}*/
 // TranslationsIndex::Exists - Check if the index is available         /*{{{*/
@@ -495,7 +496,7 @@ bool debTranslationsIndex::HasPackages() const                              /*{{{*/
 /* */
 bool debTranslationsIndex::Exists() const
 {
 /* */
 bool debTranslationsIndex::Exists() const
 {
-   return FileExists(IndexFile(Language));
+   return FileExists(IndexFile(Language.c_str()));
 }
                                                                        /*}}}*/
 // TranslationsIndex::Size - Return the size of the index              /*{{{*/
 }
                                                                        /*}}}*/
 // TranslationsIndex::Size - Return the size of the index              /*{{{*/
@@ -508,7 +509,7 @@ unsigned long debTranslationsIndex::Size() const
    /* we need to ignore errors here; if the lists are absent, just return 0 */
    _error->PushToStack();
 
    /* we need to ignore errors here; if the lists are absent, just return 0 */
    _error->PushToStack();
 
-   FileFd f(IndexFile(Language), FileFd::ReadOnly, FileFd::Extension);
+   FileFd f(IndexFile(Language.c_str()), FileFd::ReadOnly, FileFd::Extension);
    if (!f.Failed())
       size = f.Size();
 
    if (!f.Failed())
       size = f.Size();
 
@@ -525,7 +526,7 @@ unsigned long debTranslationsIndex::Size() const
 bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
 {
    // Check the translation file, if in use
 bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
 {
    // Check the translation file, if in use
-   string TranslationFile = IndexFile(Language);
+   string TranslationFile = IndexFile(Language.c_str());
    if (FileExists(TranslationFile))
    {
      FileFd Trans(TranslationFile,FileFd::ReadOnly, FileFd::Extension);
    if (FileExists(TranslationFile))
    {
      FileFd Trans(TranslationFile,FileFd::ReadOnly, FileFd::Extension);
@@ -555,7 +556,7 @@ bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
 /* */
 pkgCache::PkgFileIterator debTranslationsIndex::FindInCache(pkgCache &Cache) const
 {
 /* */
 pkgCache::PkgFileIterator debTranslationsIndex::FindInCache(pkgCache &Cache) const
 {
-   string FileName = IndexFile(Language);
+   string FileName = IndexFile(Language.c_str());
    
    pkgCache::PkgFileIterator File = Cache.FileBegin();
    for (; File.end() == false; ++File)
    
    pkgCache::PkgFileIterator File = Cache.FileBegin();
    for (; File.end() == false; ++File)
@@ -671,8 +672,7 @@ APT_CONST bool debStatusIndex::Exists() const
 }
                                                                        /*}}}*/
 
 }
                                                                        /*}}}*/
 
-// debDebPkgFile - Single .deb file                                    /*{{{*/
-// ---------------------------------------------------------------------
+// debDebPkgFile - Single .deb file                                    /*{{{*/
 debDebPkgFileIndex::debDebPkgFileIndex(std::string DebFile)
    : pkgIndexFile(true), DebFile(DebFile)
 {
 debDebPkgFileIndex::debDebPkgFileIndex(std::string DebFile)
    : pkgIndexFile(true), DebFile(DebFile)
 {
@@ -688,53 +688,56 @@ bool debDebPkgFileIndex::Exists() const
 {
    return FileExists(DebFile);
 }
 {
    return FileExists(DebFile);
 }
-bool debDebPkgFileIndex::Merge(pkgCacheGenerator& Gen, OpProgress* Prog) const
+bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &debfile)
 {
 {
-   if(Prog)
-      Prog->SubProgress(0, "Reading deb file");
-
-   // get the control data out of the deb file vid dpkg -I
-   // ... can I haz libdpkg?
-   Configuration::Item const *Opts = _config->Tree("DPkg::Options");
-   std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg");
+   // get the control data out of the deb file via dpkg-deb -I
+   std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb");
    std::vector<const char *> Args;
    Args.push_back(dpkg.c_str());
    std::vector<const char *> Args;
    Args.push_back(dpkg.c_str());
-   if (Opts != 0)
-   {
-      Opts = Opts->Child;
-      for (; Opts != 0; Opts = Opts->Next)
-      {
-        if (Opts->Value.empty() == true)
-           continue;
-        Args.push_back(Opts->Value.c_str());
-      }
-   }
    Args.push_back("-I");
    Args.push_back("-I");
-   Args.push_back(DebFile.c_str());
+   Args.push_back(debfile.c_str());
    Args.push_back("control");
    Args.push_back(NULL);
    FileFd PipeFd;
    pid_t Child;
    if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
       return _error->Error("Popen failed");
    Args.push_back("control");
    Args.push_back(NULL);
    FileFd PipeFd;
    pid_t Child;
    if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
       return _error->Error("Popen failed");
-   // FIXME: static buffer
-   char buf[8*1024];
-   unsigned long long n = 0;
-   if(PipeFd.Read(buf, sizeof(buf)-1, &n) == false)
-      return _error->Errno("read", "Failed to read dpkg pipe");
+
+   char buffer[1024];
+   do {
+      unsigned long long actual = 0;
+      if (PipeFd.Read(buffer, sizeof(buffer)-1, &actual) == false)
+        return _error->Errno("read", "Failed to read dpkg pipe");
+      if (actual == 0)
+        break;
+      buffer[actual] = '\0';
+      content << buffer;
+   } while(true);
    ExecWait(Child, "Popen");
 
    ExecWait(Child, "Popen");
 
-   // now write the control data to a tempfile
+   content << "Filename: " << debfile << "\n";
+   struct stat Buf;
+   if (stat(debfile.c_str(), &Buf) != 0)
+      return false;
+   content << "Size: " << Buf.st_size << "\n";
+
+   return true;
+}
+bool debDebPkgFileIndex::Merge(pkgCacheGenerator& Gen, OpProgress* Prog) const
+{
+   if(Prog)
+      Prog->SubProgress(0, "Reading deb file");
+
+   // write the control data to a tempfile
    SPtr<FileFd> DebControl = GetTempFile("deb-file-" + flNotDir(DebFile));
    if(DebControl == NULL)
       return false;
    SPtr<FileFd> DebControl = GetTempFile("deb-file-" + flNotDir(DebFile));
    if(DebControl == NULL)
       return false;
-   DebControl->Write(buf, n);
-   // append size of the file
-   FileFd Fd(DebFile, FileFd::ReadOnly);
-   string Size;
-   strprintf(Size, "Size: %llu\n", Fd.Size());
-   DebControl->Write(Size.c_str(), Size.size());
-   // and rewind for the listparser
+   std::ostringstream content;
+   if (GetContent(content, DebFile) == false)
+      return false;
+   std::string const contentstr = content.str();
+   DebControl->Write(contentstr.c_str(), contentstr.length());
+   // rewind for the listparser
    DebControl->Seek(0);
 
    // and give it to the list parser
    DebControl->Seek(0);
 
    // and give it to the list parser
@@ -838,7 +841,7 @@ class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type
    public:
    virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const 
    {
    public:
    virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const 
    {
-      return new debDebFileRecordParser(File.FileName(),*File.Cache());
+      return new debDebFileRecordParser(File.FileName());
    };
    debIFTypeDebPkgFile() {Label = "deb Package file";};
 };
    };
    debIFTypeDebPkgFile() {Label = "deb Package file";};
 };