private bool Refresh(bool throwException)
        {
            int r = Native.Syscall.statvfs(mount_point, out stat);

            if (r == -1 && throwException)
            {
                Native.Errno e = Native.Syscall.GetLastError();
                throw new InvalidOperationException(
                          UnixMarshal.GetErrorDescription(e),
                          new UnixIOException(e));
            }
            else if (r == -1)
            {
                return(false);
            }
            return(true);
        }
예제 #2
0
        public UnixStream Open(Native.OpenFlags flags)
        {
            if ((flags & Native.OpenFlags.O_CREAT) != 0)
            {
                throw new ArgumentException(
                          "Cannot specify OpenFlags.O_CREAT without providing " +
                          "FilePermissions.  Use the Open(OpenFlags, FilePermissions) " +
                          "method instead");
            }
            int fd = Native.Syscall.open(FullPath, flags);

            if (fd < 0)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            return(new UnixStream(fd));
        }
예제 #3
0
        public static String GetString(String s)
        {
            IntPtr ints = UnixMarshal.StringToHeap(s);

            try {
                // gettext(3) returns the input pointer if no translation is found
                IntPtr r = gettext(ints);
                if (r != ints)
                {
                    return(UnixMarshal.PtrToStringUnix(r));
                }
                return(s);
            }
            finally {
                UnixMarshal.FreeHeap(ints);
            }
        }
        public Dirent[] GetEntries(Regex regex)
        {
            Dirent[] entries;
            IntPtr   intPtr = Syscall.opendir(base.FullPath);

            if (intPtr == IntPtr.Zero)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            try
            {
                entries = UnixDirectoryInfo.GetEntries(intPtr, regex);
            }
            finally
            {
                UnixMarshal.ThrowExceptionForLastErrorIf(Syscall.closedir(intPtr));
            }
            return(entries);
        }
예제 #5
0
        public override void SetLength(long value)
        {
            int num;

            this.AssertNotDisposed();
            if (value < (long)0)
            {
                throw new ArgumentOutOfRangeException("value", "< 0");
            }
            if (!this.CanSeek && !this.CanWrite)
            {
                throw new NotSupportedException("You can't truncating the current file descriptor");
            }
            do
            {
                num = Syscall.ftruncate(this.fileDescriptor, value);
            }while (UnixMarshal.ShouldRetrySyscall(num));
            UnixMarshal.ThrowExceptionForLastErrorIf(num);
        }
        public Native.Dirent[] GetEntries(Regex regex)
        {
            IntPtr dirp = Native.Syscall.opendir(FullPath);

            if (dirp == IntPtr.Zero)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }

            try
            {
                return(GetEntries(dirp, regex));
            }
            finally
            {
                int r = Native.Syscall.closedir(dirp);
                UnixMarshal.ThrowExceptionForLastErrorIf(r);
            }
        }
예제 #7
0
        private static IntPtr Fopen(string path, string mode)
        {
            if (path.Length == 0)
            {
                throw new ArgumentException("path");
            }
            if (mode == null)
            {
                throw new ArgumentNullException("mode");
            }
            IntPtr f = Native.Stdlib.fopen(path, mode);

            if (f == IntPtr.Zero)
            {
                throw new DirectoryNotFoundException("path",
                                                     UnixMarshal.CreateExceptionForLastError());
            }
            return(f);
        }
예제 #8
0
        private static int GetStringByteLength(IntPtr p, Encoding encoding)
        {
            Type type = encoding.GetType();
            int  num  = -1;

            if (typeof(UTF8Encoding).IsAssignableFrom(type) || typeof(UTF7Encoding).IsAssignableFrom(type) || typeof(UnixEncoding).IsAssignableFrom(type) || typeof(ASCIIEncoding).IsAssignableFrom(type))
            {
                num = (void *)(checked ((int)Stdlib.strlen(p)));
            }
            else
            {
                num = (!typeof(UnicodeEncoding).IsAssignableFrom(type) ? UnixMarshal.GetRandomBufferLength(p, encoding.GetMaxByteCount(1)) : UnixMarshal.GetInt16BufferLength(p));
            }
            if (num == -1)
            {
                throw new NotSupportedException("Unable to determine native string buffer length");
            }
            return(num);
        }
예제 #9
0
파일: UnixStream.cs 프로젝트: zzwwqqq/mono
        public override void SetLength(long value)
        {
            AssertNotDisposed();
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException("value", "< 0");
            }
            if (!CanSeek && !CanWrite)
            {
                throw new NotSupportedException("You can't truncating the current file descriptor");
            }

            int r;

            do
            {
                r = Native.Syscall.ftruncate(fileDescriptor, value);
            } while (UnixMarshal.ShouldRetrySyscall(r));
            UnixMarshal.ThrowExceptionForLastErrorIf(r);
        }
예제 #10
0
        public static UnixFileSystemInfo GetFileSystemEntry(string path)
        {
            Stat stat;
            int  num = Syscall.lstat(path, out stat);

            if (num == -1 && Stdlib.GetLastError() == Errno.ENOENT)
            {
                return(new UnixFileInfo(path));
            }
            UnixMarshal.ThrowExceptionForLastErrorIf(num);
            if (UnixFileSystemInfo.IsFileType(stat.st_mode, FilePermissions.S_IFDIR))
            {
                return(new UnixDirectoryInfo(path, stat));
            }
            if (UnixFileSystemInfo.IsFileType(stat.st_mode, FilePermissions.S_IFLNK))
            {
                return(new UnixSymbolicLinkInfo(path, stat));
            }
            return(new UnixFileInfo(path, stat));
        }
예제 #11
0
        public override void Close()
        {
            int num;

            if (this.fileDescriptor == -1)
            {
                return;
            }
            this.Flush();
            if (!this.owner)
            {
                return;
            }
            do
            {
                num = Syscall.close(this.fileDescriptor);
            }while (UnixMarshal.ShouldRetrySyscall(num));
            UnixMarshal.ThrowExceptionForLastErrorIf(num);
            this.fileDescriptor = -1;
            GC.SuppressFinalize(this);
        }
예제 #12
0
        public static string GetConfigurationString(ConfstrName name)
        {
            ulong num = Syscall.confstr(name, null, (ulong)0);

            if (num == (long)-1)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            if (num == 0)
            {
                return(string.Empty);
            }
            StringBuilder stringBuilder = new StringBuilder((int)num + 1);

            num = Syscall.confstr(name, stringBuilder, num);
            if (num == (long)-1)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            return(stringBuilder.ToString());
        }
예제 #13
0
        public static UnixFileSystemInfo GetFileSystemEntry(string path)
        {
            Native.Stat stat;
            int         r = Native.Syscall.lstat(path, out stat);

            if (r == -1 && Native.Stdlib.GetLastError() == Native.Errno.ENOENT)
            {
                return(new UnixFileInfo(path));
            }
            UnixMarshal.ThrowExceptionForLastErrorIf(r);

            if (IsFileType(stat.st_mode, Native.FilePermissions.S_IFDIR))
            {
                return(new UnixDirectoryInfo(path, stat));
            }
            else if (IsFileType(stat.st_mode, Native.FilePermissions.S_IFLNK))
            {
                return(new UnixSymbolicLinkInfo(path, stat));
            }
            return(new UnixFileInfo(path, stat));
        }
예제 #14
0
 public override void Close()
 {
     if (this.file == StdioFileStream.InvalidFileStream)
     {
         return;
     }
     if (!this.owner)
     {
         this.Flush();
     }
     else if (Stdlib.fclose(this.file) != 0)
     {
         UnixMarshal.ThrowExceptionForLastError();
     }
     this.file     = StdioFileStream.InvalidFileStream;
     this.canRead  = false;
     this.canSeek  = false;
     this.canWrite = false;
     GC.SuppressFinalize(this);
     GC.KeepAlive(this);
 }
예제 #15
0
        public static string GetConfigurationString(Native.ConfstrName name)
        {
            ulong len = Native.Syscall.confstr(name, null, 0);

            if (len == unchecked ((ulong)(-1)))
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            if (len == 0)
            {
                return("");
            }
            StringBuilder buf = new StringBuilder((int)len + 1);

            len = Native.Syscall.confstr(name, buf, len);
            if (len == unchecked ((ulong)(-1)))
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            return(buf.ToString());
        }
예제 #16
0
        public static UnixUserInfo[] GetLocalUsers()
        {
            ArrayList arrayLists = new ArrayList();
            object    pwdLock    = Syscall.pwd_lock;

            Monitor.Enter(pwdLock);
            try
            {
                if (Syscall.setpwent() != 0)
                {
                    UnixMarshal.ThrowExceptionForLastError();
                }
                try
                {
                    while (true)
                    {
                        Passwd passwd  = Syscall.getpwent();
                        Passwd passwd1 = passwd;
                        if (passwd == null)
                        {
                            break;
                        }
                        arrayLists.Add(new UnixUserInfo(passwd1));
                    }
                    if ((int)Stdlib.GetLastError() != 0)
                    {
                        UnixMarshal.ThrowExceptionForLastError();
                    }
                }
                finally
                {
                    Syscall.endpwent();
                }
            }
            finally
            {
                Monitor.Exit(pwdLock);
            }
            return((UnixUserInfo[])arrayLists.ToArray(typeof(UnixUserInfo)));
        }
예제 #17
0
        public static IntPtr StringToHeap(string s, int index, int count, Encoding encoding)
        {
            if (s == null)
            {
                return(IntPtr.Zero);
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            int maxByteCount = encoding.GetMaxByteCount(1);

            char[] charArray = s.ToCharArray(index, count);
            byte[] numArray  = new byte[encoding.GetByteCount(charArray) + maxByteCount];
            if (encoding.GetBytes(charArray, 0, (int)charArray.Length, numArray, 0) != (int)numArray.Length - maxByteCount)
            {
                throw new NotSupportedException("encoding.GetBytes() doesn't equal encoding.GetByteCount()!");
            }
            IntPtr intPtr = UnixMarshal.AllocHeap((long)((int)numArray.Length));

            if (intPtr == IntPtr.Zero)
            {
                throw new UnixIOException(Errno.ENOMEM);
            }
            bool flag = false;

            try
            {
                Marshal.Copy(numArray, 0, intPtr, (int)numArray.Length);
                flag = true;
            }
            finally
            {
                if (!flag)
                {
                    UnixMarshal.FreeHeap(intPtr);
                }
            }
            return(intPtr);
        }
        public static UnixGroupInfo[] GetLocalGroups()
        {
            ArrayList arrayLists = new ArrayList();
            object    grpLock    = Syscall.grp_lock;

            Monitor.Enter(grpLock);
            try
            {
                if (Syscall.setgrent() != 0)
                {
                    UnixMarshal.ThrowExceptionForLastError();
                }
                try
                {
                    while (true)
                    {
                        Group group  = Syscall.getgrent();
                        Group group1 = group;
                        if (group == null)
                        {
                            break;
                        }
                        arrayLists.Add(new UnixGroupInfo(group1));
                    }
                    if ((int)Stdlib.GetLastError() != 0)
                    {
                        UnixMarshal.ThrowExceptionForLastError();
                    }
                }
                finally
                {
                    Syscall.endgrent();
                }
            }
            finally
            {
                Monitor.Exit(grpLock);
            }
            return((UnixGroupInfo[])arrayLists.ToArray(typeof(UnixGroupInfo)));
        }
예제 #19
0
 public static string[] PtrToStringArray(int count, IntPtr stringArray, Encoding encoding)
 {
     if (count < 0)
     {
         throw new ArgumentOutOfRangeException("count", "< 0");
     }
     if (encoding == null)
     {
         throw new ArgumentNullException("encoding");
     }
     if (stringArray == IntPtr.Zero)
     {
         return(new string[count]);
     }
     string[] str = new string[count];
     for (int i = 0; i < count; i++)
     {
         IntPtr intPtr = Marshal.ReadIntPtr(stringArray, i * IntPtr.Size);
         str[i] = UnixMarshal.PtrToString(intPtr, encoding);
     }
     return(str);
 }
예제 #20
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            AssertNotDisposed();
            if (!CanSeek)
            {
                throw new NotSupportedException("The File Stream does not support seeking");
            }

            Native.SeekFlags sf = Native.SeekFlags.SEEK_CUR;
            switch (origin)
            {
            case SeekOrigin.Begin:   sf = Native.SeekFlags.SEEK_SET; break;

            case SeekOrigin.Current: sf = Native.SeekFlags.SEEK_CUR; break;

            case SeekOrigin.End:     sf = Native.SeekFlags.SEEK_END; break;

            default: throw new ArgumentException("origin");
            }

            int r = Native.Stdlib.fseek(file, offset, sf);

            if (r != 0)
            {
                throw new IOException("Unable to seek",
                                      UnixMarshal.CreateExceptionForLastError());
            }

            long pos = Native.Stdlib.ftell(file);

            if (pos == -1)
            {
                throw new IOException("Unable to get current file position",
                                      UnixMarshal.CreateExceptionForLastError());
            }

            GC.KeepAlive(this);
            return(pos);
        }
        public void Delete(bool recursive)
        {
            if (recursive)
            {
                foreach (UnixFileSystemInfo e in GetFileSystemEntries())
                {
                    UnixDirectoryInfo d = e as UnixDirectoryInfo;
                    if (d != null)
                    {
                        d.Delete(true);
                    }
                    else
                    {
                        e.Delete();
                    }
                }
            }
            int r = Native.Syscall.rmdir(FullPath);

            UnixMarshal.ThrowExceptionForLastErrorIf(r);
            base.Refresh();
        }
 public void Delete(bool recursive)
 {
     if (recursive)
     {
         UnixFileSystemInfo[] fileSystemEntries = this.GetFileSystemEntries();
         for (int i = 0; i < (int)fileSystemEntries.Length; i++)
         {
             UnixFileSystemInfo unixFileSystemInfo = fileSystemEntries[i];
             UnixDirectoryInfo  unixDirectoryInfo  = unixFileSystemInfo as UnixDirectoryInfo;
             if (unixDirectoryInfo == null)
             {
                 unixFileSystemInfo.Delete();
             }
             else
             {
                 unixDirectoryInfo.Delete(true);
             }
         }
     }
     UnixMarshal.ThrowExceptionForLastErrorIf(Syscall.rmdir(base.FullPath));
     base.Refresh();
 }
        private static Dirent[] GetEntries(IntPtr dirp, Regex regex)
        {
            int       num;
            IntPtr    intPtr;
            ArrayList arrayLists = new ArrayList();

            do
            {
                Dirent dirent = new Dirent();
                num = Syscall.readdir_r(dirp, dirent, out intPtr);
                if (num != 0 || !(intPtr != IntPtr.Zero) || !regex.Match(dirent.d_name).Success || !(dirent.d_name != ".") || !(dirent.d_name != ".."))
                {
                    continue;
                }
                arrayLists.Add(dirent);
            }while (num == 0 && intPtr != IntPtr.Zero);
            if (num != 0)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            return((Dirent[])arrayLists.ToArray(typeof(Dirent)));
        }
예제 #24
0
        public override unsafe void Write(byte[] buffer, int offset, int count)
        {
            AssertNotDisposed();
            AssertValidBuffer(buffer, offset, count);
            if (!CanWrite)
            {
                throw new NotSupportedException("File Stream does not support writing");
            }

            ulong r = 0;

            fixed(byte *buf = &buffer[offset])
            {
                r = Native.Stdlib.fwrite(buf, (ulong)1, (ulong)count, file);
            }

            if (r != (ulong)count)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            GC.KeepAlive(this);
        }
예제 #25
0
        public static string[] GetUserShells()
        {
            ArrayList shells = new ArrayList();

            lock (Native.Syscall.usershell_lock) {
                try {
                    if (Native.Syscall.setusershell() != 0)
                    {
                        UnixMarshal.ThrowExceptionForLastError();
                    }
                    string shell;
                    while ((shell = Native.Syscall.getusershell()) != null)
                    {
                        shells.Add(shell);
                    }
                }
                finally {
                    Native.Syscall.endusershell();
                }
            }

            return((string[])shells.ToArray(typeof(string)));
        }
예제 #26
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            unsafe
            {
                this.AssertNotDisposed();
                this.AssertValidBuffer(buffer, offset, count);
                if (!this.CanWrite)
                {
                    throw new NotSupportedException("File Stream does not support writing");
                }
                ulong num = (ulong)0;
                fixed(byte *numPointer = &buffer[offset])
                {
                    num = Stdlib.fwrite(numPointer, (ulong)1, (ulong)count, this.file);
                }

                if (num != (long)count)
                {
                    UnixMarshal.ThrowExceptionForLastError();
                }
                GC.KeepAlive(this);
            }
        }
예제 #27
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            this.AssertNotDisposed();
            if (!this.CanSeek)
            {
                throw new NotSupportedException("The File Descriptor does not support seeking");
            }
            SeekFlags seekFlag = SeekFlags.SEEK_CUR;

            switch (origin)
            {
            case SeekOrigin.Begin:
            {
                seekFlag = SeekFlags.SEEK_SET;
                break;
            }

            case SeekOrigin.Current:
            {
                seekFlag = SeekFlags.SEEK_CUR;
                break;
            }

            case SeekOrigin.End:
            {
                seekFlag = SeekFlags.SEEK_END;
                break;
            }
            }
            long num = Syscall.lseek(this.fileDescriptor, offset, seekFlag);

            if (num == (long)-1)
            {
                UnixMarshal.ThrowExceptionForLastError();
            }
            return(num);
        }
예제 #28
0
        public static String GetPluralString(String s, String p, Int32 n)
        {
            IntPtr ints, intp, _ignore;

            MarshalStrings(s, out ints, p, out intp, null, out _ignore);

            try {
                // ngettext(3) returns an input pointer if no translation is found
                IntPtr r = ngettext(ints, intp, n);
                if (r == ints)
                {
                    return(s);
                }
                if (r == intp)
                {
                    return(p);
                }
                return(UnixMarshal.PtrToStringUnix(r));
            }
            finally {
                UnixMarshal.FreeHeap(ints);
                UnixMarshal.FreeHeap(intp);
            }
        }
예제 #29
0
파일: UnixStream.cs 프로젝트: zzwwqqq/mono
        public override void Close()
        {
            if (fileDescriptor == InvalidFileDescriptor)
            {
                return;
            }

            Flush();

            if (!owner)
            {
                return;
            }

            int r;

            do
            {
                r = Native.Syscall.close(fileDescriptor);
            } while (UnixMarshal.ShouldRetrySyscall(r));
            UnixMarshal.ThrowExceptionForLastErrorIf(r);
            fileDescriptor = InvalidFileDescriptor;
            GC.SuppressFinalize(this);
        }
예제 #30
0
        public static string PtrToString(IntPtr p, Encoding encoding)
        {
            if (p == IntPtr.Zero)
            {
                return(null);
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            int    stringByteLength = UnixMarshal.GetStringByteLength(p, encoding);
            string str = new string((void *)p, 0, stringByteLength, encoding);

            stringByteLength = str.Length;
            while (stringByteLength > 0 && str[stringByteLength - 1] == 0)
            {
                stringByteLength--;
            }
            if (stringByteLength == str.Length)
            {
                return(str);
            }
            return(str.Substring(0, stringByteLength));
        }