Ticket #3186: dir.contains.diff

File dir.contains.diff, 1.3 KB (added by anevilyak, 15 years ago)

Proposed fix for Contains() issues.

  • Directory.cpp

     
    1818#include <File.h>
    1919#include <fs_info.h>
    2020#include <Path.h>
     21#include <String.h>
    2122#include <SymLink.h>
    2223
    2324#include <fcntl.h>
     
    508509    if (dirPath.InitCheck() != B_OK || entryPath.InitCheck() != B_OK)
    509510        return false;
    510511
    511     return !strncmp(dirPath.Path(), entryPath.Path(), strlen(dirPath.Path()));
     512    // we need to compare path components individually
     513    // for correctness
     514    const char *dirStr = dirPath.Path();
     515    const char *entryStr = entryPath.Path();
     516    if (dirStr[0] == '/')
     517        ++dirStr;
     518    if (entryStr[0] == '/')
     519        ++entryStr;
     520    while (dirStr[0] != '\0') {
     521        const char *slashPos = strchr(dirStr, '/');
     522        const char *entryPos = strchr(entryStr, '/');
     523        if (slashPos == NULL) {
     524            if (entryPos == NULL)
     525                return strcmp(dirStr, entryStr) == 0;
     526            else
     527                return strncmp(dirStr, entryStr, entryPos - entryStr) == 0;
     528        } else {
     529            if (entryPos == NULL)
     530                return false;
     531            if (entryPos - entryStr != slashPos - dirStr)
     532                return false;
     533            if (strncmp(dirStr, entryStr, slashPos - dirStr) != 0)
     534                return false;
     535        }
     536       
     537        dirStr = slashPos + 1;
     538        entryStr = entryPos + 1;
     539    }
     540   
     541    return false;
    512542}
    513543
    514544