justCreated = path.createNewFile();
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug("Error in createNewFile: {}", e, e);
}
throw new NodeOSException(Constants.EIO, e);
}
if (justCreated) {
setMode(path, mode);
} else if ((flags & Constants.O_EXCL) != 0) {
NodeOSException ne = new NodeOSException(Constants.EEXIST);
ne.setPath(pathStr);
throw ne;
}
} else if (!path.exists()) {
NodeOSException ne = new NodeOSException(Constants.ENOENT);
ne.setPath(pathStr);
throw ne;
}
RandomAccessFile file = null;
if (path.isFile()) {
// Only open the file if it's actually a file -- we can still have an FD to a directory
String modeStr;
if ((flags & Constants.O_RDWR) != 0) {
modeStr = "rw";
} else if ((flags & Constants.O_WRONLY) != 0) {
// Java does not have write-only...
modeStr = "rw";
} else {
modeStr = "r";
}
if ((flags & Constants.O_SYNC) != 0) {
// And Java does not have read-only with sync either
modeStr = "rws";
}
try {
if (log.isDebugEnabled()) {
log.debug("Opening {} with {}", path.getPath(), modeStr);
}
file = new RandomAccessFile(path, modeStr);
if ((flags & Constants.O_TRUNC) != 0) {
file.setLength(0L);
} else if (((flags & Constants.O_APPEND) != 0) && (file.length() > 0)) {
file.seek(file.length());
}
} catch (FileNotFoundException fnfe) {
// We should only get here if O_CREAT was NOT set
if (log.isDebugEnabled()) {
log.debug("File not found: {}", path);
}
throw new NodeOSException(Constants.ENOENT);
} catch (IOException ioe) {
if (log.isDebugEnabled()) {
log.debug("I/O error: {}", ioe, ioe);
}
throw new NodeOSException(Constants.EIO, ioe);
}
}
Context.enter();
try {