Procházet zdrojové kódy

Add a GetListOfFilesInDir() helper method which replaces the old
code copies used to load the various parts-files

David Kalnischkies před 16 roky
rodič
revize
52643bec17

+ 2 - 37
apt-pkg/contrib/configuration.cc

@@ -22,14 +22,8 @@
 #include <apti18n.h>
 #include <apti18n.h>
 
 
 #include <vector>
 #include <vector>
-#include <algorithm>
 #include <fstream>
 #include <fstream>
 #include <iostream>
 #include <iostream>
-    
-#include <stdio.h>
-#include <dirent.h>
-#include <sys/stat.h>
-#include <unistd.h>
 
 
 using namespace std;
 using namespace std;
 									/*}}}*/
 									/*}}}*/
@@ -837,37 +831,8 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectio
 /* */
 /* */
 bool ReadConfigDir(Configuration &Conf,const string &Dir,bool const &AsSectional,
 bool ReadConfigDir(Configuration &Conf,const string &Dir,bool const &AsSectional,
 		   unsigned const &Depth)
 		   unsigned const &Depth)
-{   
-   DIR *D = opendir(Dir.c_str());
-   if (D == 0)
-      return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
-
-   vector<string> List;
-   
-   for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
-   {
-      if (Ent->d_name[0] == '.')
-	 continue;
-      
-      // Skip bad file names ala run-parts
-      const char *C = Ent->d_name;
-      for (; *C != 0; C++)
-	 if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-')
-	    break;
-      if (*C != 0)
-	 continue;
-      
-      // Make sure it is a file and not something else
-      string File = flCombine(Dir,Ent->d_name);
-      struct stat St;
-      if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
-	 continue;
-      
-      List.push_back(File);      
-   }   
-   closedir(D);
-   
-   sort(List.begin(),List.end());
+{
+   vector<string> const List = GetListOfFilesInDir(Dir, "", true);
 
 
    // Read the files
    // Read the files
    for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
    for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)

+ 47 - 0
apt-pkg/contrib/fileutl.cc

@@ -34,9 +34,11 @@
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/time.h>
 #include <sys/wait.h>
 #include <sys/wait.h>
+#include <dirent.h>
 #include <signal.h>
 #include <signal.h>
 #include <errno.h>
 #include <errno.h>
 #include <set>
 #include <set>
+#include <algorithm>
 									/*}}}*/
 									/*}}}*/
 
 
 using namespace std;
 using namespace std;
@@ -195,6 +197,51 @@ bool FileExists(string File)
    return true;
    return true;
 }
 }
 									/*}}}*/
 									/*}}}*/
+// GetListOfFilesInDir - returns a vector of files in the given dir	/*{{{*/
+// ---------------------------------------------------------------------
+/* If an extension is given only files with this extension are included
+   in the returned vector, otherwise every "normal" file is included. */
+std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
+				bool const &SortList) {
+	std::vector<string> List;
+	DIR *D = opendir(Dir.c_str());
+	if (D == 0) {
+		_error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
+		return List;
+	}
+
+	for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) {
+		if (Ent->d_name[0] == '.')
+			continue;
+
+		if (Ext.empty() == false && flExtension(Ent->d_name) != Ext)
+			continue;
+
+		// Skip bad file names ala run-parts
+		const char *C = Ent->d_name;
+		for (; *C != 0; ++C)
+			if (isalpha(*C) == 0 && isdigit(*C) == 0
+			    && *C != '_' && *C != '-' && *C != '.')
+				break;
+
+		if (*C != 0)
+			continue;
+
+		// Make sure it is a file and not something else
+		string const File = flCombine(Dir,Ent->d_name);
+		struct stat St;
+		if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
+			continue;
+
+		List.push_back(File);
+	}
+	closedir(D);
+
+	if (SortList == true)
+		std::sort(List.begin(),List.end());
+	return List;
+}
+									/*}}}*/
 // SafeGetCWD - This is a safer getcwd that returns a dynamic string	/*{{{*/
 // SafeGetCWD - This is a safer getcwd that returns a dynamic string	/*{{{*/
 // ---------------------------------------------------------------------
 // ---------------------------------------------------------------------
 /* We return / on failure. */
 /* We return / on failure. */

+ 3 - 0
apt-pkg/contrib/fileutl.h

@@ -23,6 +23,7 @@
 
 
 
 
 #include <string>
 #include <string>
+#include <vector>
 
 
 using std::string;
 using std::string;
 
 
@@ -81,6 +82,8 @@ bool RunScripts(const char *Cnf);
 bool CopyFile(FileFd &From,FileFd &To);
 bool CopyFile(FileFd &From,FileFd &To);
 int GetLock(string File,bool Errors = true);
 int GetLock(string File,bool Errors = true);
 bool FileExists(string File);
 bool FileExists(string File);
+std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
+				bool const &SortList);
 string SafeGetCWD();
 string SafeGetCWD();
 void SetCloseExec(int Fd,bool Close);
 void SetCloseExec(int Fd,bool Close);
 void SetNonBlock(int Fd,bool Block);
 void SetNonBlock(int Fd,bool Block);

+ 3 - 34
apt-pkg/policy.cc

@@ -27,14 +27,12 @@
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/tagfile.h>
 #include <apt-pkg/tagfile.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/fileutl.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/sptr.h>
 #include <apt-pkg/sptr.h>
-    
+
 #include <apti18n.h>
 #include <apti18n.h>
 
 
-#include <dirent.h>
-#include <sys/stat.h>
-#include <algorithm>
 #include <iostream>
 #include <iostream>
 #include <sstream>
 #include <sstream>
 									/*}}}*/
 									/*}}}*/
@@ -282,36 +280,7 @@ bool ReadPinDir(pkgPolicy &Plcy,string Dir)
       return true;
       return true;
    }
    }
 
 
-   DIR *D = opendir(Dir.c_str());
-   if (D == 0)
-      return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
-
-   vector<string> List;
-
-   for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
-   {
-      if (Ent->d_name[0] == '.')
-	 continue;
-
-      // Skip bad file names ala run-parts
-      const char *C = Ent->d_name;
-      for (; *C != 0; C++)
-	 if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-')
-	    break;
-      if (*C != 0)
-	 continue;
-
-      // Make sure it is a file and not something else
-      string File = flCombine(Dir,Ent->d_name);
-      struct stat St;
-      if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
-	 continue;
-
-      List.push_back(File);
-   }
-   closedir(D);
-
-   sort(List.begin(),List.end());
+   vector<string> const List = GetListOfFilesInDir(Dir, "", true);
 
 
    // Read the files
    // Read the files
    for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
    for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)

+ 1 - 42
apt-pkg/sourcelist.cc

@@ -17,13 +17,6 @@
 #include <apti18n.h>
 #include <apti18n.h>
 
 
 #include <fstream>
 #include <fstream>
-
-// CNC:2003-03-03 - This is needed for ReadDir stuff.
-#include <algorithm>
-#include <stdio.h>
-#include <dirent.h>
-#include <sys/stat.h>
-#include <unistd.h>
 									/*}}}*/
 									/*}}}*/
 
 
 using namespace std;
 using namespace std;
@@ -322,41 +315,7 @@ bool pkgSourceList::GetIndexes(pkgAcquire *Owner, bool GetAll) const
 /* */
 /* */
 bool pkgSourceList::ReadSourceDir(string Dir)
 bool pkgSourceList::ReadSourceDir(string Dir)
 {
 {
-   DIR *D = opendir(Dir.c_str());
-   if (D == 0)
-      return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
-
-   vector<string> List;
-   
-   for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
-   {
-      if (Ent->d_name[0] == '.')
-	 continue;
-
-      // CNC:2003-12-02 Only accept .list files as valid sourceparts
-      if (flExtension(Ent->d_name) != "list")
-	 continue;
-      
-      // Skip bad file names ala run-parts
-      const char *C = Ent->d_name;
-      for (; *C != 0; C++)
-	 if (isalpha(*C) == 0 && isdigit(*C) == 0
-             && *C != '_' && *C != '-' && *C != '.')
-	    break;
-      if (*C != 0)
-	 continue;
-      
-      // Make sure it is a file and not something else
-      string File = flCombine(Dir,Ent->d_name);
-      struct stat St;
-      if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
-	 continue;
-      
-      List.push_back(File);      
-   }   
-   closedir(D);
-   
-   sort(List.begin(),List.end());
+   vector<string> const List = GetListOfFilesInDir(Dir, "list", true);
 
 
    // Read the files
    // Read the files
    for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
    for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)