| 1 |
/** |
| 2 |
* Read a file from disk and store it in memory. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 6 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/file_manager.d, _file_manager.d) |
| 7 |
* Documentation: https://dlang.org/phobos/dmd_file_manager.html |
| 8 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/file_manager.d |
| 9 |
*/ |
| 10 |
|
| 11 |
module dmd.file_manager; |
| 12 |
|
| 13 |
import core.stdc.stdio; |
| 14 |
import dmd.common.outbuffer; |
| 15 |
import dmd.root.stringtable : StringTable; |
| 16 |
import dmd.root.file : File, Buffer; |
| 17 |
import dmd.root.filename : FileName, isDirSeparator; |
| 18 |
import dmd.root.string : toDString; |
| 19 |
import dmd.globals; |
| 20 |
import dmd.identifier; |
| 21 |
import dmd.location; |
| 22 |
|
| 23 |
enum package_d = "package." ~ mars_ext; |
| 24 |
enum package_di = "package." ~ hdr_ext; |
| 25 |
|
| 26 |
/// Returns: whether a file with `name` is a special "package.d" module |
| 27 |
bool isPackageFileName(scope FileName fileName) nothrow |
| 28 |
{ |
| 29 |
return FileName.equals(fileName.name, package_d) || FileName.equals(fileName.name, package_di); |
| 30 |
} |
| 31 |
|
| 32 |
// A path stack that allows one to go up and down the path using directory |
| 33 |
// separators. `cur` is the current path, `up` goes up one path, `down` goes |
| 34 |
// down one path. if `up` or `down` return false, there are no further paths. |
| 35 |
private struct PathStack |
| 36 |
{ |
| 37 |
private const(char)[] path; |
| 38 |
private size_t pos; |
| 39 |
|
| 40 |
@safe @nogc nothrow pure: |
| 41 |
|
| 42 |
this(const(char)[] p) |
| 43 |
{ |
| 44 |
path = p; |
| 45 |
pos = p.length; |
| 46 |
} |
| 47 |
|
| 48 |
const(char)[] cur() |
| 49 |
{ |
| 50 |
return path[0 .. pos]; |
| 51 |
} |
| 52 |
|
| 53 |
bool up() |
| 54 |
{ |
| 55 |
if (pos == 0) |
| 56 |
return false; |
| 57 |
while (--pos != 0) |
| 58 |
if (isDirSeparator(path[pos])) |
| 59 |
return true; |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
bool down() |
| 64 |
{ |
| 65 |
if (pos == path.length) |
| 66 |
return false; |
| 67 |
while (++pos != path.length) |
| 68 |
if (isDirSeparator(path[pos])) |
| 69 |
return true; |
| 70 |
return false; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/*************************** |
| 75 |
* Cache path lookups so the operating system |
| 76 |
* is only consulted once for each path. |
| 77 |
*/ |
| 78 |
private struct PathCache |
| 79 |
{ |
| 80 |
/* for filespec "a/b/c/d.ext" |
| 81 |
* a b and c are directories, a, a/b, a/b/c are paths. |
| 82 |
*/ |
| 83 |
|
| 84 |
StringTable!(bool) pathStatus; // cached value of does a path exist or not |
| 85 |
|
| 86 |
nothrow: |
| 87 |
|
| 88 |
/** |
| 89 |
* Determine if the path part of path/filename exists. |
| 90 |
* Cache the results for the path and each left-justified subpath of the path. |
| 91 |
* Params: |
| 92 |
* filespec = path/filename |
| 93 |
* Returns: |
| 94 |
* true if path exists, false if it does not |
| 95 |
*/ |
| 96 |
bool pathExists(const(char)[] filespec) nothrow |
| 97 |
{ |
| 98 |
/* look for the longest leftmost parent path that is cached |
| 99 |
* by starting at the right and working to the left |
| 100 |
*/ |
| 101 |
bool exists = true; |
| 102 |
auto st = PathStack(filespec); |
| 103 |
while (st.up) |
| 104 |
{ |
| 105 |
if (auto cached = pathStatus.lookup(st.cur)) |
| 106 |
{ |
| 107 |
exists = cached.value; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
/* found a parent path that is cached (or reached the left end of the path). |
| 112 |
* Now move right caching the results of those directories. |
| 113 |
* Once a directory is found to not exist, all the directories |
| 114 |
* to the right of it do not exist |
| 115 |
*/ |
| 116 |
while (st.down) |
| 117 |
{ |
| 118 |
if (!exists) |
| 119 |
pathStatus.insert(st.cur, false); |
| 120 |
else |
| 121 |
exists = pathStatus.insert(st.cur, FileName.exists(st.cur) == 2).value; |
| 122 |
} |
| 123 |
|
| 124 |
return exists; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Ask if path ends in a directory. |
| 129 |
* Cache result for speed. |
| 130 |
* Params: |
| 131 |
* path = a path |
| 132 |
* Returns: |
| 133 |
* true if it's a path, false if not |
| 134 |
*/ |
| 135 |
bool isExistingPath(const char[] path) |
| 136 |
{ |
| 137 |
auto cached = pathStatus.lookup(path); |
| 138 |
if (!cached) |
| 139 |
cached = pathStatus.insert(path, FileName.exists(path) == 2); |
| 140 |
return cached.value; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
final class FileManager |
| 145 |
{ |
| 146 |
private StringTable!(const(ubyte)[]) files; // contents of files indexed by file name |
| 147 |
|
| 148 |
private PathCache pathCache; |
| 149 |
|
| 150 |
/// |
| 151 |
public this () nothrow |
| 152 |
{ |
| 153 |
this.files._init(); |
| 154 |
this.pathCache.pathStatus._init(); |
| 155 |
} |
| 156 |
|
| 157 |
nothrow: |
| 158 |
/******************************************** |
| 159 |
* Look for the source file if it's different from filename. |
| 160 |
* Look for .di, .d, directory, and along global.path. |
| 161 |
* Does not open the file. |
| 162 |
* Params: |
| 163 |
* filename = as supplied by the user |
| 164 |
* pathsInfo = pathsInfo to look for filename with metadata |
| 165 |
* whichPathFoundThis = Which path from `path` was used in determining the output path, or -1 if unknown. |
| 166 |
* Returns: |
| 167 |
* the found file name or |
| 168 |
* `null` if it is not different from filename. |
| 169 |
*/ |
| 170 |
const(char)[] lookForSourceFile(const char[] filename, const ImportPathInfo[] pathsInfo, out ptrdiff_t whichPathFoundThis) |
| 171 |
{ |
| 172 |
//printf("lookForSourceFile(`%.*s`)\n", cast(int)filename.length, filename.ptr); |
| 173 |
/* Search along pathsInfo[] for .di file, then .d file. |
| 174 |
*/ |
| 175 |
|
| 176 |
whichPathFoundThis = -1; |
| 177 |
|
| 178 |
// see if we should check for the module locally. |
| 179 |
bool checkLocal = pathCache.pathExists(filename); |
| 180 |
const sdi = FileName.forceExt(filename, hdr_ext); |
| 181 |
if (checkLocal && FileName.exists(sdi) == 1) |
| 182 |
return sdi; |
| 183 |
scope(exit) FileName.free(sdi.ptr); |
| 184 |
|
| 185 |
const sd = FileName.forceExt(filename, mars_ext); |
| 186 |
if (checkLocal && FileName.exists(sd) == 1) |
| 187 |
return sd; |
| 188 |
scope(exit) FileName.free(sd.ptr); |
| 189 |
|
| 190 |
if (checkLocal) |
| 191 |
{ |
| 192 |
if (pathCache.isExistingPath(filename)) |
| 193 |
{ |
| 194 |
/* The filename exists but it's a directory. |
| 195 |
* Therefore, the result should be: filename/package.d |
| 196 |
* iff filename/package.d is a file |
| 197 |
*/ |
| 198 |
const ni = FileName.combine(filename, package_di); |
| 199 |
if (FileName.exists(ni) == 1) |
| 200 |
return ni; |
| 201 |
FileName.free(ni.ptr); |
| 202 |
|
| 203 |
const n = FileName.combine(filename, package_d); |
| 204 |
if (FileName.exists(n) == 1) |
| 205 |
return n; |
| 206 |
FileName.free(n.ptr); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (FileName.absolute(filename)) |
| 211 |
return null; |
| 212 |
if (!pathsInfo.length) |
| 213 |
return null; |
| 214 |
foreach (pathIndex, entry; pathsInfo) |
| 215 |
{ |
| 216 |
const p = entry.path.toDString(); |
| 217 |
|
| 218 |
const(char)[] n = FileName.combine(p, sdi); |
| 219 |
|
| 220 |
if (!pathCache.pathExists(n)) |
| 221 |
{ |
| 222 |
FileName.free(n.ptr); |
| 223 |
continue; // no need to check for anything else. |
| 224 |
} |
| 225 |
if (FileName.exists(n) == 1) |
| 226 |
return n; |
| 227 |
|
| 228 |
FileName.free(n.ptr); |
| 229 |
|
| 230 |
n = FileName.combine(p, sd); |
| 231 |
if (FileName.exists(n) == 1) |
| 232 |
{ |
| 233 |
whichPathFoundThis = pathIndex; |
| 234 |
return n; |
| 235 |
} |
| 236 |
FileName.free(n.ptr); |
| 237 |
|
| 238 |
n = FileName.combine(p, FileName.sansExt(filename)); |
| 239 |
scope(exit) FileName.free(n.ptr); |
| 240 |
|
| 241 |
// also cache this if we are looking for package.d[i] |
| 242 |
if (pathCache.isExistingPath(n)) |
| 243 |
{ |
| 244 |
const n2i = FileName.combine(n, package_di); |
| 245 |
if (FileName.exists(n2i) == 1) |
| 246 |
{ |
| 247 |
whichPathFoundThis = pathIndex; |
| 248 |
return n2i; |
| 249 |
} |
| 250 |
|
| 251 |
FileName.free(n2i.ptr); |
| 252 |
const n2 = FileName.combine(n, package_d); |
| 253 |
if (FileName.exists(n2) == 1) |
| 254 |
{ |
| 255 |
whichPathFoundThis = pathIndex; |
| 256 |
return n2; |
| 257 |
} |
| 258 |
FileName.free(n2.ptr); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/* ImportC: No D modules found, now search along paths[] for .i file, then .c file. |
| 263 |
*/ |
| 264 |
const si = FileName.forceExt(filename, i_ext); |
| 265 |
if (FileName.exists(si) == 1) |
| 266 |
return si; |
| 267 |
scope(exit) FileName.free(si.ptr); |
| 268 |
|
| 269 |
const sc = FileName.forceExt(filename, c_ext); |
| 270 |
if (FileName.exists(sc) == 1) |
| 271 |
return sc; |
| 272 |
scope(exit) FileName.free(sc.ptr); |
| 273 |
foreach (pathIndex, entry; pathsInfo) |
| 274 |
{ |
| 275 |
const p = entry.path.toDString(); |
| 276 |
|
| 277 |
const(char)[] n = FileName.combine(p, si); |
| 278 |
if (FileName.exists(n) == 1) |
| 279 |
{ |
| 280 |
whichPathFoundThis = pathIndex; |
| 281 |
return n; |
| 282 |
} |
| 283 |
FileName.free(n.ptr); |
| 284 |
|
| 285 |
n = FileName.combine(p, sc); |
| 286 |
if (FileName.exists(n) == 1) |
| 287 |
{ |
| 288 |
whichPathFoundThis = pathIndex; |
| 289 |
return n; |
| 290 |
} |
| 291 |
FileName.free(n.ptr); |
| 292 |
} |
| 293 |
return null; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Retrieve the cached contents of the file given by `filename`. |
| 298 |
* If the file has not been read before, read it and add the contents |
| 299 |
* to the file cache. |
| 300 |
* Params: |
| 301 |
* filename = the name of the file |
| 302 |
* Returns: |
| 303 |
* the contents of the file, or `null` if it could not be read or was empty |
| 304 |
*/ |
| 305 |
const(ubyte)[] getFileContents(FileName filename) |
| 306 |
{ |
| 307 |
const name = filename.toString; |
| 308 |
if (auto val = files.lookup(name)) // if `name` is cached |
| 309 |
return val.value; // return its contents |
| 310 |
|
| 311 |
if (FileName.exists(name) != 1) // if not an ordinary file |
| 312 |
return null; |
| 313 |
|
| 314 |
OutBuffer buf; |
| 315 |
if (File.read(name, buf)) |
| 316 |
return null; // failed |
| 317 |
|
| 318 |
buf.write32(0); // terminating dchar 0 |
| 319 |
|
| 320 |
const length = buf.length; |
| 321 |
const ubyte[] fb = cast(ubyte[])(buf.extractSlice()[0 .. length - 4]); |
| 322 |
if (files.insert(name, fb) is null) |
| 323 |
assert(0, "Insert after lookup failure should never return `null`"); |
| 324 |
|
| 325 |
return fb; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Adds the contents of a file to the table. |
| 330 |
* Params: |
| 331 |
* filename = name of the file |
| 332 |
* buffer = contents of the file |
| 333 |
* Returns: |
| 334 |
* the buffer added, or null |
| 335 |
*/ |
| 336 |
const(ubyte)[] add(FileName filename, const(ubyte)[] buffer) |
| 337 |
{ |
| 338 |
auto val = files.insert(filename.toString, buffer); |
| 339 |
return val == null ? null : val.value; |
| 340 |
} |
| 341 |
} |