Package ch.systemsx.cisd.base.unix
Class Unix
- java.lang.Object
-
- ch.systemsx.cisd.base.unix.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 ifUnix.isOperational() == true
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Unix.Group
A class representing the Unixgroup
struct.static class
Unix.Password
A class representing the Unixpasswd
struct.static class
Unix.Stat
A class representing the Unixstat
structure.static class
Unix.Time
A class to represent a Unixstruct timespec
that holds a system time in nano-second resolution.
-
Field Summary
Fields Modifier and Type Field Description static short
S_IRGRP
read by groupstatic short
S_IROTH
read by othersstatic short
S_IRUSR
read by ownerstatic short
S_ISGID
set group ID on executionstatic short
S_ISUID
set user ID on executionstatic short
S_ISVTX
sticky bitstatic short
S_IWGRP
write by groupstatic short
S_IWOTH
write by othersstatic short
S_IWUSR
write by ownerstatic short
S_IXGRP
execute/search by groupstatic short
S_IXOTH
execute/search by othersstatic 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()
Returnstrue
, 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 methodgetInode(String)
fromgetLinkInfo(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.Use methodgetNumberOfHardLinks(String)
fromgetLinkInfo(String)
instead.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()
Returnstrue
, if the native library has been loaded successfully and the link utilities are operational,false
otherwise.static boolean
isProcessRunning(int pid)
Returnstrue
, if the process with pid is currently running andfalse
, if it is not running or if process detection is not available (canDetectProcesses()
== false
).static boolean
isSymbolicLink(java.lang.String linkName)
Returnstrue
if linkName is a symbolic link andfalse
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 specifieduser
.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 specifieduser
.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, orNullPointerException
, if the information could not be obtained, e.g.static Unix.Group
tryGetGroupByGid(int gid)
Returns theUnix.Group
for the given gid, ornull
, if no group with that gid exists.static Unix.Group
tryGetGroupByName(java.lang.String groupName)
Returns theUnix.Group
for the given groupName, ornull
, if no group with that name exists.static java.lang.String
tryGetGroupNameForGid(int gid)
Returns the name of the group identified by gid, ornull
, if no group with that gid exists.static Unix.Stat
tryGetLinkInfo(java.lang.String linkName)
Returns the information about linkName, orNullPointerException
, if the information could not be obtained, e.g.static Unix.Stat
tryGetLinkInfo(java.lang.String linkName, boolean readSymbolicLinkTarget)
Returns the information about linkName, ornull
if the information can not be obtained, e.g.static Unix.Password
tryGetUserByName(java.lang.String userName)
Returns theUnix.Password
for the given userName, ornull
, if no user with that name exists.static Unix.Password
tryGetUserByUid(int uid)
Returns theUnix.Password
for the given userName, ornull
, 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, ornull
, if linkName is not a symbolic link.
-
-
-
Field Detail
-
S_ISUID
public static final short S_ISUID
set user ID on execution- See Also:
- Constant Field Values
-
S_ISGID
public static final short S_ISGID
set group ID on execution- See Also:
- Constant Field Values
-
S_ISVTX
public static final short S_ISVTX
sticky bit- See Also:
- Constant Field Values
-
S_IRUSR
public static final short S_IRUSR
read by owner- See Also:
- Constant Field Values
-
S_IWUSR
public static final short S_IWUSR
write by owner- See Also:
- Constant Field Values
-
S_IXUSR
public static final short S_IXUSR
execute/search by owner- See Also:
- Constant Field Values
-
S_IRGRP
public static final short S_IRGRP
read by group- See Also:
- Constant Field Values
-
S_IWGRP
public static final short S_IWGRP
write by group- See Also:
- Constant Field Values
-
S_IXGRP
public static final short S_IXGRP
execute/search by group- See Also:
- Constant Field Values
-
S_IROTH
public static final short S_IROTH
read by others- See Also:
- Constant Field Values
-
S_IWOTH
public static final short S_IWOTH
write by others- See Also:
- Constant Field Values
-
S_IXOTH
public static final short S_IXOTH
execute/search by others- See Also:
- Constant Field Values
-
-
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
andgetSystemTime()
is called!- Parameters:
useUnixRealTimeTimer
- iftrue
, 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()
Returnstrue
, if the native library has been loaded successfully and the link utilities are operational,false
otherwise.
-
canDetectProcesses
public static boolean canDetectProcesses()
Returnstrue
, 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 aftertryGetLinkInfo(String)
ortryGetFileInfo(String)
returnednull
.
-
getPid
public static int getPid()
Returns the process identifier of the current process.
-
isProcessRunning
public static boolean isProcessRunning(int pid)
Returnstrue
, if the process with pid is currently running andfalse
, 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 methodgetInode(String)
fromgetLinkInfo(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.
-
getNumberOfHardLinks
@Deprecated public static final int getNumberOfHardLinks(java.lang.String linkName) throws IOExceptionUnchecked
Deprecated.Use methodgetNumberOfHardLinks(String)
fromgetLinkInfo(String)
instead.Returns the number of hard links for the linkName. 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
Returnstrue
if linkName is a symbolic link andfalse
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, ornull
, 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, orNullPointerException
, if the information could not be obtained, e.g. because the file does not exist (callgetLastError()
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. IfreadSymbolicLinkTarget == 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, orNullPointerException
, if the information could not be obtained, e.g. because the link does not exist (callgetLastError()
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, ornull
if the information can not be obtained, e.g. because the link does not exist (callgetLastError()
to find out what went wrong). IfreadSymbolicLinkTarget == 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
-
setOwner
public static final void setOwner(java.lang.String fileName, Unix.Password user) throws IOExceptionUnchecked
Sets the owner of fileName to the uid and gid of the specifieduser
. 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 specifieduser
. 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 asUnix.Time
object.modificationTime
- The new modification time asUnix.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 asUnix.Time
object.modificationTime
- The new modification time asUnix.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 theUnix.Password
for the given userName, ornull
, if no user with that name exists.
-
tryGetUserByUid
public static final Unix.Password tryGetUserByUid(int uid)
Returns theUnix.Password
for the given userName, ornull
, 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, ornull
, 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 theUnix.Group
for the given groupName, ornull
, if no group with that name exists.
-
tryGetGroupByGid
public static final Unix.Group tryGetGroupByGid(int gid)
Returns theUnix.Group
for the given gid, ornull
, 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.
-
-