// Check if the guest path exists - doesn't need to be mappable // eg: if "C:\WINDOWS" is mapped then "C:\" does exist even if // not mapped // eg2: if "C:\WINDOWS" is mapped then "C:\WINDOWS\MAYBE" should only // return true if MAYBE actually exists // Host path doesn't need to be writable public bool DoesGuestDirectoryExist(string guestPath) { // Check it's valid if (!DosPath.IsValid(guestPath)) { return(false); } // Check if it exists as a parent folder of a mapping (example 1 above) for (int i = 0; i < _mountPoints.Count; i++) { var mp = _mountPoints[i]; if (DoesPathPrefixMatch(guestPath, mp.guest)) { return(true); } } // Try to map it and check if it exists (example 2 above) var hostPath = TryMapGuestToHost(guestPath, false); if (hostPath == null) { return(false); } return(System.IO.Directory.Exists(hostPath)); }
private string LocateModule(string moduleName, string parentPath) { // Append dll extension if (!System.IO.Path.GetFileName(moduleName).Contains('.')) { moduleName += ".DLL"; } // Fully qualified already? if (DosPath.IsFullyQualified(moduleName)) { return(moduleName.ToUpper()); } // Check each search location foreach (var path in GetSearchPath(parentPath)) { // Must be fully qualified if (!DosPath.IsFullyQualified(path)) { continue; } // Resolve it var resolvedPathGuest = DosPath.ResolveRelativePath(path, moduleName); var resolvedPathHost = _machine.PathMapper.TryMapGuestToHost(resolvedPathGuest, false); if (resolvedPathHost == null) { continue; } if (System.IO.File.Exists(resolvedPathHost)) { return(resolvedPathGuest.ToUpper()); } } // Not found! return(null); }
// Convert a host filename to 8.3 friendly version // Returns null if can't public string TryMapHostToGuest(string hostPath, bool forWrite) { // Map special extension if (hostPath.EndsWith(".exe16", StringComparison.InvariantCultureIgnoreCase)) { hostPath = hostPath.Substring(0, hostPath.Length - 2); } for (int i = 0; i < _mountPoints.Count; i++) { var mp = _mountPoints[i]; string mapped = null; if (mp.hostWrite != null && forWrite) { if (DoesPathPrefixMatch(mp.hostWrite, hostPath)) { mapped = mp.guest + hostPath.Substring(mp.hostWrite.Length); } } else { if (DoesPathPrefixMatch(mp.host, hostPath)) { mapped = mp.guest + hostPath.Substring(mp.host.Length); } } if (mapped != null) { if (!DosPath.IsValid(mapped)) { break; } return(mapped); } } return(null); }
public void Prepare(Dictionary <string, Mount> mountPointMap) { var vr = _machine.VariableResolver; // Replace all foreach (var kv in mountPointMap) { kv.Value.guest = vr.Resolve(kv.Value.guest == null ? kv.Key : kv.Value.guest); kv.Value.host = vr.Resolve(kv.Value.host); kv.Value.hostWrite = vr.Resolve(kv.Value.hostWrite); } // Sort longest to shortest _mountPoints = mountPointMap.Values.OrderByDescending(x => x.guest.Length).ToList(); // Check all mount points are valid foreach (var mp in _mountPoints) { if (!DosPath.IsValid(mp.guest)) { throw new InvalidDataException(string.Format("The mounted path '{0}' isn't a valid 8.3 filename", mp.guest)); } } }
// Convert a 8.3 file name to host equivalent public string TryMapGuestToHost(string guestPath, bool forWrite) { if (guestPath == null) { return(null); } if (DosPath.IsValid(guestPath)) { for (int i = 0; i < _mountPoints.Count; i++) { var mp = _mountPoints[i]; if (DoesPathPrefixMatch(mp.guest, guestPath)) { // Work out the read-only path var readPath = mp.host + guestPath.Substring(mp.guest.Length); if (!forWrite && System.IO.Directory.Exists(readPath)) { return(readPath); } // If no separate write mapping then use the read path if (mp.hostWrite != null) { // Work out the write path var writePath = mp.hostWrite + guestPath.Substring(mp.guest.Length); // Does it already exist? if (System.IO.Directory.Exists(writePath)) { return(writePath); } if (System.IO.File.Exists(writePath)) { return(writePath); } // Copy from the read directory? if (forWrite) { // Make sure the mounted directory exists if (!System.IO.Directory.Exists(mp.hostWrite)) { System.IO.Directory.CreateDirectory(mp.hostWrite); } // File already exist? if (System.IO.File.Exists(readPath)) { // Make sure the target folder exists System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(writePath)); // Copy the source folder System.IO.File.Copy(readPath, writePath); } return(writePath); } } if (readPath.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase) && System.IO.File.Exists(readPath + "16")) { return(readPath + "16"); } // Done! return(readPath); } } } return(null); }