Beispiel #1
0
        public override int ReadByte()
        {
            if (!CanRead)
            {
                throw new NotSupportedException("The stream does not support reading");
            }

            byte[] buffer = new byte[1];
            ulong  bytesRead;
            Result result;

            if (async)
            {
                Async.Read(handle, out buffer[0], 1, readCallback);
                Wait();
                result = asyncResult;
            }
            else
            {
                result = Sync.Read(handle, out buffer[0], 1UL, out bytesRead);
            }
            if (result == Result.ErrorEof)
            {
                return(-1);
            }

            Vfs.ThrowException(Uri, result);
            return(buffer[0]);
        }
Beispiel #2
0
        private void AsyncWrite(Handle handle, Result result, byte[] buffer,
                                ulong bytesRequested, ulong bytesWritten)
        {
            if (result == Result.Ok)
            {
                bytesRemaining -= (int)bytesWritten;
                if (bytesRemaining > 0)
                {
                    Async.Write(handle, out buffer[offset + count - bytesRemaining],
                                (uint)bytesRemaining, new AsyncWriteCallback(AsyncWrite));
                }
                else if (cback != null)
                {
                    asyncResult.SetComplete(null, count);
                    cback(asyncResult);
                }
            }
            else if (result == Result.ErrorEof)
            {
                bytesRemaining -= (int)bytesWritten;
                asyncResult.SetComplete(null, count - bytesRemaining);

                if (cback != null)
                {
                    cback(asyncResult);
                }
            }
            else if (cback != null)
            {
                Exception e = new IOException(Vfs.ResultToString(result));
                asyncResult.SetComplete(e, -1);
                cback(asyncResult);
            }
        }
Beispiel #3
0
        public static Handle Open(Uri uri, OpenMode mode)
        {
            IntPtr handle = IntPtr.Zero;
            Result result = gnome_vfs_open_uri(out handle, uri.Handle, mode);

            if (result != Result.Ok)
            {
                Vfs.ThrowException(uri, result);
                return(null);
            }
            else
            {
                return(new Handle(handle));
            }
        }
Beispiel #4
0
        public static Handle Create(Uri uri, OpenMode mode, bool exclusive, FilePermissions perm)
        {
            IntPtr handle = IntPtr.Zero;
            Result result = gnome_vfs_create_uri(out handle, uri.Handle, mode, exclusive, (uint)perm);

            if (result != Result.Ok)
            {
                Vfs.ThrowException(uri, result);
                return(null);
            }
            else
            {
                return(new Handle(handle));
            }
        }
Beispiel #5
0
        public override void SetLength(long length)
        {
            if (!CanSeek)
            {
                throw new NotSupportedException("The stream does not support seeking");
            }
            else if (!CanWrite)
            {
                throw new NotSupportedException("The stream does not support writing");
            }

            Result result = Sync.Truncate(handle, (ulong)length);

            Vfs.ThrowException(Uri, result);
        }
Beispiel #6
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            if (!CanSeek)
            {
                throw new NotSupportedException("The stream does not support seeking");
            }
            if (IsAsync && origin == SeekOrigin.Current)
            {
                throw new NotSupportedException("Cannot tell what the offset is in async mode");
            }

            SeekPosition seekPos   = SeekPosition.Start;
            long         newOffset = -1;

            switch (origin)
            {
            case SeekOrigin.Begin:
                seekPos   = SeekPosition.Start;
                newOffset = offset;
                break;

            case SeekOrigin.Current:
                seekPos = SeekPosition.Current;
                break;

            case SeekOrigin.End:
                seekPos   = SeekPosition.End;
                newOffset = Length + offset;
                break;
            }

            Result result;

            if (async)
            {
                Async.Seek(handle, seekPos, offset, callback);
                Wait();
                result = asyncResult;
            }
            else
            {
                result = Sync.Seek(handle, seekPos, offset);
            }
            Vfs.ThrowException(Uri, result);
            return(newOffset);
        }
Beispiel #7
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            else if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "Must be >= 0");
            }
            else if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Must be >= 0");
            }
            else if (count > buffer.Length - offset)
            {
                throw new ArgumentException("Buffer too small, count/offset wrong");
            }
            else if (!CanRead)
            {
                throw new NotSupportedException("The stream does not support reading");
            }

            ulong  bytesRead;
            Result result;

            if (async)
            {
                Async.Read(handle, out buffer[offset], (uint)count, readCallback);
                Wait();
                result    = asyncResult;
                bytesRead = asyncBytesRead;
            }
            else
            {
                result = Sync.Read(handle, out buffer[offset], (ulong)count, out bytesRead);
            }
            if (result == Result.ErrorEof)
            {
                return(0);
            }

            Vfs.ThrowException(Uri, result);
            return((int)bytesRead);
        }
Beispiel #8
0
        public static FileInfo[] GetEntries(string text_uri, FileInfoOptions options)
        {
            IntPtr raw_ret;
            Result result = gnome_vfs_directory_list_load(out raw_ret, text_uri, options);

            Vfs.ThrowException(text_uri, result);

            GLib.List list = new GLib.List(raw_ret, typeof(IntPtr));
            list.Managed = true;
            FileInfo[] entries = new FileInfo [list.Count];
            int        i       = 0;

            foreach (IntPtr info in list)
            {
                entries[i++] = new FileInfo(info);
            }

            return(entries);
        }
Beispiel #9
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            else if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "Must be >= 0");
            }
            else if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Must be >= 0");
            }
            else if (count > buffer.Length - offset)
            {
                throw new ArgumentException("Buffer too small, count/offset wrong");
            }
            else if (!CanWrite)
            {
                throw new NotSupportedException("The stream does not support writing");
            }

            ulong  bytesWritten;
            Result result;

            if (async)
            {
                Async.Write(handle, out buffer[offset], (uint)count, writeCallback);
                Wait();
                result       = asyncResult;
                bytesWritten = asyncBytesWritten;
            }
            else
            {
                result = Sync.Write(handle, out buffer[offset], (ulong)count, out bytesWritten);
            }
            Vfs.ThrowException(Uri, result);
        }
Beispiel #10
0
        private void AsyncRead(Handle handle, Result result, byte[] buf,
                               ulong bytesRequested, ulong bytesRead)
        {
            if (result == Result.Ok)
            {
                Array.Copy(buf, 0, buffer, offset + count - bytesRemaining, (int)bytesRead);
                bytesRemaining -= (int)bytesRead;
                if (bytesRemaining > 0)
                {
                    buf = new byte[bytesRemaining];
                    Async.Read(handle, out buf[0], (uint)bytesRemaining,
                               new AsyncReadCallback(AsyncRead));
                }
                else if (cback != null)
                {
                    asyncResult.SetComplete(null, count);
                    cback(asyncResult);
                }
            }
            else if (result == Result.ErrorEof)
            {
                Array.Copy(buf, 0, buffer, offset + count - bytesRemaining, (int)bytesRead);
                bytesRemaining -= (int)bytesRead;
                asyncResult.SetComplete(null, count - bytesRemaining);

                if (cback != null)
                {
                    cback(asyncResult);
                }
            }
            else if (cback != null)
            {
                Exception e = new IOException(Vfs.ResultToString(result));
                asyncResult.SetComplete(e, -1);
                cback(asyncResult);
            }
        }
Beispiel #11
0
        public override void WriteByte(byte value)
        {
            if (!CanWrite)
            {
                throw new NotSupportedException("The stream does not support writing");
            }

            byte[] buffer = new byte[1];
            buffer[0] = value;
            ulong  bytesWritten;
            Result result;

            if (async)
            {
                Async.Write(handle, out buffer[0], 1, writeCallback);
                Wait();
                result = asyncResult;
            }
            else
            {
                result = Sync.Write(handle, out buffer[0], 1UL, out bytesWritten);
            }
            Vfs.ThrowException(Uri, result);
        }
Beispiel #12
0
        public FileInfo(Uri uri, FileInfoOptions options) : this()
        {
            Result result = gnome_vfs_get_file_info_uri(uri.Handle, Handle, (int)options);

            Vfs.ThrowException(uri, result);
        }
 public VfsException(Result result)
     : base(Vfs.ResultToString(result))
 {
     this.result = result;
 }
Beispiel #14
0
        public override void Close()
        {
            Result result = Sync.Close(handle);

            Vfs.ThrowException(Uri, result);
        }
Beispiel #15
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;
        }