Search in sources :

Example 16 with FileSystemOperationException

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;
}
Also used : Path(java.nio.file.Path) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 17 with FileSystemOperationException

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);
    }
}
Also used : FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) Pattern(java.util.regex.Pattern) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) FileMatchInfo(com.axway.ats.common.filesystem.FileMatchInfo) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 18 with FileSystemOperationException

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);
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 19 with FileSystemOperationException

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);
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 20 with FileSystemOperationException

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 + "'");
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)58 IOException (java.io.IOException)32 File (java.io.File)31 RandomAccessFile (java.io.RandomAccessFile)29 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)29 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)16 FileNotFoundException (java.io.FileNotFoundException)16 SocketTimeoutException (java.net.SocketTimeoutException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)13 EOFException (java.io.EOFException)13 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)13 FileOutputStream (java.io.FileOutputStream)10 FileInputStream (java.io.FileInputStream)9 BufferedOutputStream (java.io.BufferedOutputStream)8 DataOutputStream (java.io.DataOutputStream)8 BaseTest (com.axway.ats.action.BaseTest)7 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7