Class Unix


  • public final class Unix
    extends java.lang.Object
    A utility class that provides access to common Unix system calls. Obviously, this will only work on Unix platforms and it requires a native library to be loaded.

    Check with isOperational() if this class is operational and only call the other methods if Unix.isOperational() == true.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Unix.Group
      A class representing the Unix group struct.
      static class  Unix.Password
      A class representing the Unix passwd struct.
      static class  Unix.Stat
      A class representing the Unix stat structure.
      static class  Unix.Time
      A class to represent a Unix struct timespec that holds a system time in nano-second resolution.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static short S_IRGRP
      read by group
      static short S_IROTH
      read by others
      static short S_IRUSR
      read by owner
      static short S_ISGID
      set group ID on execution
      static short S_ISUID
      set user ID on execution
      static short S_ISVTX
      sticky bit
      static short S_IWGRP
      write by group
      static short S_IWOTH
      write by others
      static short S_IWUSR
      write by owner
      static short S_IXGRP
      execute/search by group
      static short S_IXOTH
      execute/search by others
      static short S_IXUSR
      execute/search by owner
    • Constructor Summary

      Constructors 
      Constructor Description
      Unix()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static boolean canDetectProcesses()
      Returns true, if process detection is available on this system.
      static void createHardLink​(java.lang.String fileName, java.lang.String linkName)
      Creates a hard link linkName that points to fileName.
      static void createSymbolicLink​(java.lang.String fileName, java.lang.String linkName)
      Creates a symbolic link linkName that points to fileName.
      static int getEgid()
      Returns the effective gid that determines the permissions of this process.
      static java.lang.String getErrorString​(int errnum)
      Returns the error string for the given errnum.
      static int getEuid()
      Returns the effective uid that determines the permissions of this process.
      static Unix.Stat getFileInfo​(java.lang.String fileName)
      Returns the information about fileName.
      static int getGid()
      Returns the gid of the user that started this process.
      static int getGidForGroupName​(java.lang.String groupName)
      Returns the gid of the groupName, or -1, if no group with this name exists.
      static long getInode​(java.lang.String linkName)
      Deprecated.
      Use method getInode(String) from getLinkInfo(String) instead.
      static java.lang.String getLastError()
      Returns the last error that occurred in this class.
      static Unix.Stat getLinkInfo​(java.lang.String linkName)
      Returns the information about linkName.
      static Unix.Stat getLinkInfo​(java.lang.String linkName, boolean readSymbolicLinkTarget)
      Returns the information about linkName.
      static int getNumberOfHardLinks​(java.lang.String linkName)
      Deprecated.
      static int getPid()
      Returns the process identifier of the current process.
      static Unix.Time getSystemTime()
      Gets the current system time.
      static long getSystemTimeMillis()
      Gets the current system time.
      static int getUid()
      Returns the uid of the user that started this process.
      static int getUidForUserName​(java.lang.String userName)
      Returns the uid of the userName, or -1, if no user with this name exists.
      static boolean isOperational()
      Returns true, if the native library has been loaded successfully and the link utilities are operational, false otherwise.
      static boolean isProcessRunning​(int pid)
      Returns true, if the process with pid is currently running and false, if it is not running or if process detection is not available ( canDetectProcesses() == false).
      static boolean isSymbolicLink​(java.lang.String linkName)
      Returns true if linkName is a symbolic link and false otherwise.
      static boolean isUseUnixRealtimeTimer()  
      static void setAccessMode​(java.lang.String fileName, short mode)
      Sets the access mode of filename to the specified mode value.
      static void setFileTimestamps​(java.lang.String fileName)
      Change file timestamps of a file, directory or link to the current time.
      static void setFileTimestamps​(java.lang.String fileName, long accessTimeSecs, long modificationTimeSecs)
      Change file timestamps of a file, directory or link.
      static void setFileTimestamps​(java.lang.String fileName, long accessTimeSecs, long accessTimeMicroSecs, long modificationTimeSecs, long modificationTimeMicroSecs)
      Change file timestamps of a file, directory or link.
      static void setFileTimestamps​(java.lang.String fileName, Unix.Time accessTime, Unix.Time modificationTime)
      Change file timestamps of a file, directory or link.
      static void setLinkOwner​(java.lang.String linkName, int uid, int gid)
      Sets the owner of linkName to the specified uid and gid values.
      static void setLinkOwner​(java.lang.String linkName, Unix.Password user)
      Sets the owner of linkName to the uid and gid of the specified user.
      static void setLinkTimestamps​(java.lang.String fileName)
      Change file timestamps of a file, directory or link to the current time.
      static void setLinkTimestamps​(java.lang.String fileName, long accessTimeSecs, long modificationTimeSecs)
      Change file timestamps of a file, directory or link.
      static void setLinkTimestamps​(java.lang.String fileName, long accessTimeSecs, long accessTimeMicroSecs, long modificationTimeSecs, long modificationTimeMicroSecs)
      Change link timestamps of a file, directory or link.
      static void setLinkTimestamps​(java.lang.String fileName, Unix.Time accessTime, Unix.Time modificationTime)
      Change file timestamps of a file, directory or link.
      static void setOwner​(java.lang.String fileName, int uid, int gid)
      Sets the owner of fileName to the specified uid and gid values.
      static void setOwner​(java.lang.String fileName, Unix.Password user)
      Sets the owner of fileName to the uid and gid of the specified user.
      static void setUseUnixRealtimeTimer​(boolean useUnixRealTimeTimer)
      Sets whether to use the Unix realttime timer.
      static Unix.Stat tryGetFileInfo​(java.lang.String fileName)
      Returns the information about fileName, or NullPointerException, if the information could not be obtained, e.g.
      static Unix.Group tryGetGroupByGid​(int gid)
      Returns the Unix.Group for the given gid, or null, if no group with that gid exists.
      static Unix.Group tryGetGroupByName​(java.lang.String groupName)
      Returns the Unix.Group for the given groupName, or null, if no group with that name exists.
      static java.lang.String tryGetGroupNameForGid​(int gid)
      Returns the name of the group identified by gid, or null, if no group with that gid exists.
      static Unix.Stat tryGetLinkInfo​(java.lang.String linkName)
      Returns the information about linkName, or NullPointerException, if the information could not be obtained, e.g.
      static Unix.Stat tryGetLinkInfo​(java.lang.String linkName, boolean readSymbolicLinkTarget)
      Returns the information about linkName, or null if the information can not be obtained, e.g.
      static Unix.Password tryGetUserByName​(java.lang.String userName)
      Returns the Unix.Password for the given userName, or null, if no user with that name exists.
      static Unix.Password tryGetUserByUid​(int uid)
      Returns the Unix.Password for the given userName, or null, if no user with that name exists.
      static java.lang.String tryGetUserNameForUid​(int uid)
      Returns the name of the user identified by uid.
      static java.lang.String tryReadSymbolicLink​(java.lang.String linkName)
      Returns the value of the symbolik link linkName, or null, if linkName is not a symbolic link.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Unix

        public Unix()
    • Method Detail

      • isUseUnixRealtimeTimer

        public static boolean isUseUnixRealtimeTimer()
      • setUseUnixRealtimeTimer

        public static void setUseUnixRealtimeTimer​(boolean useUnixRealTimeTimer)
        Sets whether to use the Unix realttime timer.

        Note that old versions of Linux and MacOSX do not yet support this and will terminate the Java program when this flag is set to true and getSystemTime() is called!

        Parameters:
        useUnixRealTimeTimer - if true, the realtime timer (nano-second resolution) will be used, otherwise the regular timer (micro-second resolution) will be used.
      • isOperational

        public static final boolean isOperational()
        Returns true, if the native library has been loaded successfully and the link utilities are operational, false otherwise.
      • canDetectProcesses

        public static boolean canDetectProcesses()
        Returns true, if process detection is available on this system.
      • getLastError

        public static java.lang.String getLastError()
        Returns the last error that occurred in this class. Use this to find out what went wrong after tryGetLinkInfo(String) or tryGetFileInfo(String) returned null.
      • getPid

        public static int getPid()
        Returns the process identifier of the current process.
      • isProcessRunning

        public static boolean isProcessRunning​(int pid)
        Returns true, if the process with pid is currently running and false, if it is not running or if process detection is not available ( canDetectProcesses() == false).
      • getUid

        public static final int getUid()
        Returns the uid of the user that started this process.
      • getEuid

        public static final int getEuid()
        Returns the effective uid that determines the permissions of this process.
      • getGid

        public static final int getGid()
        Returns the gid of the user that started this process.
      • getEgid

        public static final int getEgid()
        Returns the effective gid that determines the permissions of this process.
      • getSystemTime

        public static final Unix.Time getSystemTime()
        Gets the current system time.
        Returns:
        the system time as seconds since the epoch and, in addition, nano-seconds since the current second.
      • getSystemTimeMillis

        public static final long getSystemTimeMillis()
        Gets the current system time.
        Returns:
        the system time as milli-seconds since the epoch.
      • createHardLink

        public static final void createHardLink​(java.lang.String fileName,
                                                java.lang.String linkName)
                                         throws IOExceptionUnchecked
        Creates a hard link linkName that points to fileName.
        Throws:
        IOExceptionUnchecked - If the underlying system call fails, e.g. because linkName already exists or fileName does not exist.
      • createSymbolicLink

        public static final void createSymbolicLink​(java.lang.String fileName,
                                                    java.lang.String linkName)
                                             throws IOExceptionUnchecked
        Creates a symbolic link linkName that points to fileName.
        Throws:
        IOExceptionUnchecked - If the underlying system call fails, e.g. because linkName already exists.
      • getInode

        @Deprecated
        public static final long getInode​(java.lang.String linkName)
                                   throws IOExceptionUnchecked
        Deprecated.
        Use method getInode(String) from getLinkInfo(String) instead.
        Returns the inode for the fileName. Does not dereference a symbolic link.
        Throws:
        IOExceptionUnchecked - If the information could not be obtained, e.g. because the link does not exist.
      • isSymbolicLink

        public static final boolean isSymbolicLink​(java.lang.String linkName)
                                            throws IOExceptionUnchecked
        Returns true if linkName is a symbolic link and false otherwise.
        Throws:
        IOExceptionUnchecked - If the information could not be obtained, e.g. because the link does not exist.
      • tryReadSymbolicLink

        public static final java.lang.String tryReadSymbolicLink​(java.lang.String linkName)
                                                          throws IOExceptionUnchecked
        Returns the value of the symbolik link linkName, or null, if linkName is not a symbolic link.
        Throws:
        IOExceptionUnchecked - If the information could not be obtained, e.g. because the link does not exist.
      • getFileInfo

        public static final Unix.Stat getFileInfo​(java.lang.String fileName)
                                           throws IOExceptionUnchecked
        Returns the information about fileName.
        Throws:
        IOExceptionUnchecked - If the information could not be obtained, e.g. because the file does not exist.
      • tryGetFileInfo

        public static final Unix.Stat tryGetFileInfo​(java.lang.String fileName)
                                              throws IOExceptionUnchecked
        Returns the information about fileName, or NullPointerException, if the information could not be obtained, e.g. because the file does not exist (call getLastError() to find out what went wrong).
        Throws:
        IOExceptionUnchecked
      • getLinkInfo

        public static final Unix.Stat getLinkInfo​(java.lang.String linkName)
                                           throws IOExceptionUnchecked
        Returns the information about linkName.
        Throws:
        IOExceptionUnchecked - If the information could not be obtained, e.g. because the link does not exist.
      • getLinkInfo

        public static final Unix.Stat getLinkInfo​(java.lang.String linkName,
                                                  boolean readSymbolicLinkTarget)
                                           throws IOExceptionUnchecked
        Returns the information about linkName. If readSymbolicLinkTarget == true, then the symbolic link target is read when linkName is a symbolic link.
        Throws:
        IOExceptionUnchecked - If the information could not be obtained, e.g. because the link does not exist.
      • tryGetLinkInfo

        public static final Unix.Stat tryGetLinkInfo​(java.lang.String linkName)
                                              throws IOExceptionUnchecked
        Returns the information about linkName, or NullPointerException, if the information could not be obtained, e.g. because the link does not exist (call getLastError() to find out what went wrong).
        Throws:
        IOExceptionUnchecked
      • tryGetLinkInfo

        public static final Unix.Stat tryGetLinkInfo​(java.lang.String linkName,
                                                     boolean readSymbolicLinkTarget)
                                              throws IOExceptionUnchecked
        Returns the information about linkName, or null if the information can not be obtained, e.g. because the link does not exist (call getLastError() to find out what went wrong). If readSymbolicLinkTarget == true, then the symbolic link target is read when linkName is a symbolic link.
        Throws:
        IOExceptionUnchecked
      • setAccessMode

        public static final void setAccessMode​(java.lang.String fileName,
                                               short mode)
                                        throws IOExceptionUnchecked
        Sets the access mode of filename to the specified mode value. Dereferences a symbolic link.
        Throws:
        IOExceptionUnchecked
      • setOwner

        public static final void setOwner​(java.lang.String fileName,
                                          int uid,
                                          int gid)
                                   throws IOExceptionUnchecked
        Sets the owner of fileName to the specified uid and gid values. Dereferences a symbolic link.
        Throws:
        IOExceptionUnchecked
      • setLinkOwner

        public static final void setLinkOwner​(java.lang.String linkName,
                                              int uid,
                                              int gid)
                                       throws IOExceptionUnchecked
        Sets the owner of linkName to the specified uid and gid values. Does not dereference a symbolic link.
        Throws:
        IOExceptionUnchecked
      • setLinkOwner

        public static final void setLinkOwner​(java.lang.String linkName,
                                              Unix.Password user)
                                       throws IOExceptionUnchecked
        Sets the owner of linkName to the uid and gid of the specified user. Does not dereference a symbolic link.
        Throws:
        IOExceptionUnchecked
      • setLinkTimestamps

        public static void setLinkTimestamps​(java.lang.String fileName,
                                             long accessTimeSecs,
                                             long accessTimeMicroSecs,
                                             long modificationTimeSecs,
                                             long modificationTimeMicroSecs)
                                      throws IOExceptionUnchecked
        Change link timestamps of a file, directory or link. Does not dereference a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        accessTimeSecs - The new access time in seconds since start of the epoch.
        accessTimeMicroSecs - The micro-second part of the new access time.
        modificationTimeSecs - The new modification time in seconds since start of the epoch.
        modificationTimeMicroSecs - The micro-second part of the new modification time.
        Throws:
        IOExceptionUnchecked
      • setLinkTimestamps

        public static void setLinkTimestamps​(java.lang.String fileName,
                                             long accessTimeSecs,
                                             long modificationTimeSecs)
                                      throws IOExceptionUnchecked
        Change file timestamps of a file, directory or link. Does not dereference a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        accessTimeSecs - The new access time in seconds since start of the epoch.
        modificationTimeSecs - The new modification time in seconds since start of the epoch.
        Throws:
        IOExceptionUnchecked
      • setLinkTimestamps

        public static void setLinkTimestamps​(java.lang.String fileName,
                                             Unix.Time accessTime,
                                             Unix.Time modificationTime)
                                      throws IOExceptionUnchecked
        Change file timestamps of a file, directory or link. Does not dereference a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        accessTime - The new access time as Unix.Time object.
        modificationTime - The new modification time as Unix.Time object.
        Throws:
        IOExceptionUnchecked
      • setLinkTimestamps

        public static void setLinkTimestamps​(java.lang.String fileName)
                                      throws IOExceptionUnchecked
        Change file timestamps of a file, directory or link to the current time. Does not dereference a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        Throws:
        IOExceptionUnchecked
      • setFileTimestamps

        public static void setFileTimestamps​(java.lang.String fileName,
                                             long accessTimeSecs,
                                             long accessTimeMicroSecs,
                                             long modificationTimeSecs,
                                             long modificationTimeMicroSecs)
                                      throws IOExceptionUnchecked
        Change file timestamps of a file, directory or link. Dereferences a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        accessTimeSecs - The new access time in seconds since start of the epoch.
        accessTimeMicroSecs - The micro-second part of the new access time.
        modificationTimeSecs - The new modification time in seconds since start of the epoch.
        modificationTimeMicroSecs - The micro-second part of the new modification time.
        Throws:
        IOExceptionUnchecked
      • setFileTimestamps

        public static void setFileTimestamps​(java.lang.String fileName,
                                             long accessTimeSecs,
                                             long modificationTimeSecs)
                                      throws IOExceptionUnchecked
        Change file timestamps of a file, directory or link. Does not dereference a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        accessTimeSecs - The new access time in seconds since start of the epoch.
        modificationTimeSecs - The new modification time in seconds since start of the epoch.
        Throws:
        IOExceptionUnchecked
      • setFileTimestamps

        public static void setFileTimestamps​(java.lang.String fileName,
                                             Unix.Time accessTime,
                                             Unix.Time modificationTime)
                                      throws IOExceptionUnchecked
        Change file timestamps of a file, directory or link. Does not dereference a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        accessTime - The new access time as Unix.Time object.
        modificationTime - The new modification time as Unix.Time object.
        Throws:
        IOExceptionUnchecked
      • setFileTimestamps

        public static void setFileTimestamps​(java.lang.String fileName)
                                      throws IOExceptionUnchecked
        Change file timestamps of a file, directory or link to the current time. Does not dereference a symbolic link.
        Parameters:
        fileName - The name of the file or link to change the timestamp of.
        Throws:
        IOExceptionUnchecked
      • tryGetUserNameForUid

        public static final java.lang.String tryGetUserNameForUid​(int uid)
        Returns the name of the user identified by uid.
      • getUidForUserName

        public static final int getUidForUserName​(java.lang.String userName)
        Returns the uid of the userName, or -1, if no user with this name exists.
      • tryGetUserByName

        public static final Unix.Password tryGetUserByName​(java.lang.String userName)
        Returns the Unix.Password for the given userName, or null, if no user with that name exists.
      • tryGetUserByUid

        public static final Unix.Password tryGetUserByUid​(int uid)
        Returns the Unix.Password for the given userName, or null, if no user with that name exists.
      • tryGetGroupNameForGid

        public static final java.lang.String tryGetGroupNameForGid​(int gid)
        Returns the name of the group identified by gid, or null, if no group with that gid exists.
      • getGidForGroupName

        public static final int getGidForGroupName​(java.lang.String groupName)
        Returns the gid of the groupName, or -1, if no group with this name exists.
      • tryGetGroupByName

        public static final Unix.Group tryGetGroupByName​(java.lang.String groupName)
        Returns the Unix.Group for the given groupName, or null, if no group with that name exists.
      • tryGetGroupByGid

        public static final Unix.Group tryGetGroupByGid​(int gid)
        Returns the Unix.Group for the given gid, or null, if no group with that gid exists.
      • getErrorString

        public static final java.lang.String getErrorString​(int errnum)
        Returns the error string for the given errnum.