コード例 #1
0
        public static bool MakeExecutable(string filePath, bool throwOnError = true)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException("must not be null or empty", nameof(filePath));
            }

            if (!File.Exists(filePath))
            {
                Log.WarningLine($"Script {filePath} does not exist");
                return(true);
            }

            int ret = Syscall.chmod(filePath,
                                    FilePermissions.S_IRGRP | FilePermissions.S_IXGRP |
                                    FilePermissions.S_IROTH | FilePermissions.S_IXOTH |
                                    FilePermissions.S_IRUSR | FilePermissions.S_IXUSR | FilePermissions.S_IWUSR
                                    );

            if (ret == 0)
            {
                return(true);
            }

            string message = $"Failed to make {filePath} executable: {Stdlib.strerror (Stdlib.GetLastError ())}";

            if (throwOnError)
            {
                throw new InvalidOperationException(message);
            }
            Log.WarningLine(message);
            return(false);
        }
コード例 #2
0
        partial void MakeExecutable(string scriptPath)
        {
            if (String.IsNullOrEmpty(scriptPath))
            {
                throw new ArgumentException("must not be null or empty", nameof(scriptPath));
            }

            if (!File.Exists(scriptPath))
            {
                Log.WarningLine($"Script {scriptPath} does not exist");
                return;
            }

            int ret = Syscall.chmod(scriptPath,
                                    FilePermissions.S_IRGRP | FilePermissions.S_IXGRP |
                                    FilePermissions.S_IROTH | FilePermissions.S_IXOTH |
                                    FilePermissions.S_IRUSR | FilePermissions.S_IXUSR | FilePermissions.S_IWUSR
                                    );

            if (ret == 0)
            {
                return;
            }

            Log.ErrorLine($"Failed to make {scriptPath} executable: {Stdlib.strerror (Stdlib.GetLastError ())}");
            throw new InvalidOperationException("Failed");
        }
コード例 #3
0
        protected static bool IsExecutable(string fullPath, bool throwOnErrors = false)
        {
            Stat sbuf;
            int  ret = Syscall.stat(fullPath, out sbuf);

            if (ret < 0)
            {
                if (throwOnErrors)
                {
                    throw new InvalidOperationException($"Failed to stat file '{fullPath}': {Stdlib.strerror (Stdlib.GetLastError ())}");
                }
                return(false);
            }

            if ((sbuf.st_mode & ExecutableBits) == 0)
            {
                if (throwOnErrors)
                {
                    throw new InvalidOperationException($"File '{fullPath}' is not executable");
                }
                return(false);
            }

            return(true);
        }
コード例 #4
0
            public override unsafe int Read(Span <byte> data)
            {
                if (data.IsEmpty)
                {
                    return(0);
                }

                long ret;

                lock (_lock)
                {
                    while (true)
                    {
                        fixed(byte *p = data)
                        {
                            while ((ret = Syscall.read(Handle, p, (ulong)data.Length)) == -1 &&
                                   Stdlib.GetLastError() == Errno.EINTR)
                            {
                                // Retry in case we get interrupted by a signal.
                            }

                            if (ret != -1)
                            {
                                break;
                            }

                            var err = Stdlib.GetLastError();

                            // The descriptor was probably redirected to a program that ended. Just
                            // silently ignore this situation.
                            //
                            // The strange condition where errno is zero happens e.g. on Linux if
                            // the process is killed while blocking in the read system call.
                            if (err == 0 || err == Errno.EPIPE)
                            {
                                ret = 0;

                                break;
                            }

                            // The file descriptor has been configured as non-blocking. Instead of
                            // busily trying to read over and over, poll until we can write and then
                            // try again.
                            if (err == Errno.EAGAIN)
                            {
                                _ = Syscall.poll(new[]
                                {
                                    new Pollfd
                                    {
                                        fd     = Handle,
                                        events = PollEvents.POLLIN,
                                    },
                                }, 1, Timeout.Infinite);

                                continue;
                            }

                            if (err == 0)
                            {
                                err = Errno.EBADF;
                            }

                            throw new TerminalException(
                                      $"Could not read from standard {_name}: {Stdlib.strerror(err)}");
                        }
                    }
                }

                return((int)ret);
            }
コード例 #5
0
        protected BaseSharedMemory(bool allowCreate, Type type, bool oneSide)
        {
            HalftoneBuffer = new IntPtr[2];
            BinBuffer      = new IntPtr[2];
            ShmType        = type;
            BufferSize     = Marshal.SizeOf(ShmType);
            shmid          = shmget(SHMKEY, 0, 0);
            Errno errno = Stdlib.GetLastError();

            if (shmid == -1)
            {
                if (allowCreate)
                {
                    shmid = shmget(SHMKEY, BufferSize, SHM_PERMISSION | IPC_CREAT | IPC_EXCL);
                    errno = Stdlib.GetLastError();
                    if (shmid == -1)
                    {
                        throw new Exception(string.Format(NoSharedMemory, BufferSize, "shmget-new", errno, Stdlib.strerror(errno)));
                    }
                }
                else
                {
                    throw new Exception(string.Format(NoSharedMemory, BufferSize, "shmget", errno, Stdlib.strerror(errno)));
                }
            }
            ptr   = shmat(shmid, 0, 0);
            errno = Stdlib.GetLastError();
            if (ptr == -1)
            {
                throw new Exception(string.Format(NoSharedMemory, BufferSize, "shmat", errno, Stdlib.strerror(errno)));
            }
            BinBuffer[0]      = GetDataPointer("oneSideBin");
            HalftoneBuffer[0] = GetDataPointer("oneSide");
            if (oneSide)
            {
                BinBuffer[1]      = BinBuffer[0];
                HalftoneBuffer[1] = HalftoneBuffer[0];
            }
            else
            {
                BinBuffer[1]      = GetDataPointer("twoSideBin");
                HalftoneBuffer[1] = GetDataPointer("twoSide");
            }
        }
コード例 #6
0
        /// <summary>
        ///   Moves file from <paramref name="sourcePath"/> to <paramref name="destinationPath"/> the same way <see
        ///   cref="System.IO.File.Move"/> does but it checks whether the file pointed to by <paramref
        ///   name="sourcePath"/> is a dangling symlink and, if yes, it does not call <see cref="System.IO.File.Move"/>
        ///   but instead uses <c>readlink(2)</c> and <c>symlink(2)</c> to recreate the symlink at the destination. This
        ///   is to work around a bug in Mono 6 series which will throw an exception when trying to move a dangling
        ///   symlink.
        /// </summary>
        public static void FileMove(string sourcePath, string destinationPath)
        {
            if (String.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentException("must not be null or empty", nameof(sourcePath));
            }
            if (String.IsNullOrEmpty(destinationPath))
            {
                throw new ArgumentException("must not be null or empty", nameof(destinationPath));
            }

            int ret = Syscall.lstat(sourcePath, out Stat sbuf);

            if (ret != 0 || (ret == 0 && (sbuf.st_mode & FilePermissions.S_IFLNK) != FilePermissions.S_IFLNK))
            {
                // Not a symlink or an error, just call to the BCL and let it handle both situations
                File.Move(sourcePath, destinationPath);
                return;
            }

            // Source is a symlink
            ret = Syscall.stat(sourcePath, out sbuf);
            if (ret < 0)
            {
                Log.DebugLine($"stat on {sourcePath} returned {ret}. Errno: {Stdlib.GetLastError ()}");
            }
            if (!FileIsDanglingSymlink(sourcePath))
            {
                // let BCL handle it
                File.Move(sourcePath, destinationPath);
                return;
            }

            Log.DebugLine($"Moving a dangling symlink from {sourcePath} to {destinationPath}");
            // We have a dangling symlink, we'll just recreate it at the destination and remove the source
            var sb = new StringBuilder(checked ((int)sbuf.st_size));

            ret = Syscall.readlink(sourcePath, sb);
            if (ret < 0)
            {
                throw new IOException($"Failed to read a symbolic link '{sourcePath}'. {Stdlib.strerror (Stdlib.GetLastError ())}");
            }

            string sourceLinkContents = sb.ToString();

            Log.DebugLine($"Source symlink {sourcePath} points to: {sourceLinkContents}");
            ret = Syscall.symlink(sourceLinkContents, destinationPath);
            if (ret < 0)
            {
                throw new IOException($"Failed to create a symbolic link '{destinationPath}' -> '{sourceLinkContents}'. {Stdlib.strerror (Stdlib.GetLastError ())}");
            }
        }
コード例 #7
0
 public FsException(Errno errnum, Exception?innerException = null) :
     base(Stdlib.strerror(errnum), innerException)
 {
 }
コード例 #8
0
 public FsException(string message, string path, Errno errnum, Exception?innerException = null) :
     base($"{message}. {path}: {Stdlib.strerror(errnum)}", innerException)
 {
 }
コード例 #9
0
 private static string strerror(Errno errno)
 {
     return(Stdlib.strerror(errno));
 }