Branch: Tag:

2016-04-05

2016-04-05 16:11:55 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Filesystem: Improved wrapper for normalize_path().

Attempt to handle the case where the entire path is invalid.

Potential fix for more issues on NT.

48: Inside #if constant(System.normalize_path)
  // NB: System.normalize_path() throws errors on nonexisting files.   protected string normalize_path(string path)   { +  // Get rid of segments with ".." and ".".    path = combine_path(path); -  +     if (Stdio.exist(path)) { -  +  // The easy case.    return System.normalize_path(path);    } -  +     if (has_suffix(path, "/") && Stdio.exist(path + ".")) { -  +  // The almost easy case.    path = System.normalize_path(path + ".");   #ifdef __NT__    path += "\\";
61:   #endif    return path;    } +  +  // Try finding a valid prefix with binary search.    array(string) a = path/"/";    int lo, hi = sizeof(a);   #ifdef __NT__
69: Inside #if defined(__NT__)
   lo = 1;    }   #endif -  while (lo + 1 < hi) { +  while (lo < hi) {    int m = (lo + hi)/2;    if (Stdio.exist(a[..m] * "/")) { -  +  if (lo == m) break;    lo = m;    } else {    hi = m;    }    } -  // NB: hi == lo + 1 here. -  path = System.normalize_path(a[..lo] * "/") + "/" + (a[hi..] * "/"); +  +  // NB: If hi is zero then the entire path is invalid, +  // so no need to call System.normalize_path(). +  if (hi) { +  path = System.normalize_path(a[..lo] * "/") + "/" + (a[lo+1..] * "/"); +  } +    #ifdef __NT__    return replace(path, "/", "\\");   #else