Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openbis
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sispub
openbis
Commits
33ba9559
Commit
33ba9559
authored
12 years ago
by
brinn
Browse files
Options
Downloads
Patches
Plain Diff
Add methods Unix.tryGetLinkInfo() and Unix.tryGetFileInfo() and Unix.getLastError().
SVN: 25106
parent
3c6e5279
No related branches found
No related tags found
1 merge request
!40
SSDM-13578 : 2PT : Database and V3 Implementation - include the new AFS "free"...
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
source/java/ch/systemsx/cisd/base/unix/Unix.java
+81
-11
81 additions, 11 deletions
source/java/ch/systemsx/cisd/base/unix/Unix.java
sourceTest/java/ch/systemsx/cisd/base/unix/UnixTests.java
+30
-1
30 additions, 1 deletion
sourceTest/java/ch/systemsx/cisd/base/unix/UnixTests.java
with
111 additions
and
12 deletions
source/java/ch/systemsx/cisd/base/unix/Unix.java
+
81
−
11
View file @
33ba9559
...
...
@@ -458,6 +458,16 @@ public final class Unix
return
processDetection
!=
ProcessDetection
.
NONE
;
}
/**
* Returns the last error that occurred in this class. Use this to find out what went wrong
* after {@link #tryGetLinkInfo(String)} or {@link #tryGetFileInfo(String)} returned
* <code>null</code>.
*/
public
static
String
getLastError
()
{
return
strerror
();
}
//
// Process functions
//
...
...
@@ -572,6 +582,15 @@ public final class Unix
}
}
private
static
Stat
tryGetStat
(
String
fileName
)
throws
IOExceptionUnchecked
{
if
(
fileName
==
null
)
{
throw
new
NullPointerException
(
"fileName"
);
}
return
stat
(
fileName
);
}
private
static
Stat
getStat
(
String
fileName
)
throws
IOExceptionUnchecked
{
if
(
fileName
==
null
)
...
...
@@ -586,6 +605,15 @@ public final class Unix
return
result
;
}
private
static
Stat
tryGetLStat
(
String
linkName
)
throws
IOExceptionUnchecked
{
if
(
linkName
==
null
)
{
throw
new
NullPointerException
(
"linkName"
);
}
return
lstat
(
linkName
);
}
private
static
Stat
getLStat
(
String
linkName
)
throws
IOExceptionUnchecked
{
if
(
linkName
==
null
)
...
...
@@ -648,25 +676,35 @@ public final class Unix
}
/**
* Returns the information about <var>
link
Name</var>.
* Returns the information about <var>
file
Name</var>.
*
* @throws IOExceptionUnchecked If the information could not be obtained, e.g. because the
link
* @throws IOExceptionUnchecked If the information could not be obtained, e.g. because the
file
* does not exist.
*/
public
static
final
Stat
get
Link
Info
(
String
link
Name
)
throws
IOExceptionUnchecked
public
static
final
Stat
get
File
Info
(
String
file
Name
)
throws
IOExceptionUnchecked
{
return
get
LinkInfo
(
linkName
,
tru
e
);
return
get
Stat
(
fileNam
e
);
}
/**
* Returns the information about <var>fileName</var>.
* Returns the information about <var>fileName</var>, or {@link NullPointerException}, if the
* information could not be obtained, e.g. because the file does not exist (call
* {@link #getLastError()} to find out what went wrong).
*/
public
static
final
Stat
tryGetFileInfo
(
String
fileName
)
throws
IOExceptionUnchecked
{
return
tryGetStat
(
fileName
);
}
/**
* Returns the information about <var>linkName</var>.
*
* @throws IOExceptionUnchecked If the information could not be obtained, e.g. because the
file
* @throws IOExceptionUnchecked If the information could not be obtained, e.g. because the
link
* does not exist.
*/
public
static
final
Stat
get
File
Info
(
String
file
Name
)
throws
IOExceptionUnchecked
public
static
final
Stat
get
Link
Info
(
String
link
Name
)
throws
IOExceptionUnchecked
{
return
get
Stat
(
fileNam
e
);
return
get
LinkInfo
(
linkName
,
tru
e
);
}
/**
...
...
@@ -682,8 +720,39 @@ public final class Unix
{
final
Stat
stat
=
getLStat
(
linkName
);
final
String
symbolicLinkOrNull
=
(
readSymbolicLinkTarget
&&
stat
.
isSymbolicLink
())
?
readlink
(
linkName
,
(
int
)
stat
.
getSize
())
:
null
;
(
readSymbolicLinkTarget
&&
stat
.
isSymbolicLink
())
?
readlink
(
linkName
,
(
int
)
stat
.
getSize
())
:
null
;
stat
.
setSymbolicLinkOrNull
(
symbolicLinkOrNull
);
return
stat
;
}
/**
* Returns the information about <var>linkName</var>, or {@link NullPointerException}, if the
* information could not be obtained, e.g. because the link does not exist (call
* {@link #getLastError()} to find out what went wrong).
*/
public
static
final
Stat
tryGetLinkInfo
(
String
linkName
)
throws
IOExceptionUnchecked
{
return
tryGetLinkInfo
(
linkName
,
true
);
}
/**
* Returns the information about <var>linkName</var>, or <code>null</code> if the information
* can not be obtained, e.g. because the link does not exist (call {@link #getLastError()} to
* find out what went wrong). If <code>readSymbolicLinkTarget == true</code>, then the symbolic
* link target is read when <var>linkName</var> is a symbolic link.
*/
public
static
final
Stat
tryGetLinkInfo
(
String
linkName
,
boolean
readSymbolicLinkTarget
)
throws
IOExceptionUnchecked
{
final
Stat
stat
=
tryGetLStat
(
linkName
);
if
(
stat
==
null
)
{
return
null
;
}
final
String
symbolicLinkOrNull
=
(
readSymbolicLinkTarget
&&
stat
.
isSymbolicLink
())
?
readlink
(
linkName
,
(
int
)
stat
.
getSize
())
:
null
;
stat
.
setSymbolicLinkOrNull
(
symbolicLinkOrNull
);
return
stat
;
}
...
...
@@ -708,7 +777,8 @@ public final class Unix
* Sets the owner of <var>filename</var> to the specified <var>uid</var> and <var>gid</var>
* values.
*/
public
static
final
void
setOwner
(
String
fileName
,
int
uid
,
int
gid
)
throws
IOExceptionUnchecked
public
static
final
void
setOwner
(
String
fileName
,
int
uid
,
int
gid
)
throws
IOExceptionUnchecked
{
if
(
fileName
==
null
)
{
...
...
This diff is collapsed.
Click to expand it.
sourceTest/java/ch/systemsx/cisd/base/unix/UnixTests.java
+
30
−
1
View file @
33ba9559
...
...
@@ -104,6 +104,35 @@ public class UnixTests extends AbstractFileSystemTestCase
assertNull
(
info2
.
tryGetSymbolicLink
());
}
@Test
(
groups
=
{
"requires_unix"
})
public
void
testGetLinkInfoSymLinkDanglingLink
()
throws
IOException
{
final
File
s
=
new
File
(
workingDirectory
,
"someDanglingLink"
);
Unix
.
createSymbolicLink
(
"link_to_nowhere"
,
s
.
getAbsolutePath
());
final
Stat
info
=
Unix
.
tryGetLinkInfo
(
s
.
getAbsolutePath
());
assertNotNull
(
info
);
assertEquals
(
1
,
info
.
getNumberOfHardLinks
());
assertEquals
(
FileLinkType
.
SYMLINK
,
info
.
getLinkType
());
assertTrue
(
info
.
isSymbolicLink
());
final
Stat
info2
=
Unix
.
tryGetFileInfo
(
s
.
getAbsolutePath
());
assertNull
(
info2
);
assertEquals
(
"No such file or directory"
,
Unix
.
getLastError
());
}
@Test
(
groups
=
{
"requires_unix"
})
public
void
testGetLinkInfoNonExistent
()
throws
IOException
{
final
File
s
=
new
File
(
workingDirectory
,
"nonExistent"
);
final
Stat
info
=
Unix
.
tryGetLinkInfo
(
s
.
getAbsolutePath
());
assertNull
(
info
);
assertEquals
(
"No such file or directory"
,
Unix
.
getLastError
());
final
Stat
info2
=
Unix
.
tryGetFileInfo
(
s
.
getAbsolutePath
());
assertNull
(
info2
);
assertEquals
(
"No such file or directory"
,
Unix
.
getLastError
());
}
@Test
(
groups
=
{
"requires_unix"
},
expectedExceptions
=
NullPointerException
.
class
)
public
void
testCreateSymbolicLinkNull
()
throws
IOException
...
...
@@ -268,7 +297,7 @@ public class UnixTests extends AbstractFileSystemTestCase
assertTrue
(
Unix
.
canDetectProcesses
());
assertTrue
(
Unix
.
isProcessRunningPS
(
Unix
.
getPid
()));
}
public
static
void
main
(
String
[]
args
)
throws
Throwable
{
System
.
out
.
println
(
BuildAndEnvironmentInfo
.
INSTANCE
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment