internal bool ExecuteImpl(Microsoft.Build.Shared.FileExists fileExists, Microsoft.Build.Shared.FileCreate fileCreate, GetAttributes fileGetAttributes, SetAttributes fileSetAttributes, SetLastAccessTime fileSetLastAccessTime, SetLastWriteTime fileSetLastWriteTime)
 {
     DateTime touchDateTime;
     try
     {
         touchDateTime = this.GetTouchDateTime();
     }
     catch (FormatException exception)
     {
         base.Log.LogErrorWithCodeFromResources("Touch.TimeSyntaxIncorrect", new object[] { exception.Message });
         return false;
     }
     bool flag = true;
     ArrayList list = new ArrayList();
     HashSet<string> set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
     foreach (ITaskItem item in this.Files)
     {
         if (!set.Contains(item.ItemSpec))
         {
             if (this.TouchFile(item.ItemSpec, touchDateTime, fileExists, fileCreate, fileGetAttributes, fileSetAttributes, fileSetLastAccessTime, fileSetLastWriteTime))
             {
                 list.Add(item);
             }
             else
             {
                 flag = false;
             }
             set.Add(item.ItemSpec);
         }
     }
     this.TouchedFiles = (ITaskItem[]) list.ToArray(typeof(ITaskItem));
     return flag;
 }
Example #2
0
        /// <summary>
        /// Implementation of the execute method.
        /// </summary>
        /// <returns></returns>
        internal bool ExecuteImpl
        (
            FileExists fileExists,
            FileCreate fileCreate,
            GetAttributes fileGetAttributes,
            SetAttributes fileSetAttributes,
            SetLastAccessTime fileSetLastAccessTime,
            SetLastWriteTime fileSetLastWriteTime
        )
        {
            // See what time we are touching all files to
            DateTime touchDateTime;

            try
            {
                touchDateTime = GetTouchDateTime();
            }
            catch (FormatException e)
            {
                Log.LogErrorWithCodeFromResources("Touch.TimeSyntaxIncorrect", e.Message);
                return(false);
            }

            // Go through all files and touch 'em
            bool retVal          = true;
            var  touchedItems    = new ArrayList();
            var  touchedFilesSet = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (ITaskItem file in Files)
            {
                string path = FileUtilities.FixFilePath(file.ItemSpec);
                // For speed, eliminate duplicates caused by poor targets authoring
                if (touchedFilesSet.Contains(path))
                {
                    continue;
                }

                // Touch the file.  If the file was touched successfully then add it to our array of
                // touched items.
                if
                (
                    TouchFile
                    (
                        path,
                        touchDateTime,
                        fileExists,
                        fileCreate,
                        fileGetAttributes,
                        fileSetAttributes,
                        fileSetLastAccessTime,
                        fileSetLastWriteTime
                    )
                )
                {
                    touchedItems.Add(file);
                }
                else
                {
                    retVal = false;
                }

                // Add even on failure to avoid reattempting
                touchedFilesSet.Add(path);
            }

            // Now, set the property that indicates which items we touched.  Note that we
            // touch all the items
            TouchedFiles = (ITaskItem[])touchedItems.ToArray(typeof(ITaskItem));
            return(retVal);
        }
Example #3
0
        /// <summary>
        /// Helper method touches a file.
        /// </summary>
        /// <returns>"True" if the file was touched.</returns>
        private bool TouchFile
        (
            string file,
            DateTime dt,
            FileExists fileExists,
            FileCreate fileCreate,
            GetAttributes fileGetAttributes,
            SetAttributes fileSetAttributes,
            SetLastAccessTime fileSetLastAccessTime,
            SetLastWriteTime fileSetLastWriteTime
        )
        {
            if (!fileExists(file))
            {
                // If the file does not exist then we check if we need to create it.
                if (AlwaysCreate)
                {
                    Log.LogMessageFromResources(MessageImportance.Normal, "Touch.CreatingFile", file, "AlwaysCreate");
                    if (!CreateFile(file, fileCreate))
                    {
                        return(false);
                    }
                }
                else
                {
                    Log.LogErrorWithCodeFromResources("Touch.FileDoesNotExist", file);
                    return(false);
                }
            }
            else
            {
                Log.LogMessageFromResources(MessageImportance.Normal, "Touch.Touching", file);
            }

            // If the file is read only then we must either issue an error, or, if the user so
            // specified, make the file temporarily not read only.
            bool           needToRestoreAttributes = false;
            FileAttributes faOriginal = fileGetAttributes(file);

            if ((faOriginal & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                if (ForceTouch)
                {
                    try
                    {
                        FileAttributes faNew = (faOriginal & ~FileAttributes.ReadOnly);
                        fileSetAttributes(file, faNew);
                        needToRestoreAttributes = true;
                    }
                    catch (Exception e) when(ExceptionHandling.IsIoRelatedException(e))
                    {
                        Log.LogErrorWithCodeFromResources("Touch.CannotMakeFileWritable", file, e.Message);
                        return(false);
                    }
                }
            }

            // Do the actual touch operation
            bool retVal = true;

            try
            {
                fileSetLastAccessTime(file, dt);
                fileSetLastWriteTime(file, dt);
            }
            catch (Exception e) when(ExceptionHandling.IsIoRelatedException(e))
            {
                Log.LogErrorWithCodeFromResources("Touch.CannotTouch", file, e.Message);
                return(false);
            }
            finally
            {
                if (needToRestoreAttributes)
                {
                    // Attempt to restore the attributes.  If we fail here, then there is
                    // not much we can do.
                    try
                    {
                        fileSetAttributes(file, faOriginal);
                    }
                    catch (Exception e) when(ExceptionHandling.IsIoRelatedException(e))
                    {
                        Log.LogErrorWithCodeFromResources("Touch.CannotRestoreAttributes", file, e.Message);
                        retVal = false;
                    }
                }
            }

            return(retVal);
        }
Example #4
0
        private bool TouchFile(string file, DateTime dt, Microsoft.Build.Shared.FileExists fileExists, Microsoft.Build.Shared.FileCreate fileCreate, GetAttributes fileGetAttributes, SetAttributes fileSetAttributes, SetLastAccessTime fileSetLastAccessTime, SetLastWriteTime fileSetLastWriteTime)
        {
            if (!fileExists(file))
            {
                if (!this.AlwaysCreate)
                {
                    base.Log.LogErrorWithCodeFromResources("Touch.FileDoesNotExist", new object[] { file });
                    return(false);
                }
                base.Log.LogMessageFromResources(MessageImportance.Normal, "Touch.CreatingFile", new object[] { file, "AlwaysCreate" });
                if (!this.CreateFile(file, fileCreate))
                {
                    return(false);
                }
            }
            else
            {
                base.Log.LogMessageFromResources(MessageImportance.Normal, "Touch.Touching", new object[] { file });
            }
            bool           flag       = false;
            FileAttributes attributes = fileGetAttributes(file);

            if (((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) && this.ForceTouch)
            {
                try
                {
                    FileAttributes attributes2 = attributes & ~FileAttributes.ReadOnly;
                    fileSetAttributes(file, attributes2);
                    flag = true;
                }
                catch (Exception exception)
                {
                    if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
                    {
                        throw;
                    }
                    base.Log.LogErrorWithCodeFromResources("Touch.CannotMakeFileWritable", new object[] { file, exception.Message });
                    return(false);
                }
            }
            bool flag2 = true;

            try
            {
                fileSetLastAccessTime(file, dt);
                fileSetLastWriteTime(file, dt);
            }
            catch (Exception exception2)
            {
                if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception2))
                {
                    throw;
                }
                base.Log.LogErrorWithCodeFromResources("Touch.CannotTouch", new object[] { file, exception2.Message });
                return(false);
            }
            finally
            {
                if (flag)
                {
                    try
                    {
                        fileSetAttributes(file, attributes);
                    }
                    catch (Exception exception3)
                    {
                        if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception3))
                        {
                            throw;
                        }
                        base.Log.LogErrorWithCodeFromResources("Touch.CannotRestoreAttributes", new object[] { file, exception3.Message });
                        flag2 = false;
                    }
                }
            }
            return(flag2);
        }
Example #5
0
        internal bool ExecuteImpl(Microsoft.Build.Shared.FileExists fileExists, Microsoft.Build.Shared.FileCreate fileCreate, GetAttributes fileGetAttributes, SetAttributes fileSetAttributes, SetLastAccessTime fileSetLastAccessTime, SetLastWriteTime fileSetLastWriteTime)
        {
            DateTime touchDateTime;

            try
            {
                touchDateTime = this.GetTouchDateTime();
            }
            catch (FormatException exception)
            {
                base.Log.LogErrorWithCodeFromResources("Touch.TimeSyntaxIncorrect", new object[] { exception.Message });
                return(false);
            }
            bool             flag = true;
            ArrayList        list = new ArrayList();
            HashSet <string> set  = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (ITaskItem item in this.Files)
            {
                if (!set.Contains(item.ItemSpec))
                {
                    if (this.TouchFile(item.ItemSpec, touchDateTime, fileExists, fileCreate, fileGetAttributes, fileSetAttributes, fileSetLastAccessTime, fileSetLastWriteTime))
                    {
                        list.Add(item);
                    }
                    else
                    {
                        flag = false;
                    }
                    set.Add(item.ItemSpec);
                }
            }
            this.TouchedFiles = (ITaskItem[])list.ToArray(typeof(ITaskItem));
            return(flag);
        }
Example #6
0
        /// <summary>
        /// Implementation of the execute method.
        /// </summary>
        /// <returns></returns>
        internal bool ExecuteImpl
        (
            FileExists fileExists,
            FileCreate fileCreate,
            GetAttributes fileGetAttributes,
            SetAttributes fileSetAttributes,
            SetLastAccessTime fileSetLastAccessTime,
            SetLastWriteTime fileSetLastWriteTime

        )
        {
            // See what time we are touching all files to
            DateTime touchDateTime;
            try
            {
                touchDateTime = GetTouchDateTime();
            }
            catch (FormatException e)
            {
                Log.LogErrorWithCodeFromResources("Touch.TimeSyntaxIncorrect", e.Message);
                return false;
            }

            // Go through all files and touch 'em
            bool retVal = true;
            ArrayList touchedItems = new ArrayList();
            HashSet<string> touchedFilesSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            foreach (ITaskItem file in Files)
            {
                // For speed, eliminate duplicates caused by poor targets authoring
                if (touchedFilesSet.Contains(file.ItemSpec))
                {
                    continue;
                }

                // Touch the file.  If the file was touched successfully then add it to our array of 
                // touched items. 
                if
                (
                    TouchFile
                    (
                        file.ItemSpec,
                        touchDateTime,
                        fileExists,
                        fileCreate,
                        fileGetAttributes,
                        fileSetAttributes,
                        fileSetLastAccessTime,
                        fileSetLastWriteTime

                    )
                )
                {
                    touchedItems.Add(file);
                }
                else
                {
                    retVal = false;
                }

                // Add even on failure to avoid reattempting
                touchedFilesSet.Add(file.ItemSpec);
            }

            // Now, set the property that indicates which items we touched.  Note that we
            // touch all the items 
            TouchedFiles = (ITaskItem[])touchedItems.ToArray(typeof(ITaskItem));
            return retVal;
        }
Example #7
0
        /// <summary>
        /// Helper method touches a file.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="dt"></param>
        /// <param name="fileExists"></param>
        /// <param name="fileCreate"></param>
        /// <param name="fileGetAttributes"></param>
        /// <param name="fileSetAttributes"></param>
        /// <param name="fileSetLastAccessTime"></param>
        /// <param name="fileSetLastWriteTime"></param>
        /// <returns>"True" if the file was touched.</returns>
        private bool TouchFile
        (
            string file,
            DateTime dt,
            FileExists fileExists,
            FileCreate fileCreate,
            GetAttributes fileGetAttributes,
            SetAttributes fileSetAttributes,
            SetLastAccessTime fileSetLastAccessTime,
            SetLastWriteTime fileSetLastWriteTime
        )
        {
            if (!fileExists(file))
            {
                // If the file does not exist then we check if we need to create it.
                if (AlwaysCreate)
                {
                    Log.LogMessageFromResources(MessageImportance.Normal, "Touch.CreatingFile", file, "AlwaysCreate");
                    if (!CreateFile(file, fileCreate))
                    {
                        return false;
                    }
                }
                else
                {
                    Log.LogErrorWithCodeFromResources("Touch.FileDoesNotExist", file);
                    return false;
                }
            }
            else
            {
                Log.LogMessageFromResources(MessageImportance.Normal, "Touch.Touching", file);
            }

            // If the file is read only then we must either issue an error, or, if the user so 
            // specified, make the file temporarily not read only.
            bool needToRestoreAttributes = false;
            System.IO.FileAttributes faOriginal = fileGetAttributes(file);
            if ((faOriginal & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)
            {
                if (ForceTouch)
                {
                    try
                    {
                        System.IO.FileAttributes faNew = (faOriginal & ~System.IO.FileAttributes.ReadOnly);
                        fileSetAttributes(file, faNew);
                        needToRestoreAttributes = true;
                    }
                    catch (Exception e) // Catching Exception, but rethrowing unless it's a well-known exception.
                    {
                        if (ExceptionHandling.NotExpectedException(e))
                            throw;
                        Log.LogErrorWithCodeFromResources("Touch.CannotMakeFileWritable", file, e.Message);
                        return false;
                    }
                }
            }

            // Do the actual touch operation
            bool retVal = true;
            try
            {
                fileSetLastAccessTime(file, dt);
                fileSetLastWriteTime(file, dt);
            }
            catch (Exception e) // Catching Exception, but rethrowing unless it's a well-known exception.
            {
                if (ExceptionHandling.NotExpectedException(e))
                    throw;
                Log.LogErrorWithCodeFromResources("Touch.CannotTouch", file, e.Message);
                return false;
            }
            finally
            {
                if (needToRestoreAttributes)
                {
                    // Attempt to restore the attributes.  If we fail here, then there is 
                    // not much we can do.
                    try
                    {
                        fileSetAttributes(file, faOriginal);
                    }
                    catch (Exception e) // Catching Exception, but rethrowing unless it's a well-known exception.
                    {
                        if (ExceptionHandling.NotExpectedException(e))
                            throw;
                        Log.LogErrorWithCodeFromResources("Touch.CannotRestoreAttributes", file, e.Message);
                        retVal = false;
                    }
                }
            }

            return retVal;
        }
 private bool TouchFile(string file, DateTime dt, Microsoft.Build.Shared.FileExists fileExists, Microsoft.Build.Shared.FileCreate fileCreate, GetAttributes fileGetAttributes, SetAttributes fileSetAttributes, SetLastAccessTime fileSetLastAccessTime, SetLastWriteTime fileSetLastWriteTime)
 {
     if (!fileExists(file))
     {
         if (!this.AlwaysCreate)
         {
             base.Log.LogErrorWithCodeFromResources("Touch.FileDoesNotExist", new object[] { file });
             return false;
         }
         base.Log.LogMessageFromResources(MessageImportance.Normal, "Touch.CreatingFile", new object[] { file, "AlwaysCreate" });
         if (!this.CreateFile(file, fileCreate))
         {
             return false;
         }
     }
     else
     {
         base.Log.LogMessageFromResources(MessageImportance.Normal, "Touch.Touching", new object[] { file });
     }
     bool flag = false;
     FileAttributes attributes = fileGetAttributes(file);
     if (((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) && this.ForceTouch)
     {
         try
         {
             FileAttributes attributes2 = attributes & ~FileAttributes.ReadOnly;
             fileSetAttributes(file, attributes2);
             flag = true;
         }
         catch (Exception exception)
         {
             if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception))
             {
                 throw;
             }
             base.Log.LogErrorWithCodeFromResources("Touch.CannotMakeFileWritable", new object[] { file, exception.Message });
             return false;
         }
     }
     bool flag2 = true;
     try
     {
         fileSetLastAccessTime(file, dt);
         fileSetLastWriteTime(file, dt);
     }
     catch (Exception exception2)
     {
         if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception2))
         {
             throw;
         }
         base.Log.LogErrorWithCodeFromResources("Touch.CannotTouch", new object[] { file, exception2.Message });
         return false;
     }
     finally
     {
         if (flag)
         {
             try
             {
                 fileSetAttributes(file, attributes);
             }
             catch (Exception exception3)
             {
                 if (Microsoft.Build.Shared.ExceptionHandling.NotExpectedException(exception3))
                 {
                     throw;
                 }
                 base.Log.LogErrorWithCodeFromResources("Touch.CannotRestoreAttributes", new object[] { file, exception3.Message });
                 flag2 = false;
             }
         }
     }
     return flag2;
 }