Example #1
0
        public static bool SetFileTime(string path, int type, long creation_time, long last_access_time, long last_write_time, DateTime dateTime, out MonoIOError error)
        {
            IntPtr intPtr = MonoIO.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, FileOptions.None, out error);

            if (intPtr == MonoIO.InvalidHandle)
            {
                return(false);
            }
            switch (type)
            {
            case 1:
                creation_time = dateTime.ToFileTime();
                break;

            case 2:
                last_access_time = dateTime.ToFileTime();
                break;

            case 3:
                last_write_time = dateTime.ToFileTime();
                break;
            }
            bool        result = MonoIO.SetFileTime(intPtr, creation_time, last_access_time, last_write_time, out error);
            MonoIOError monoIOError;

            MonoIO.Close(intPtr, out monoIOError);
            return(result);
        }
Example #2
0
        internal FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool anonymous, FileOptions options)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Length == 0)
            {
                throw new ArgumentException("Path is empty");
            }

            this.anonymous = anonymous;
            // ignore the Inheritable flag
            share &= ~FileShare.Inheritable;

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize", "Positive number required.");
            }

            if (mode < FileMode.CreateNew || mode > FileMode.Append)
            {
#if NET_2_1
                if (anonymous)
                {
                    throw new ArgumentException("mode", "Enum value was out of legal range.");
                }
                else
#endif
                throw new ArgumentOutOfRangeException("mode", "Enum value was out of legal range.");
            }

            if (access < FileAccess.Read || access > FileAccess.ReadWrite)
            {
                throw new ArgumentOutOfRangeException("access", "Enum value was out of legal range.");
            }

            if (share < FileShare.None || share > (FileShare.ReadWrite | FileShare.Delete))
            {
                throw new ArgumentOutOfRangeException("share", "Enum value was out of legal range.");
            }

            if (path.IndexOfAny(Path.InvalidPathChars) != -1)
            {
                throw new ArgumentException("Name has invalid chars");
            }

            if (Directory.Exists(path))
            {
                // don't leak the path information for isolated storage
                string msg = Locale.GetText("Access to the path '{0}' is denied.");
                throw new UnauthorizedAccessException(String.Format(msg, GetSecureFileName(path, false)));
            }

            /* Append streams can't be read (see FileMode
             * docs)
             */
            if (mode == FileMode.Append &&
                (access & FileAccess.Read) == FileAccess.Read)
            {
                throw new ArgumentException("Append access can be requested only in write-only mode.");
            }

            if ((access & FileAccess.Write) == 0 &&
                (mode != FileMode.Open && mode != FileMode.OpenOrCreate))
            {
                string msg = Locale.GetText("Combining FileMode: {0} with " +
                                            "FileAccess: {1} is invalid.");
                throw new ArgumentException(string.Format(msg, access, mode));
            }

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

            string dname;
            if (Path.DirectorySeparatorChar != '/' && path.IndexOf('/') >= 0)
            {
                dname = Path.GetDirectoryName(Path.GetFullPath(path));
            }
            else
            {
                dname = Path.GetDirectoryName(path);
            }
            if (dname.Length > 0)
            {
                string fp = Path.GetFullPath(dname);
                if (!Directory.Exists(fp))
                {
                    // don't leak the path information for isolated storage
                    string msg   = Locale.GetText("Could not find a part of the path \"{0}\".");
                    string fname = (anonymous) ? dname : Path.GetFullPath(path);
                    throw new DirectoryNotFoundException(String.Format(msg, fname));
                }
            }

            if (access == FileAccess.Read && mode != FileMode.Create && mode != FileMode.OpenOrCreate &&
                mode != FileMode.CreateNew && !File.Exists(path))
            {
                // don't leak the path information for isolated storage
                string msg   = Locale.GetText("Could not find file \"{0}\".");
                string fname = GetSecureFileName(path);
                throw new FileNotFoundException(String.Format(msg, fname), fname);
            }

            // IsolatedStorage needs to keep the Name property to the default "[Unknown]"
            if (!anonymous)
            {
                this.name = path;
            }

            // TODO: demand permissions

            MonoIOError error;

            var nativeHandle = MonoIO.Open(path, mode, access, share, options, out error);

            if (nativeHandle == MonoIO.InvalidHandle)
            {
                // don't leak the path information for isolated storage
                throw MonoIO.GetException(GetSecureFileName(path), error);
            }

            this.safeHandle = new SafeFileHandle(nativeHandle, false);

            this.access = access;
            this.owner  = true;

            /* Can we open non-files by name? */

            if (MonoIO.GetFileType(safeHandle, out error) == MonoFileType.Disk)
            {
                this.canseek = true;
                this.async   = (options & FileOptions.Asynchronous) != 0;
            }
            else
            {
                this.canseek = false;
                this.async   = false;
            }


            if (access == FileAccess.Read && canseek && (bufferSize == DefaultBufferSize))
            {
                /* Avoid allocating a large buffer for small files */
                long len = Length;
                if (bufferSize > len)
                {
                    bufferSize = (int)(len < 1000 ? 1000 : len);
                }
            }

            InitBuffer(bufferSize, false);

            if (mode == FileMode.Append)
            {
                this.Seek(0, SeekOrigin.End);
                this.append_startpos = this.Position;
            }
            else
            {
                this.append_startpos = 0;
            }
        }