Exemplo n.º 1
0
 public VfsStreamAsyncResult BeginWrite()
 {
     asyncResult = new VfsStreamAsyncResult(state);
     Async.Write(handle, out buffer[offset], (uint)count, new AsyncWriteCallback(AsyncWrite));
     return(asyncResult);
 }
Exemplo n.º 2
0
        public VfsStream(string text_uri, FileMode mode, bool async)
        {
            if (text_uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            if (text_uri == "")
            {
                throw new ArgumentNullException("Uri is empty");
            }

            if (mode < FileMode.CreateNew || mode > FileMode.Append)
            {
                throw new ArgumentOutOfRangeException("mode");
            }

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

            uri = new Gnome.Vfs.Uri(text_uri);

            if (mode == FileMode.Open && !uri.Exists)
            {
                throw new FileNotFoundException("Could not find uri \"" + text_uri + "\".");
            }

            if (mode == FileMode.CreateNew)
            {
                string dname = uri.ExtractDirname();
                if (dname != "" && !new Uri(dname).Exists)
                {
                    throw new DirectoryNotFoundException("Could not find a part of " +
                                                         "the path \"" + dname + "\".");
                }
            }

            if (async)
            {
                callback      = new AsyncCallback(OnAsyncCallback);
                readCallback  = new AsyncReadCallback(OnAsyncReadCallback);
                writeCallback = new AsyncWriteCallback(OnAsyncWriteCallback);
            }

            OpenMode om = OpenMode.None;

            switch (mode)
            {
            case FileMode.CreateNew:
            case FileMode.Create:
            case FileMode.Truncate:
            case FileMode.Append:
                om     = OpenMode.Write;
                access = FileAccess.Write;
                break;

            case FileMode.OpenOrCreate:
                if (uri.Exists)
                {
                    om     = OpenMode.Read;
                    access = FileAccess.Read;
                }
                else
                {
                    om     = OpenMode.Write;
                    access = FileAccess.Write;
                }
                break;

            case FileMode.Open:
                om     = OpenMode.Read;
                access = FileAccess.Read;
                break;
            }

            /* 644 */
            FilePermissions perms = FilePermissions.UserRead |
                                    FilePermissions.UserWrite |
                                    FilePermissions.GroupRead |
                                    FilePermissions.OtherRead;

            Result result;

            handle = null;
            switch (mode)
            {
            case FileMode.Append:
                if (uri.Exists)
                {
                    if (async)
                    {
                        handle = Async.Open(uri, om,
                                            (int)Async.Priority.Default,
                                            callback);
                        Wait();
                        Async.Seek(handle, SeekPosition.End, 0, callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Open(uri, om);
                        result = Sync.Seek(handle, SeekPosition.End, 0);
                        Vfs.ThrowException(uri, result);
                    }
                }
                else
                {
                    if (async)
                    {
                        handle = Async.Create(uri, om, true, perms,
                                              (int)Async.Priority.Default,
                                              callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Create(uri, om, true, perms);
                    }
                }
                break;

            case FileMode.Create:
                if (uri.Exists)
                {
                    if (async)
                    {
                        handle = Async.Open(uri, om,
                                            (int)Async.Priority.Default,
                                            callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Open(uri, om);
                        result = uri.Truncate(0);
                        Vfs.ThrowException(uri, result);
                    }
                }
                else
                {
                    handle = Sync.Create(uri, om, true, perms);
                }
                break;

            case FileMode.CreateNew:
                if (uri.Exists)
                {
                    throw new IOException("Uri \"" + text_uri + "\" already exists.");
                }
                else
                {
                    if (async)
                    {
                        handle = Async.Create(uri, om, true, perms,
                                              (int)Async.Priority.Default,
                                              callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Create(uri, om, true, perms);
                    }
                }
                break;

            case FileMode.Open:
                if (uri.Exists)
                {
                    if (async)
                    {
                        handle = Async.Open(uri, om,
                                            (int)Async.Priority.Default,
                                            callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Open(uri, om);
                    }
                }
                else
                {
                    throw new FileNotFoundException(text_uri);
                }
                break;

            case FileMode.OpenOrCreate:
                if (uri.Exists)
                {
                    if (async)
                    {
                        handle = Async.Open(uri, om,
                                            (int)Async.Priority.Default,
                                            callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Open(uri, om);
                    }
                }
                else
                {
                    if (async)
                    {
                        handle = Async.Create(uri, om, true, perms,
                                              (int)Async.Priority.Default,
                                              callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Create(uri, om, true, perms);
                    }
                }
                break;

            case FileMode.Truncate:
                if (uri.Exists)
                {
                    result = uri.Truncate(0);
                    if (async)
                    {
                        handle = Async.Open(uri, om,
                                            (int)Async.Priority.Default,
                                            callback);
                        Wait();
                    }
                    else
                    {
                        handle = Sync.Open(uri, om);
                        Vfs.ThrowException(uri, result);
                    }
                }
                else
                {
                    throw new FileNotFoundException(text_uri);
                }
                break;
            }

            this.mode    = mode;
            this.canseek = true;
            this.async   = async;
        }