Changeset - r15140:17ee0814362e
[Not reviewed]
master
0 2 0
rubidium - 14 years ago 2010-05-10 09:49:02
rubidium@openttd.org
(svn r19779) -Change: add a return type to AppendPathSeparator and use that to determine whether we could append the path separator. If not, do not recurse into that directory as the path would exceed the maximum path length
2 files changed with 10 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/fileio.cpp
Show inline comments
 
@@ -445,22 +445,27 @@ void FioCreateDirectory(const char *name
 

	
 
/**
 
 * Appends, if necessary, the path separator character to the end of the string.
 
 * It does not add the path separator to zero-sized strings.
 
 * @param buf    string to append the separator to
 
 * @param buflen the length of the buf
 
 * @return true iff the operation succeeded
 
 */
 
void AppendPathSeparator(char *buf, size_t buflen)
 
bool AppendPathSeparator(char *buf, size_t buflen)
 
{
 
	size_t s = strlen(buf);
 

	
 
	/* Length of string + path separator + '\0' */
 
	if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
 
	if (s != 0 && buf[s - 1] != PATHSEPCHAR) {
 
		if (s + 2 >= buflen) return false;
 

	
 
		buf[s]     = PATHSEPCHAR;
 
		buf[s + 1] = '\0';
 
	}
 

	
 
	return true;
 
}
 

	
 
/**
 
 * Allocates and files a variable with the full path
 
 * based on the given directory.
 
 * @param dir the directory to base the path on
 
@@ -1106,13 +1111,13 @@ static uint ScanPath(FileScanner *fs, co
 
		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
 

	
 
		if (S_ISDIR(sb.st_mode)) {
 
			/* Directory */
 
			if (!recursive) continue;
 
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
 
			AppendPathSeparator(filename, lengthof(filename));
 
			if (!AppendPathSeparator(filename, lengthof(filename))) continue;
 
			num += ScanPath(fs, extension, filename, basepath_length, recursive);
 
		} else if (S_ISREG(sb.st_mode)) {
 
			/* File */
 
			if (extension != NULL) {
 
				char *ext = strrchr(filename, '.');
 

	
 
@@ -1193,9 +1198,9 @@ uint FileScanner::Scan(const char *exten
 
 *         AddFile returned true.
 
 */
 
uint FileScanner::Scan(const char *extension, const char *directory, bool recursive)
 
{
 
	char path[MAX_PATH];
 
	strecpy(path, directory, lastof(path));
 
	AppendPathSeparator(path, lengthof(path));
 
	if (!AppendPathSeparator(path, lengthof(path))) return 0;
 
	return ScanPath(this, extension, path, strlen(path), recursive);
 
}
src/fileio_func.h
Show inline comments
 
@@ -54,13 +54,13 @@ bool FioCheckFileExists(const char *file
 
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename);
 
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename);
 
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir);
 
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir);
 

	
 
void SanitizeFilename(char *filename);
 
void AppendPathSeparator(char *buf, size_t buflen);
 
bool AppendPathSeparator(char *buf, size_t buflen);
 
void DeterminePaths(const char *exe);
 
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
 
bool FileExists(const char *filename);
 
const char *FioTarFirstDir(const char *tarname);
 
void FioTarAddLink(const char *src, const char *dest);
 
bool ExtractTar(const char *tar_filename);
0 comments (0 inline, 0 general)