public final class Unix
extends java.lang.Object
Check with isOperational()
if this class is operational and only call the other methods if
Unix.isOperational() == true
.
Modifier and Type | Class and 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. |
Modifier and Type | Field and 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 and Description |
---|
Unix() |
Modifier and Type | Method and 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.
Use method
getNumberOfHardLinks(String) from getLinkInfo(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()
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. |
public static final short S_ISUID
public static final short S_ISGID
public static final short S_ISVTX
public static final short S_IRUSR
public static final short S_IWUSR
public static final short S_IXUSR
public static final short S_IRGRP
public static final short S_IWGRP
public static final short S_IXGRP
public static final short S_IROTH
public static final short S_IWOTH
public static final short S_IXOTH
public static boolean isUseUnixRealtimeTimer()
public static void setUseUnixRealtimeTimer(boolean useUnixRealTimeTimer)
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!
useUnixRealTimeTimer
- if true
, the realtime timer (nano-second resolution) will be used,
otherwise the regular timer (micro-second resolution) will be used.public static final boolean isOperational()
true
, if the native library has been loaded successfully and the link utilities are operational, false
otherwise.public static boolean canDetectProcesses()
true
, if process detection is available on this system.public static java.lang.String getLastError()
tryGetLinkInfo(String)
or
tryGetFileInfo(String)
returned null
.public static int getPid()
public static boolean isProcessRunning(int pid)
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
).public static final int getUid()
public static final int getEuid()
public static final int getGid()
public static final int getEgid()
public static final Unix.Time getSystemTime()
public static final long getSystemTimeMillis()
public static final void createHardLink(java.lang.String fileName, java.lang.String linkName) throws IOExceptionUnchecked
IOExceptionUnchecked
- If the underlying system call fails, e.g. because linkName already exists or fileName does
not exist.public static final void createSymbolicLink(java.lang.String fileName, java.lang.String linkName) throws IOExceptionUnchecked
IOExceptionUnchecked
- If the underlying system call fails, e.g. because linkName already exists.@Deprecated public static final long getInode(java.lang.String linkName) throws IOExceptionUnchecked
getInode(String)
from getLinkInfo(String)
instead.IOExceptionUnchecked
- If the information could not be obtained, e.g. because the link does not exist.@Deprecated public static final int getNumberOfHardLinks(java.lang.String linkName) throws IOExceptionUnchecked
getNumberOfHardLinks(String)
from getLinkInfo(String)
instead.IOExceptionUnchecked
- If the information could not be obtained, e.g. because the link does not exist.public static final boolean isSymbolicLink(java.lang.String linkName) throws IOExceptionUnchecked
true
if linkName is a symbolic link and false
otherwise.IOExceptionUnchecked
- If the information could not be obtained, e.g. because the link does not exist.public static final java.lang.String tryReadSymbolicLink(java.lang.String linkName) throws IOExceptionUnchecked
null
, if linkName is not a symbolic link.IOExceptionUnchecked
- If the information could not be obtained, e.g. because the link does not exist.public static final Unix.Stat getFileInfo(java.lang.String fileName) throws IOExceptionUnchecked
IOExceptionUnchecked
- If the information could not be obtained, e.g. because the file does not exist.public static final Unix.Stat tryGetFileInfo(java.lang.String fileName) throws IOExceptionUnchecked
NullPointerException
, if the information could not be obtained, e.g. because the
file does not exist (call getLastError()
to find out what went wrong).IOExceptionUnchecked
public static final Unix.Stat getLinkInfo(java.lang.String linkName) throws IOExceptionUnchecked
IOExceptionUnchecked
- If the information could not be obtained, e.g. because the link does not exist.public static final Unix.Stat getLinkInfo(java.lang.String linkName, boolean readSymbolicLinkTarget) throws IOExceptionUnchecked
readSymbolicLinkTarget == true
, then the symbolic link target is read when
linkName is a symbolic link.IOExceptionUnchecked
- If the information could not be obtained, e.g. because the link does not exist.public static final Unix.Stat tryGetLinkInfo(java.lang.String linkName) throws IOExceptionUnchecked
NullPointerException
, if the information could not be obtained, e.g. because the
link does not exist (call getLastError()
to find out what went wrong).IOExceptionUnchecked
public static final Unix.Stat tryGetLinkInfo(java.lang.String linkName, boolean readSymbolicLinkTarget) throws IOExceptionUnchecked
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.IOExceptionUnchecked
public static final void setAccessMode(java.lang.String fileName, short mode) throws IOExceptionUnchecked
IOExceptionUnchecked
public static final void setOwner(java.lang.String fileName, int uid, int gid) throws IOExceptionUnchecked
IOExceptionUnchecked
public static final void setOwner(java.lang.String fileName, Unix.Password user) throws IOExceptionUnchecked
user
.
Dereferences a symbolic link.IOExceptionUnchecked
public static final void setLinkOwner(java.lang.String linkName, int uid, int gid) throws IOExceptionUnchecked
IOExceptionUnchecked
public static final void setLinkOwner(java.lang.String linkName, Unix.Password user) throws IOExceptionUnchecked
user
.
Does not dereference a symbolic link.IOExceptionUnchecked
public static void setLinkTimestamps(java.lang.String fileName, long accessTimeSecs, long accessTimeMicroSecs, long modificationTimeSecs, long modificationTimeMicroSecs) throws IOExceptionUnchecked
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.IOExceptionUnchecked
public static void setLinkTimestamps(java.lang.String fileName, long accessTimeSecs, long modificationTimeSecs) throws IOExceptionUnchecked
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.IOExceptionUnchecked
public static void setLinkTimestamps(java.lang.String fileName, Unix.Time accessTime, Unix.Time modificationTime) throws IOExceptionUnchecked
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.IOExceptionUnchecked
public static void setLinkTimestamps(java.lang.String fileName) throws IOExceptionUnchecked
fileName
- The name of the file or link to change the timestamp of.IOExceptionUnchecked
public static void setFileTimestamps(java.lang.String fileName, long accessTimeSecs, long accessTimeMicroSecs, long modificationTimeSecs, long modificationTimeMicroSecs) throws IOExceptionUnchecked
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.IOExceptionUnchecked
public static void setFileTimestamps(java.lang.String fileName, long accessTimeSecs, long modificationTimeSecs) throws IOExceptionUnchecked
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.IOExceptionUnchecked
public static void setFileTimestamps(java.lang.String fileName, Unix.Time accessTime, Unix.Time modificationTime) throws IOExceptionUnchecked
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.IOExceptionUnchecked
public static void setFileTimestamps(java.lang.String fileName) throws IOExceptionUnchecked
fileName
- The name of the file or link to change the timestamp of.IOExceptionUnchecked
public static final java.lang.String tryGetUserNameForUid(int uid)
public static final int getUidForUserName(java.lang.String userName)
-1
, if no user with this name exists.public static final Unix.Password tryGetUserByName(java.lang.String userName)
Unix.Password
for the given userName, or null
, if no user with that name exists.public static final Unix.Password tryGetUserByUid(int uid)
Unix.Password
for the given userName, or null
, if no user with that name exists.public static final java.lang.String tryGetGroupNameForGid(int gid)
null
, if no group with that gid exists.public static final int getGidForGroupName(java.lang.String groupName)
-1
, if no group with this name exists.public static final Unix.Group tryGetGroupByName(java.lang.String groupName)
Unix.Group
for the given groupName, or null
, if no group with that name exists.public static final Unix.Group tryGetGroupByGid(int gid)
Unix.Group
for the given gid, or null
, if no group with that gid exists.public static final java.lang.String getErrorString(int errnum)