use of java.nio.file.FileAlreadyExistsException in project alluxio by Alluxio.
the class AlluxioFuseFileSystem method renameInternal.
private int renameInternal(String oldPath, String newPath) {
final AlluxioURI oldUri = mPathResolverCache.getUnchecked(oldPath);
final AlluxioURI newUri = mPathResolverCache.getUnchecked(newPath);
final String name = newUri.getName();
if (name.length() > MAX_NAME_LENGTH) {
LOG.error("Failed to rename {} to {}, name {} is longer than {} characters", oldPath, newPath, name, MAX_NAME_LENGTH);
return -ErrorCodes.ENAMETOOLONG();
}
try {
mFileSystem.rename(oldUri, newUri);
OpenFileEntry oe = mOpenFiles.getFirstByField(PATH_INDEX, oldPath);
if (oe != null) {
oe.setPath(newPath);
}
} catch (FileDoesNotExistException e) {
LOG.debug("Failed to rename {} to {}, file {} does not exist", oldPath, newPath, oldPath);
return -ErrorCodes.ENOENT();
} catch (FileAlreadyExistsException e) {
LOG.debug("Failed to rename {} to {}, file {} already exists", oldPath, newPath, newPath);
return -ErrorCodes.EEXIST();
} catch (Throwable t) {
LOG.error("Failed to rename {} to {}", oldPath, newPath, t);
return AlluxioFuseUtils.getErrorCode(t);
}
return 0;
}
use of java.nio.file.FileAlreadyExistsException in project alluxio by Alluxio.
the class AlluxioFuseFileSystem method mkdirInternal.
private int mkdirInternal(String path, @mode_t long mode) {
final AlluxioURI turi = mPathResolverCache.getUnchecked(path);
if (turi.getName().length() > MAX_NAME_LENGTH) {
LOG.error("Failed to create directory {}, directory name is longer than {} characters", path, MAX_NAME_LENGTH);
return -ErrorCodes.ENAMETOOLONG();
}
SetAttributePOptions.Builder attributeOptionsBuilder = SetAttributePOptions.newBuilder();
FuseContext fc = getContext();
long uid = fc.uid.get();
long gid = fc.gid.get();
try {
if (gid != GID) {
String groupName = AlluxioFuseUtils.getGroupName(gid);
if (groupName.isEmpty()) {
// This should never be reached since input gid is always valid
LOG.error("Failed to get group name from gid {}.", gid);
return -ErrorCodes.EFAULT();
}
attributeOptionsBuilder.setGroup(groupName);
}
if (uid != UID) {
String userName = AlluxioFuseUtils.getUserName(uid);
if (userName.isEmpty()) {
// This should never be reached since input uid is always valid
LOG.error("Failed to get user name from uid {}", uid);
return -ErrorCodes.EFAULT();
}
attributeOptionsBuilder.setOwner(userName);
}
SetAttributePOptions setAttributePOptions = attributeOptionsBuilder.build();
mFileSystem.createDirectory(turi, CreateDirectoryPOptions.newBuilder().setMode(new alluxio.security.authorization.Mode((short) mode).toProto()).build());
if (gid != GID || uid != UID) {
LOG.debug("Set attributes of path {} to {}", path, setAttributePOptions);
mFileSystem.setAttribute(turi, setAttributePOptions);
}
} catch (FileAlreadyExistsException e) {
LOG.debug("Failed to create directory {}, directory already exists", path);
return -ErrorCodes.EEXIST();
} catch (InvalidPathException e) {
LOG.debug("Failed to create directory {}, path is invalid", path);
return -ErrorCodes.ENOENT();
} catch (Throwable t) {
LOG.error("Failed to create directory {}", path, t);
return AlluxioFuseUtils.getErrorCode(t);
}
return 0;
}
use of java.nio.file.FileAlreadyExistsException in project alluxio by Alluxio.
the class AlluxioFuseFileSystem method createInternal.
private int createInternal(String path, @mode_t long mode, FuseFileInfo fi) {
final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
if (uri.getName().length() > MAX_NAME_LENGTH) {
LOG.error("Failed to create {}, file name is longer than {} characters", path, MAX_NAME_LENGTH);
return -ErrorCodes.ENAMETOOLONG();
}
try {
if (mOpenFiles.size() >= MAX_OPEN_FILES) {
LOG.error("Cannot create {}: too many open files (MAX_OPEN_FILES: {})", path, MAX_OPEN_FILES);
return -ErrorCodes.EMFILE();
}
SetAttributePOptions.Builder attributeOptionsBuilder = SetAttributePOptions.newBuilder();
FuseContext fc = getContext();
long uid = fc.uid.get();
long gid = fc.gid.get();
if (gid != GID) {
String groupName = AlluxioFuseUtils.getGroupName(gid);
if (groupName.isEmpty()) {
// This should never be reached since input gid is always valid
LOG.error("Failed to get group name from gid {}.", gid);
return -ErrorCodes.EFAULT();
}
attributeOptionsBuilder.setGroup(groupName);
}
if (uid != UID) {
String userName = AlluxioFuseUtils.getUserName(uid);
if (userName.isEmpty()) {
// This should never be reached since input uid is always valid
LOG.error("Failed to get user name from uid {}", uid);
return -ErrorCodes.EFAULT();
}
attributeOptionsBuilder.setOwner(userName);
}
SetAttributePOptions setAttributePOptions = attributeOptionsBuilder.build();
FileOutStream os = mFileSystem.createFile(uri, CreateFilePOptions.newBuilder().setMode(new alluxio.security.authorization.Mode((short) mode).toProto()).build());
long fid = mNextOpenFileId.getAndIncrement();
mOpenFiles.add(new OpenFileEntry(fid, path, null, os));
fi.fh.set(fid);
if (gid != GID || uid != UID) {
LOG.debug("Set attributes of path {} to {}", path, setAttributePOptions);
mFileSystem.setAttribute(uri, setAttributePOptions);
}
} catch (FileAlreadyExistsException e) {
LOG.debug("Failed to create {}, file already exists", path);
return -ErrorCodes.EEXIST();
} catch (InvalidPathException e) {
LOG.debug("Failed to create {}, path is invalid", path);
return -ErrorCodes.ENOENT();
} catch (Throwable t) {
LOG.error("Failed to create {}", path, t);
return AlluxioFuseUtils.getErrorCode(t);
}
return 0;
}
use of java.nio.file.FileAlreadyExistsException in project coastal-hazards by USGS-CIDA.
the class FeatureCollectionExport method checkAndCreateFile.
private File checkAndCreateFile() throws IOException {
File shpFile = null;
if (!outputDirectory.exists()) {
FileUtils.forceMkdir(outputDirectory);
} else if (!outputDirectory.isDirectory()) {
throw new IOException("outputDirectory must be a directory");
} else {
// good to go?
}
String shpName = namePrefix + ".shp";
shpFile = FileUtils.getFile(outputDirectory, shpName);
if (shpFile.exists()) {
throw new FileAlreadyExistsException(shpName);
}
return shpFile;
}
use of java.nio.file.FileAlreadyExistsException in project zaproxy by zaproxy.
the class ExtensionAutoUpdate method installLocalAddOnQuietly.
boolean installLocalAddOnQuietly(Path file) {
AddOn ao;
try {
ao = new AddOn(file);
} catch (IOException e) {
logger.warn("Failed to create the add-on: {}", e.getMessage(), e);
return false;
}
if (!ao.canLoadInCurrentVersion()) {
logger.warn("Can not install the add-on, incompatible ZAP version.");
return false;
}
AddOn installedAddOn = this.getLocalVersionInfo().getAddOn(ao.getId());
if (installedAddOn != null) {
try {
if (Files.isSameFile(installedAddOn.getFile().toPath(), ao.getFile().toPath())) {
logger.warn("Can not install the add-on, same file already installed.");
return false;
}
} catch (IOException e) {
logger.warn("An error occurred while checking the add-ons' files: {}", e.getMessage(), e);
return false;
}
if (!uninstallAddOn(null, installedAddOn, true)) {
return false;
}
}
File addOnFile;
try {
addOnFile = copyAddOnFileToLocalPluginFolder(ao);
} catch (FileAlreadyExistsException e) {
logger.warn("Unable to copy add-on, a file with the same name already exists.", e);
return false;
} catch (IOException e) {
logger.warn("Unable to copy add-on to local plugin folder.", e);
return false;
}
ao.setFile(addOnFile);
return install(ao);
}
Aggregations