-----------------------------------------------------------------------------
| 1043 | |
| 1044 | //----------------------------------------------------------------------------- |
| 1045 | bool Platform::isSubDirectory(const char *pParent, const char *pDir) |
| 1046 | { |
| 1047 | if (!pParent || !*pDir) |
| 1048 | return false; |
| 1049 | |
| 1050 | // this is somewhat of a brute force method but we need to be 100% sure |
| 1051 | // that the user cannot enter things like ../dir or /dir etc,... |
| 1052 | DIR *directory; |
| 1053 | |
| 1054 | directory = opendir(pParent); |
| 1055 | if (directory == NULL) |
| 1056 | return false; |
| 1057 | |
| 1058 | struct dirent *fEntry; |
| 1059 | fEntry = readdir(directory); |
| 1060 | if ( fEntry == NULL ) |
| 1061 | { |
| 1062 | closedir(directory); |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
| 1066 | do |
| 1067 | { |
| 1068 | char dirBuf[MaxPath]; |
| 1069 | struct stat fStat; |
| 1070 | |
| 1071 | dSprintf(dirBuf, sizeof(dirBuf), "%s/%s", pParent, fEntry->d_name); |
| 1072 | if (stat(dirBuf, &fStat) < 0) |
| 1073 | continue; |
| 1074 | // if it is a directory... |
| 1075 | if ( (fStat.st_mode & S_IFMT) == S_IFDIR) |
| 1076 | { |
| 1077 | // and the names match |
| 1078 | if (dStrcmp(pDir, fEntry->d_name ) == 0) |
| 1079 | { |
| 1080 | // then we have a real sub directory |
| 1081 | closedir(directory); |
| 1082 | return true; |
| 1083 | } |
| 1084 | } |
| 1085 | } while( (fEntry = readdir(directory)) != NULL ); |
| 1086 | |
| 1087 | closedir(directory); |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | //----------------------------------------------------------------------------- |
| 1092 |