Exemple #1
0
        public static bool Exists(string path)
        {
            if (path == null)
            {
                return(false);
            }

            MonoIOError error;
            bool        exists;

            exists = MonoIO.ExistsDirectory(path, out error);
            if (error != MonoIOError.ERROR_SUCCESS &&
                error != MonoIOError.ERROR_PATH_NOT_FOUND &&
                error != MonoIOError.ERROR_INVALID_HANDLE &&
                error != MonoIOError.ERROR_ACCESS_DENIED)
            {
                // INVALID_HANDLE might happen if the file is moved
                // while testing for the existence, a kernel issue
                // according to Larry Ewing.

                throw MonoIO.GetException(path, error);
            }

            return(exists);
        }
Exemple #2
0
        public static bool Exists(string path)
        {
            if (path == null)
            {
                return(false);
            }

            // on Moonlight this does not throw but returns false
            if (!SecurityManager.CheckElevatedPermissions())
            {
                return(false);
            }

            string full_path;

            try {
                full_path = Path.GetFullPath(path);
            } catch {
                return(false);
            }

            MonoIOError error;
            bool        exists;

            exists = MonoIO.ExistsDirectory(full_path, out error);
            /* This should not throw exceptions */
            return(exists);
        }
Exemple #3
0
        public static bool Exists(string path)
        {
            if (path == null)
            {
                return(false);
            }

            MonoIOError error;
            bool        exists;

            exists = MonoIO.ExistsDirectory(path, out error);
            /* This should not throw exceptions */
            return(exists);
        }
Exemple #4
0
        // private

        // Does the common validation, searchPattern has already been checked for not-null
        static string ValidateDirectoryListing(string path, string searchPattern, out bool stop)
        {
            Path.Validate(path);

            string wild     = Path.Combine(path, searchPattern);
            string wildpath = Path.GetDirectoryName(wild);

            if (wildpath.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("Pattern contains invalid characters", "pattern");
            }

            MonoIOError error;

            if (!MonoIO.ExistsDirectory(wildpath, out error))
            {
                if (error == MonoIOError.ERROR_SUCCESS)
                {
                    MonoIOError file_error;
                    if (MonoIO.ExistsFile(wildpath, out file_error))
                    {
                        stop = true;
                        return(wildpath);
                    }
                }

                if (error != MonoIOError.ERROR_PATH_NOT_FOUND)
                {
                    throw MonoIO.GetException(wildpath, error);
                }

                if (wildpath.IndexOfAny(SearchPattern.WildcardChars) == -1)
                {
                    throw new DirectoryNotFoundException("Directory '" + wildpath + "' not found.");
                }

                if (path.IndexOfAny(SearchPattern.WildcardChars) == -1)
                {
                    throw new ArgumentException("Pattern is invalid", "searchPattern");
                }

                throw new ArgumentException("Path is invalid", "path");
            }

            stop = false;
            return(wild);
        }
        /// <summary>Permanently deletes a file.</summary>
        /// <exception cref="T:System.IO.IOException">The target file is open or memory-mapped on a computer running Microsoft Windows NT. </exception>
        /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
        /// <exception cref="T:System.UnauthorizedAccessException">The path is a directory. </exception>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        /// </PermissionSet>
        public override void Delete()
        {
            MonoIOError error;

            if (!MonoIO.Exists(this.FullPath, out error))
            {
                return;
            }
            if (MonoIO.ExistsDirectory(this.FullPath, out error))
            {
                throw new UnauthorizedAccessException("Access to the path \"" + this.FullPath + "\" is denied.");
            }
            if (!MonoIO.DeleteFile(this.FullPath, out error))
            {
                throw MonoIO.GetException(this.OriginalPath, error);
            }
        }
Exemple #6
0
        // file methods

        public override void Delete()
        {
            MonoIOError error;

            if (!MonoIO.Exists(FullPath, out error))
            {
                // a weird MS.NET behaviour
                return;
            }

            if (MonoIO.ExistsDirectory(FullPath, out error))
            {
                throw new UnauthorizedAccessException("Access to the path \"" + FullPath + "\" is denied.");
            }
            if (!MonoIO.DeleteFile(FullPath, out error))
            {
                throw MonoIO.GetException(OriginalPath, error);
            }
        }
Exemple #7
0
        // file methods

        public override void Delete()
        {
            MonoIOError error;

            SecurityManager.EnsureElevatedPermissions();              // this is a no-op outside moonlight

            if (!MonoIO.Exists(FullPath, out error))
            {
                // a weird MS.NET behaviour
                return;
            }

            if (MonoIO.ExistsDirectory(FullPath, out error))
            {
                throw new UnauthorizedAccessException("Access to the path \"" + FullPath + "\" is denied.");
            }
            if (!MonoIO.DeleteFile(FullPath, out error))
            {
                throw MonoIO.GetException(OriginalPath, error);
            }
        }
Exemple #8
0
        public override void Delete()
        {
#if FEATURE_MONO_CAS
#if FEATURE_CORECLR
            FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, DisplayPath, FullPath);
            state.EnsureState();
#else
            // For security check, path should be resolved to an absolute path.
            FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, FullPath, false, false);
#endif
#endif

#if MONO
            MonoIOError error;

            if (MonoIO.ExistsDirectory(FullPath, out error))
            {
                __Error.WinIOError(Win32Native.ERROR_ACCESS_DENIED, DisplayPath);
            }

            if (!MonoIO.DeleteFile(FullPath, out error))
            {
                int hr = (int)error;
#else
            bool r = Win32Native.DeleteFile(FullPath);
            if (!r)
            {
                int hr = Marshal.GetLastWin32Error();
#endif
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return;
                }
                else
                {
                    __Error.WinIOError(hr, DisplayPath);
                }
            }
        }
Exemple #9
0
        public static void Replace(string sourceFileName,
                                   string destinationFileName,
                                   string destinationBackupFileName,
                                   bool ignoreMetadataErrors)
        {
            MonoIOError error;

            if (sourceFileName == null)
            {
                throw new ArgumentNullException("sourceFileName");
            }
            if (destinationFileName == null)
            {
                throw new ArgumentNullException("destinationFileName");
            }
            if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("sourceFileName");
            }
            if (destinationFileName.Trim().Length == 0 || destinationFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("destinationFileName");
            }

            string fullSource = Path.GetFullPath(sourceFileName);
            string fullDest   = Path.GetFullPath(destinationFileName);

            if (MonoIO.ExistsDirectory(fullSource, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", sourceFileName));
            }
            if (MonoIO.ExistsDirectory(fullDest, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", destinationFileName));
            }

            if (!Exists(fullSource))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName),
                                                sourceFileName);
            }
            if (!Exists(fullDest))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", destinationFileName),
                                                destinationFileName);
            }
            if (fullSource == fullDest)
            {
                throw new IOException(Locale.GetText("Source and destination arguments are the same file."));
            }

            string fullBackup = null;

            if (destinationBackupFileName != null)
            {
                if (destinationBackupFileName.Trim().Length == 0 ||
                    destinationBackupFileName.IndexOfAny(Path.InvalidPathChars) != -1)
                {
                    throw new ArgumentException("destinationBackupFileName");
                }

                fullBackup = Path.GetFullPath(destinationBackupFileName);
                if (MonoIO.ExistsDirectory(fullBackup, out error))
                {
                    throw new IOException(Locale.GetText("{0} is a directory", destinationBackupFileName));
                }
                if (fullSource == fullBackup)
                {
                    throw new IOException(Locale.GetText("Source and backup arguments are the same file."));
                }
                if (fullDest == fullBackup)
                {
                    throw new IOException(Locale.GetText(
                                              "Destination and backup arguments are the same file."));
                }
            }

            var attrs = GetAttributes(fullDest);

            // TODO: Should be done in wapi, win32 api handles this already
            if ((attrs & FileAttributes.ReadOnly) != 0)
            {
                throw MonoIO.GetException(MonoIOError.ERROR_ACCESS_DENIED);
            }

            if (!MonoIO.ReplaceFile(fullSource, fullDest, fullBackup,
                                    ignoreMetadataErrors, out error))
            {
                throw MonoIO.GetException(error);
            }
        }
Exemple #10
0
        public static void Replace(string sourceFileName,
                                   string destinationFileName,
                                   string destinationBackupFileName,
                                   bool ignoreMetadataErrors)
        {
            MonoIOError error;

            if (sourceFileName == null)
            {
                throw new ArgumentNullException("sourceFileName");
            }
            if (destinationFileName == null)
            {
                throw new ArgumentNullException("destinationFileName");
            }
            if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("sourceFileName");
            }
            if (destinationFileName.Trim().Length == 0 || destinationFileName.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("destinationFileName");
            }

            string fullSource = Path.GetFullPath(sourceFileName);
            string fullDest   = Path.GetFullPath(destinationFileName);

            if (MonoIO.ExistsDirectory(fullSource, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", sourceFileName));
            }
            if (MonoIO.ExistsDirectory(fullDest, out error))
            {
                throw new IOException(Locale.GetText("{0} is a directory", destinationFileName));
            }

            if (!Exists(fullSource))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName),
                                                sourceFileName);
            }
            if (!Exists(fullDest))
            {
                throw new FileNotFoundException(Locale.GetText("{0} does not exist", destinationFileName),
                                                destinationFileName);
            }
            if (fullSource == fullDest)
            {
                throw new IOException(Locale.GetText("Source and destination arguments are the same file."));
            }

            string fullBackup = null;

            if (destinationBackupFileName != null)
            {
                if (destinationBackupFileName.Trim().Length == 0 ||
                    destinationBackupFileName.IndexOfAny(Path.InvalidPathChars) != -1)
                {
                    throw new ArgumentException("destinationBackupFileName");
                }

                fullBackup = Path.GetFullPath(destinationBackupFileName);
                if (MonoIO.ExistsDirectory(fullBackup, out error))
                {
                    throw new IOException(Locale.GetText("{0} is a directory", destinationBackupFileName));
                }
                if (fullSource == fullBackup)
                {
                    throw new IOException(Locale.GetText("Source and backup arguments are the same file."));
                }
                if (fullDest == fullBackup)
                {
                    throw new IOException(Locale.GetText(
                                              "Destination and backup arguments are the same file."));
                }
            }

            if (!MonoIO.ReplaceFile(fullSource, fullDest, fullBackup,
                                    ignoreMetadataErrors, out error))
            {
                throw MonoIO.GetException(error);
            }
        }
Exemple #11
0
        private static string [] GetFileSystemEntries(string path, string searchPattern, FileAttributes mask, FileAttributes attrs)
        {
            if (path == null || searchPattern == null)
            {
                throw new ArgumentNullException();
            }

            if (searchPattern.Length == 0)
            {
                return new string [] {}
            }
            ;

            if (path.Trim().Length == 0)
            {
                throw new ArgumentException("The Path does not have a valid format");
            }

            string wild     = Path.Combine(path, searchPattern);
            string wildpath = Path.GetDirectoryName(wild);

            if (wildpath.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("Path contains invalid characters");
            }

            if (wildpath.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                if (path.IndexOfAny(SearchPattern.InvalidChars) == -1)
                {
                    throw new ArgumentException("Path contains invalid characters", "path");
                }

                throw new ArgumentException("Pattern contains invalid characters", "pattern");
            }

            MonoIOError error;

            if (!MonoIO.ExistsDirectory(wildpath, out error))
            {
                if (error == MonoIOError.ERROR_SUCCESS)
                {
                    MonoIOError file_error;
                    if (MonoIO.ExistsFile(wildpath, out file_error))
                    {
                        return(new string [] { wildpath });
                    }
                }

                if (error != MonoIOError.ERROR_PATH_NOT_FOUND)
                {
                    throw MonoIO.GetException(wildpath, error);
                }

                if (wildpath.IndexOfAny(SearchPattern.WildcardChars) == -1)
                {
                    throw new DirectoryNotFoundException("Directory '" + wildpath + "' not found.");
                }

                if (path.IndexOfAny(SearchPattern.WildcardChars) == -1)
                {
                    throw new ArgumentException("Pattern is invalid", "searchPattern");
                }

                throw new ArgumentException("Path is invalid", "path");
            }

            string path_with_pattern = Path.Combine(wildpath, searchPattern);

            string [] result = MonoIO.GetFileSystemEntries(path, path_with_pattern, (int)attrs, (int)mask, out error);
            if (error != 0)
            {
                throw MonoIO.GetException(wildpath, error);
            }

            return(result);
        }