// NAnt - A .NET build tool // Copyright (C) 2001-2003 Gerry Shaw // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Ian Maclean (imaclean@gmail.com) // Jaroslaw Kowalski (jkowalski@users.sourceforge.net) using System; using System.Collections; using System.Diagnostics; using System.Globalization; using System.IO; using System.Reflection; using System.Security; using NAnt.Core; using NAnt.Core.Types; using NAnt.Core.Attributes; namespace NAnt.Core.Functions { /// /// Groups a set of functions for dealing with files. /// [FunctionSet("file", "File")] public class FileFunctions : FunctionSetBase { #region Public Instance Constructors public FileFunctions(Project project, PropertyDictionary properties) : base(project, properties) { } #endregion Public Instance Constructors #region Public Instance Methods /// /// Returns the creation date and time of the specified file. /// /// The file for which to obtain creation date and time information. /// /// The creation date and time of the specified file. /// /// The specified file does not exist. /// is a zero-length string, contains only white space, or contains one or more invalid characters. /// The specified path, file name, or both exceed the system-defined maximum length. /// The parameter is in an invalid format. [Function("get-creation-time")] public DateTime GetCreationTime(string path) { string fullPath = Project.GetFullPath(path); // File.GetCreationTime no longer throws an IOException on // .NET 2.0 if the path does not exist, so we take care of this // ourselves to ensure the behaviour of this function remains // consistent across different CLR versions if (!File.Exists(fullPath)) { throw new IOException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path \"{0}\".", fullPath)); } return File.GetCreationTime(fullPath); } /// /// Returns the date and time the specified file was last written to. /// /// The file for which to obtain write date and time information. /// /// The date and time the specified file was last written to. /// /// The specified file does not exist. /// is a zero-length string, contains only white space, or contains one or more invalid characters. /// The specified path, file name, or both exceed the system-defined maximum length. [Function("get-last-write-time")] public DateTime GetLastWriteTime(string path) { string fullPath = Project.GetFullPath(path); // File.GetLastWriteTime no longer throws an IOException on // .NET 2.0 if the path does not exist, so we take care of this // ourselves to ensure the behaviour of this function remains // consistent across different CLR versions if (!File.Exists(fullPath)) { throw new IOException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path \"{0}\".", fullPath)); } return File.GetLastWriteTime(fullPath); } /// /// Returns the date and time the specified file was last accessed. /// /// The file for which to obtain access date and time information. /// /// The date and time the specified file was last accessed. /// /// The specified file does not exist. /// is a zero-length string, contains only white space, or contains one or more invalid characters. /// The specified path, file name, or both exceed the system-defined maximum length. /// The parameter is in an invalid format. [Function("get-last-access-time")] public DateTime GetLastAccessTime(string path) { string fullPath = Project.GetFullPath(path); // File.GetLastAccessTime no longer throws an IOException on // .NET 2.0 if the path does not exist, so we take care of this // ourselves to ensure the behaviour of this function remains // consistent across different CLR versions if (!File.Exists(fullPath)) { throw new IOException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path \"{0}\".", fullPath)); } return File.GetLastAccessTime(fullPath); } /// /// Determines whether the specified file exists. /// /// The file to check. /// /// if refers to an /// existing file; otherwise, . /// /// /// Execute a set of tasks, if file "output.xml" does not exist. /// /// /// ... /// /// ]]> /// /// [Function("exists")] public bool Exists(string file) { return File.Exists(Project.GetFullPath(file)); } /// /// Determines whether is more or equal /// up-to-date than . /// /// The file to check against the target file. /// The file for which we want to determine the status. /// /// if is more /// or equal up-to-date than ; otherwise, /// . /// /// or is a zero-length string, contains only white space, or contains one or more invalid characters. /// The specified path, file name, or both of either or exceed the system-defined maximum length. [Function("up-to-date")] public bool UpToDate(string srcFile, string targetFile) { string srcPath = Project.GetFullPath(srcFile); string targetPath = Project.GetFullPath(targetFile); if (!File.Exists(targetPath)) { // if targetFile does not exist, we consider it out-of-date return false; } // get lastwritetime of targetFile DateTime targetLastWriteTime = File.GetLastWriteTime(targetPath); // determine whether lastwritetime of srcFile is more recent // than lastwritetime or targetFile string newerFile = FileSet.FindMoreRecentLastWriteTime( srcPath, targetLastWriteTime); // return true if srcFile is not newer than target file return newerFile == null; } /// /// Gets the length of the file. /// /// filename /// /// Length in bytes, of the file named . /// /// The file specified cannot be found. [Function("get-length")] public long GetLength(string file) { FileInfo fi = new FileInfo(Project.GetFullPath(file)); return fi.Length; } /// /// Checks if a given file is an assembly. /// /// The name or path of the file to be checked. /// True if the file is a valid assembly, false if it's not or if the assembly seems corrupted (invalid headers or metadata). /// is a null . /// is an empty . /// is not found, or the file you are trying to check does not specify a filename extension. /// The caller does not have path discovery permission. [Function("is-assembly")] public bool IsAssembly(string assemblyFile) { try { AssemblyName.GetAssemblyName(Project.GetFullPath(assemblyFile)); //no exception occurred, this is an assembly return true; } catch (FileLoadException) { return false; } catch (BadImageFormatException) { return false; } } #endregion Public Instance Methods } }