Exists() public static method

public static Exists ( string path ) : bool
path string
return bool
Exemplo n.º 1
0
 public virtual bool IsSupported(IEnumerable <string> args)
 {
     OperationStart = DateTime.Now;
     if (FileUtils.Exists(Compiler.CompilerExe))
     {
         var rv = Compiler.ProcessArguments(args.ToArray());
         if (!rv)
         {
             Logging.Emit("args not supported {0}", Cache.GetType().Name);
         }
         return(rv);
     }
     throw new FileNotFoundException(Compiler.CompilerExe);
 }
Exemplo n.º 2
0
 public virtual bool IsSupported(ICompiler comp, IEnumerable <string> args)
 {
     if (FileUtils.Exists(compilerPath))
     {
         var rv = comp.ProcessArguments(args.ToArray());
         if (!rv)
         {
             Logging.Emit("unsupported args: {0}", string.Join(" ", args.ToArray()));
         }
         else
         {
             Logging.Input(comp.WorkingDirectory, comp.ObjectTarget, args);
         }
         return(rv);
     }
     throw new FileNotFoundException(compilerPath);
 }
Exemplo n.º 3
0
        public bool ContainsEntry(string key, string filename)
        {
            var p = MakePath(key, filename);

            if (CacheEntryChecksInMemory)
            {
                if (entryCache.Contains(p))
                {
                    return(true);
                }
            }
            var rv = FileUtils.Exists(p);

            if (CacheEntryChecksInMemory && rv)
            {
                entryCache.Add(p);
            }
            return(rv);
        }
Exemplo n.º 4
0
        static string _Find()
        {
            var compiler = Environment.GetEnvironmentVariable("CCLASH_CL");

            if ((compiler != null) && File.Exists(compiler))
            {
                return(compiler);
            }

            var self  = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            var path  = Environment.GetEnvironmentVariable("PATH");
            var paths = path.Split(';');

            var selfdir = Path.GetDirectoryName(self);
            var realcl  = Path.Combine(selfdir, "cl_real.exe");

            if (File.Exists(realcl))
            {
                return(realcl);
            }

            foreach (var p in paths)
            {
                var f = Path.Combine(p, "cl.exe");
                if (FileUtils.Exists(f))
                {
                    if (f.Equals(self, StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }
                    if (Path.IsPathRooted(f))
                    {
                        return(f);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 5
0
 public virtual bool FileExists(string path)
 {
     return(FileUtils.Exists(path));
 }
Exemplo n.º 6
0
        DataHash DigestFile(MD5 provider, string filepath, Regex findDateTime)
        {
            var rv = new DataHash()
            {
                Result    = DataHashResult.FileNotFound,
                InputName = filepath,
            };

            if (!FileUtils.Exists(filepath))
            {
                return(rv);
            }
            provider.Initialize();
            var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

            using (var bs = new BufferedStream(fs))
            {
                Logging.Emit("digest {0}", filepath);
                rv.Hash   = new SoapHexBinary(provider.ComputeHash(bs)).ToString();
                rv.Result = DataHashResult.Ok;

                if (findDateTime != null)
                {
                    // check include cache for this file

                    if (includeCache.ContainsEntry(rv.Hash, F_NotDateTime))
                    {
                        return(rv);
                    }
                    if (includeCache.ContainsEntry(rv.Hash, F_HasDateTime))
                    {
                        rv.Result = DataHashResult.ContainsTimeOrDate;
                        return(rv);
                    }

                    bs.Seek(0, SeekOrigin.Begin);
                    using (var ts = new StreamReader(bs))
                    {
                        string line = null;
                        do
                        {
                            line = ts.ReadLine();
                            if (line == null)
                            {
                                includeCache.WaitOne();
                                try
                                {
                                    includeCache.AddTextFileContent(rv.Hash, F_NotDateTime, "");
                                }
                                finally
                                {
                                    includeCache.ReleaseMutex();
                                }
                                break;
                            }

                            if (findDateTime.IsMatch(line))
                            {
                                rv.Result = DataHashResult.ContainsTimeOrDate;

                                includeCache.WaitOne();
                                try
                                {
                                    includeCache.AddTextFileContent(rv.Hash, F_HasDateTime, "");
                                }
                                finally
                                {
                                    includeCache.ReleaseMutex();
                                }

                                break;
                            }
                        } while (true);
                    }
                }
            }
            rv.Result = DataHashResult.Ok;

            return(rv);
        }
Exemplo n.º 7
0
        int RealInvokeCompiler(IEnumerable <string> args, Action <string> onStdErr, Action <string> onStdOut, bool showIncludes, List <string> foundIncludes)
        {
            int  rv = -1;
            bool retry;

            do
            {
                retry = false;
                Logging.Emit("invoking real compiler: {0} {1} [{2}]", CompilerExe, WorkingDirectory, string.Join(" ", args.ToArray()));

                if (string.IsNullOrWhiteSpace(CompilerExe) || !FileUtils.Exists(CompilerExe))
                {
                    throw new FileNotFoundException("cant find cl.exe");
                }

                if (string.IsNullOrWhiteSpace(compworkdir))
                {
                    throw new InvalidOperationException("no working directory set");
                }

                if (compenvs == null || compenvs.Count == 0)
                {
                    throw new InvalidOperationException("no environment set");
                }

                var cla    = ArgumentUtils.JoinAguments(ArgumentUtils.FixupArgs(args));
                var runExe = compilerExe;

                if (showIncludes)
                {
                    foundIncludes.Add(SingleSourceFile);
                    if (TrackerEnabled)
                    {
                        runExe = "tracker.exe";
                        var trackerargs = new List <string> {
                            "/if", Path.Combine(WorkingDirectory, TrackerFolder),
                            "/k", "/t"
                        };
                        var tcla = ArgumentUtils.JoinAguments(trackerargs);
                        cla = String.Format("{0} /c \"{1}\" {2}", tcla, compilerExe, cla);
                    }
                    else
                    {
                        cla += " /showIncludes";
                    }
                }

                var psi = new ProcessStartInfo(runExe, cla)
                {
                    UseShellExecute        = false,
                    RedirectStandardError  = true,
                    RedirectStandardOutput = true,
                    WorkingDirectory       = compworkdir,
                };

                psi.EnvironmentVariables.Clear();
                foreach (var row in compenvs)
                {
                    psi.EnvironmentVariables[row.Key] = row.Value;
                }
                psi.EnvironmentVariables["PATH"] = Path.GetDirectoryName(CompilerExe) + ";" + psi.EnvironmentVariables["PATH"];
                psi.ErrorDialog = true;
                var p = Process.Start(psi);

                p.OutputDataReceived += (o, a) =>
                {
                    if (a.Data != null)
                    {
                        if (showIncludes && a.Data.StartsWith("Note: including file:"))
                        {
                            var inc = a.Data.Substring("Note: including file:".Length + 1).TrimStart(' ');
                            if (inc.Contains('/'))
                            {
                                inc = inc.Replace('/', '\\');
                            }
                            foundIncludes.Add(inc);
                        }
                        else
                        {
                            if (StdOutputCallback != null)
                            {
                                StdOutputCallback(a.Data + Environment.NewLine);
                            }
                            if (onStdOut != null)
                            {
                                onStdOut(a.Data + Environment.NewLine);
                            }
                            if (Settings.DebugEnabled)
                            {
                                Logging.Emit("stdout {0}", a.Data);
                            }
                        }
                    }
                };

                p.ErrorDataReceived += (o, a) =>
                {
                    if (a.Data != null)
                    {
                        if (StdErrorCallback != null)
                        {
                            StdErrorCallback(a.Data + Environment.NewLine);
                        }
                        if (onStdErr != null)
                        {
                            onStdErr(a.Data + Environment.NewLine);
                        }
                        if (Settings.DebugEnabled)
                        {
                            Logging.Emit("stderr {0}", a.Data);
                        }
                    }
                };

                p.BeginErrorReadLine();
                p.BeginOutputReadLine();

                p.WaitForExit();

                rv = p.ExitCode;
                p.Close();
                Logging.Emit("cl exit {0}", rv);
                if (rv == 0)
                {
                    if (IsSupported)
                    {
                        if (!string.IsNullOrEmpty(ObjectTarget))
                        {
                            var sw = new Stopwatch();
                            sw.Start();
                            while (!File.Exists(ObjectTarget) && (sw.ElapsedMilliseconds < WaitForSlowObject))
                            {
                                System.Threading.Thread.Sleep(500);
                            }
                            sw.Stop();

                            if (!File.Exists(ObjectTarget))
                            {
                                retry = true;
                                if (sw.ElapsedMilliseconds > 2000)
                                {
                                    Logging.Emit("compiler didn't write expected object! {0} after {1}ms", ObjectTarget, (int)sw.Elapsed.TotalMilliseconds);
                                    retry = false;
                                }
                                string logmsg = string.Format("cl exited with zero but failed to create the expected object file! {0}", ObjectTarget);
                                // let the retry system have a go with this

                                if (retry)
                                {
                                    Logging.Warning("{0}, re-running!", logmsg);
                                }
                            }
                            else
                            {
                                Logging.Emit("output: {0} seen", ObjectTarget);
                            }
                        }
                    }
                }

                if (rv != 0)
                {
                    Logging.Emit("non-zero exit");
                }
            } while (retry);
            return(rv);
        }
Exemplo n.º 8
0
        public bool ProcessArguments(string[] args)
        {
            try
            {
                DisableTracker();
                CommandLine = args;
                for (int i = 0; i < args.Length; i++)
                {
                    var opt  = getOption(args[i]);
                    var full = getFullOption(args[i]);

                    #region switch process each argument type
                    switch (opt)
                    {
                    case "/c":
                        HasDashC = true;
                        break;

                    case "/o":
                        return(NotSupported("/o"));

                    case "/D":
                        if (opt == full)
                        {
                            // define value is next argument...
                            i++;
                        }
                        break;

                    case "/I":
                        if (opt == full)
                        {
                            // include path is next argument..
                            // microsoft really dont know how to do command line!
                            i++;
                            if (i > args.Length)
                            {
                                return(NotSupported("-I has no path!"));
                            }
                            full = "/I" + args[i];
                            goto default;
                        }
                        break;

                    case "/Z7":
                        GeneratePdb = false;
                        PdbFile     = null;
                        break;

                    case "/Yu":
                        PrecompiledHeaders = true;
                        return(NotSupported("pre-compiler headers {0}", opt));

                    case "/FI":
                        return(NotSupported(opt));

                    case "/Zi":
                        GeneratePdb = true;
                        break;

                    case "/Fd":
                        PdbFile = Path.Combine(WorkingDirectory, full.Substring(3));
                        // openssl gives us a posix path here..
                        PdbFile = PdbFile.Replace('/', '\\');
                        if (!PdbFile.ToLower().EndsWith(".pdb") && !PdbFile.EndsWith("\\"))
                        {
                            PdbFile = PdbFile + ".pdb";
                        }
                        break;

                    case "/Fo":
                        ObjectTarget = Path.Combine(WorkingDirectory, full.Substring(3));
                        if (ArgumentUtils.TargetIsFolder(ObjectTarget))
                        {
                            ObjectTargetIsFolder = true;
                        }
                        else
                        {
                            ObjectTarget = ArgumentUtils.TargetObject(ObjectTarget);
                        }
                        break;

                    case "/Tp":
                    case "/Tc":
                        var srcfile = ArgumentUtils.MakeWindowsPath(full.Substring(3));
                        if (!Path.IsPathRooted(srcfile))
                        {
                            srcfile = Path.Combine(WorkingDirectory, srcfile);
                        }

                        if (FileUtils.Exists(srcfile))
                        {
                            srcs.Add(srcfile);
                        }
                        else
                        {
                            return(NotSupported("cant find file for {0}", full));
                        }
                        break;

                    case "/E":
                        return(NotSupported(opt));

                    case "/EP":
                        return(NotSupported(opt));

                    default:
                        #region positional or other flag options

                        if (full == "/link")
                        {
                            Linking = true;
                            return(NotSupported("/link"));
                        }

                        if (opt.StartsWith("@"))
                        {
                            #region response file
                            ResponseFile = ArgumentUtils.MakeWindowsPath(full.Substring(1));

                            if (ResponseFile.EndsWith(InternalResponseFileSuffix))
                            {
                                Logging.Emit("cclash misshelper internal response file");
                                return(false);
                            }

                            if (!Path.IsPathRooted(ResponseFile))
                            {
                                ResponseFile = Path.Combine(WorkingDirectory, ResponseFile);
                            }
                            string rsptxt = File.ReadAllText(ResponseFile);
                            if (rsptxt.Length < 2047)
                            // windows max command line, this is why they invented response files
                            {
                                Logging.Emit("response data [{0}]", rsptxt);
                                if (args.Length == 1)
                                {
                                    // this only works if it is the one and only arg!
                                    args = ArgumentUtils.FixupArgs(CommandLineToArgs(rsptxt).Skip(1)).ToArray();
                                    i    = -1;
                                    // replace the command line with the response file content
                                    // and restart parsing. This does go wrong if the response text is huge
                                    continue;
                                }
                            }
                            else
                            {
                                Logging.Emit("response file too large");
                            }

                            return(NotSupported("response file error"));

                            #endregion
                        }

                        if (!full.StartsWith("/"))
                        {
                            // NOTE, if we ever cache -link calls this will also match input objects and libs
                            var file = ArgumentUtils.MakeWindowsPath(full);
                            if (!Path.IsPathRooted(file))
                            {
                                file = Path.Combine(WorkingDirectory, file);
                            }

                            if (FileUtils.Exists(file))
                            {
                                srcs.Add(file);
                                continue;
                            }
                        }
                        if (full.StartsWith("/I"))
                        {
                            var d = ArgumentUtils.MakeWindowsPath(full.Substring(2));
                            if (d == ".")
                            {
                                d = WorkingDirectory;
                            }
                            if (d == "..")
                            {
                                d = Path.GetDirectoryName(WorkingDirectory);
                            }

                            if (!Path.IsPathRooted(d))
                            {
                                d = Path.Combine(WorkingDirectory, d);
                            }

                            if (Directory.Exists(d))
                            {
                                cliincs.Add(d);
                                continue;
                            }
                        }
                        #endregion

                        break;
                    }
                    #endregion
                }

                if (SingleSource)
                {
                    var defaultObj = ArgumentUtils.TargetObject(Path.GetFileNameWithoutExtension(SingleSourceFile));
                    if (ObjectTarget == null)
                    {
                        if (Path.IsPathRooted(defaultObj))
                        {
                            ObjectTarget = defaultObj;
                        }
                        else
                        {
                            ObjectTarget = Path.Combine(WorkingDirectory, defaultObj);
                        }
                    }

                    if (ObjectTargetIsFolder)
                    {
                        ObjectTarget = Path.Combine(ObjectTarget, defaultObj);
                    }

                    if (GeneratePdb)
                    {
                        if (Settings.ConvertObjPdbToZ7)
                        {
                            Logging.Emit("converting pdb request to Z7 embedded debug {0}:{1}", WorkingDirectory, Path.GetFileName(ObjectTarget));
                            // append /Z7 to the arg list and don't generate a pdb
                            var newargs = new List <string>();
                            foreach (var a in args)
                            {
                                if (!(a.StartsWith("/Zi") || a.StartsWith("/Fd")))
                                {
                                    newargs.Add(a);
                                }
                            }
                            newargs.Add("/Z7");
                            AttemptPdb       = false;
                            PdbFile          = null;
                            GeneratePdb      = false;
                            PdbExistsAlready = false;
                            args             = newargs.ToArray();
                        }
                    }

                    if (GeneratePdb)
                    {
                        return(NotSupported("PDB file requested"));
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                return(NotSupported("option parser exception '{0}'", e));
            }
            CompileArgs = args.ToArray();
            return(IsSupported);
        }
Exemplo n.º 9
0
        /// <summary>
        /// When this returns, we will hold the output cache mutex.
        /// </summary>
        /// <param name="commonkey"></param>
        /// <param name="manifest"></param>
        /// <returns></returns>
        public override bool CheckCache(ICompiler comp, IEnumerable <string> args, DataHash commonkey, out CacheManifest manifest)
        {
            manifest = null;
            Lock(CacheLockType.Read);
            manifest = GetCachedManifestLocked(commonkey);
            if (manifest != null)
            {
                #region build missed before
                if (manifest.Disable)
                {
                    Logging.Emit("disabled by manifest");
                    return(false);
                }
                #region check includes
                foreach (var f in manifest.PotentialNewIncludes)
                {
                    if (!FileUtils.FileMissing(f))
                    {
                        Logging.Emit("detected added include file {0}", f);
                        Logging.Miss(commonkey.Hash, DataHashResult.FileAdded, Directory.GetCurrentDirectory(), comp.SingleSourceFile, f);
                        return(false);
                    }
                }
                var hashes = GetHashes(manifest.IncludeFiles.Keys);

                foreach (var h in hashes)
                {
                    if (h.Value.Result == DataHashResult.Ok)
                    {
                        string mhash;
                        if (manifest.IncludeFiles.TryGetValue(h.Key, out mhash))
                        {
                            if (mhash != h.Value.Hash)
                            {
                                Logging.Emit("include file hash changed {0}", h.Key);
                                Logging.Miss(commonkey.Hash, DataHashResult.FileChanged, Directory.GetCurrentDirectory(), comp.SingleSourceFile, h.Key);
                                return(false);
                            }
                        }
                        else
                        {
                            Logging.Emit("include file added {0}", h.Key);
                            Logging.Miss(commonkey.Hash, DataHashResult.FileAdded, Directory.GetCurrentDirectory(), comp.SingleSourceFile, h.Key);
                            return(false);
                        }
                    }
                    else
                    {
                        Logging.Emit("include file hash error {0} {1}", h.Key, h.Value.Result);
                        Logging.Miss(commonkey.Hash, h.Value.Result, Directory.GetCurrentDirectory(), comp.SingleSourceFile, h.Key);
                        return(false);
                    }
                }
                #endregion

                #region check pdb
                if (comp.AttemptPdb)
                {
                    if (comp.PdbExistsAlready)
                    {
                        var pdbhash = hasher.DigestBinaryFile(comp.PdbFile);
                        if (pdbhash.Hash != manifest.EarlierPdbHash)
                        {
                            outputCache.Remove(commonkey.Hash);
                            Logging.Miss(commonkey.Hash, DataHashResult.FileChanged, commonkey.Hash, comp.PdbFile, "");
                            return(false);
                        }
                    }
                }
                #endregion

                #region check cached data exists
                foreach (var f in new string[] { F_Manifest, F_Object })
                {
                    if (!FileUtils.Exists(outputCache.MakePath(commonkey.Hash, f)))
                    {
                        outputCache.Remove(commonkey.Hash);
                        Logging.Miss(commonkey.Hash, DataHashResult.CacheCorrupt, commonkey.Hash, comp.SingleSourceFile, "");
                        return(false);
                    }
                }
                #endregion
                if (Settings.MissLogEnabled)
                {
                    Logging.Emit("hit hc={0},dir={1},src={2}", commonkey.Hash, comp.WorkingDirectory, comp.SingleSourceFile);
                }
                return(true); // cache hit, all includes match and no new files added

                #endregion
            }

            Logging.Miss(commonkey.Hash, DataHashResult.NoPreviousBuild, comp.WorkingDirectory, comp.SingleSourceFile, "");
            return(false);
        }