use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method getPermissions.
private String getPermissions(String sourceFile) {
int ownerPermissions = 0;
int groupPermissions = 0;
int othersPermissions = 0;
try {
Path path = Paths.get(sourceFile);
PosixFileAttributes attr;
attr = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
Set<PosixFilePermission> filePermissions = attr.permissions();
if (filePermissions.contains(PosixFilePermission.OWNER_READ)) {
ownerPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.OWNER_WRITE)) {
ownerPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
ownerPermissions += 1;
}
if (filePermissions.contains(PosixFilePermission.GROUP_READ)) {
groupPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.GROUP_WRITE)) {
groupPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.GROUP_EXECUTE)) {
groupPermissions += 1;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_READ)) {
othersPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_WRITE)) {
othersPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE)) {
othersPermissions += 1;
}
} catch (IOException ioe) {
throw new FileSystemOperationException("Could not get permissions for file '" + sourceFile + "'", ioe);
}
return "0" + ownerPermissions + groupPermissions + othersPermissions;
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method findTextInFileAfterGivenPosition.
@Override
public FileMatchInfo findTextInFileAfterGivenPosition(String fileName, String[] searchTexts, boolean isRegex, long searchFromPosition, int currentLineNumber) {
File targetFile = new File(fileName);
if (!targetFile.exists()) {
throw new FileDoesNotExistException(fileName);
}
if (searchFromPosition < 0L) {
searchFromPosition = 0L;
} else if (searchFromPosition > targetFile.length()) {
throw new FileSystemOperationException("The file '" + fileName + "' has size(" + targetFile.length() + " bytes) lower than the starting position for search (" + searchFromPosition + " byte). It is possible that file size had been cut since previous inspection but this is not supported.");
}
if (currentLineNumber <= 0) {
currentLineNumber = 1;
}
int matches = 0;
RandomAccessFile raf = null;
try {
List<Pattern> searchPatterns = null;
if (isRegex) {
searchPatterns = new ArrayList<Pattern>();
for (String searchText : searchTexts) {
searchPatterns.add(Pattern.compile(searchText, Pattern.DOTALL));
}
}
raf = new RandomAccessFile(targetFile, "r");
raf.seek(searchFromPosition);
List<String> matchedLines = new ArrayList<String>();
List<String> matchedPatterns = new ArrayList<String>();
List<Integer> matchedLineNumbers = new ArrayList<Integer>();
String line = null;
long lastLineByteOffset = searchFromPosition;
while ((line = IoUtils.readLineWithEOL(raf)) != null) {
for (int i = 0; i < searchTexts.length; i++) {
if ((isRegex && searchPatterns.get(i).matcher(line).matches()) || (!isRegex && line.contains(searchTexts[i]))) {
matches++;
matchedLines.add(line.trim());
matchedPatterns.add(searchTexts[i]);
matchedLineNumbers.add(currentLineNumber);
lastLineByteOffset = raf.getFilePointer();
break;
}
}
if (line.endsWith("\n") || line.endsWith("\r")) {
currentLineNumber++;
lastLineByteOffset = raf.getFilePointer();
}
}
return new FileMatchInfo(matches, currentLineNumber, lastLineByteOffset, matchedLines.toArray(new String[matchedLines.size()]), matchedLineNumbers.toArray(new Integer[matchedLines.size()]), matchedPatterns.toArray(new String[matchedPatterns.size()]));
} catch (IOException ioe) {
throw new FileSystemOperationException("Could not read file '" + fileName + "' seeking from byte " + searchFromPosition, ioe);
} finally {
IoUtils.closeStream(raf);
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method extractZip.
private void extractZip(String zipFilePath, String outputDirPath) {
ZipArchiveEntry zipEntry = null;
File outputDir = new File(outputDirPath);
// check if the dir is created
outputDir.mkdirs();
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
int unixPermissions = 0;
while (entries.hasMoreElements()) {
zipEntry = entries.nextElement();
if (log.isDebugEnabled()) {
log.debug("Extracting " + zipEntry.getName());
}
File entryDestination = new File(outputDirPath, zipEntry.getName());
unixPermissions = zipEntry.getUnixMode();
if (zipEntry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
InputStream in = null;
OutputStream out = null;
in = zipFile.getInputStream(zipEntry);
out = new BufferedOutputStream(new FileOutputStream(entryDestination));
IoUtils.copyStream(in, out);
}
if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
// check if the OS is UNIX
// set file/dir permissions, after it is created
Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(unixPermissions));
}
}
} catch (Exception e) {
String errorMsg = "Unable to unzip " + ((zipEntry != null) ? zipEntry.getName() + " from " : "") + zipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
throw new FileSystemOperationException(errorMsg, e);
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method unzip.
/**
* Unzip file to local or remote machine. If the machine is UNIX-like it will preserve the permissions
*
* @param zipFilePath the zip file path
* @param outputDirPath output directory. The directory will be created if it does not exist
* @throws FileSystemOperationException
*/
@Deprecated
@Override
public void unzip(String zipFilePath, String outputDirPath) throws FileSystemOperationException {
ZipArchiveEntry zipEntry = null;
File outputDir = new File(outputDirPath);
// check if the dir is created
outputDir.mkdirs();
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries();
int unixPermissions = 0;
while (entries.hasMoreElements()) {
zipEntry = entries.nextElement();
if (log.isDebugEnabled()) {
log.debug("Extracting " + zipEntry.getName());
}
File entryDestination = new File(outputDirPath, zipEntry.getName());
unixPermissions = zipEntry.getUnixMode();
if (zipEntry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
InputStream in = null;
OutputStream out = null;
in = zipFile.getInputStream(zipEntry);
out = new BufferedOutputStream(new FileOutputStream(entryDestination));
IoUtils.copyStream(in, out);
}
if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
// check if the OS is UNIX
// set file/dir permissions, after it is created
Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(unixPermissions));
}
}
} catch (Exception e) {
String errorMsg = "Unable to unzip " + ((zipEntry != null) ? zipEntry.getName() + " from " : "") + zipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
throw new FileSystemOperationException(errorMsg, e);
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method setFileModificationTime.
@Override
public void setFileModificationTime(String sourceFile, long lastModificationTime) {
File file = new File(sourceFile);
checkFileExistence(file);
if (!file.setLastModified(lastModificationTime)) {
throw new FileSystemOperationException("Could not set last modification time for file '" + sourceFile + "'");
}
}
Aggregations