Search in sources :

Example 1 with FileAttributes

use of com.intellij.openapi.util.io.FileAttributes in project intellij-community by JetBrains.

the class CompilerUtil method refreshOutputRoots.

/**
   * A lightweight procedure which ensures that given roots exist in the VFS.
   * No actual refresh is performed.
   */
public static void refreshOutputRoots(@NotNull Collection<String> outputRoots) {
    LocalFileSystem fs = LocalFileSystem.getInstance();
    Collection<VirtualFile> toRefresh = ContainerUtil.newHashSet();
    for (String outputRoot : outputRoots) {
        FileAttributes attributes = FileSystemUtil.getAttributes(FileUtil.toSystemDependentName(outputRoot));
        VirtualFile vFile = fs.findFileByPath(outputRoot);
        if (attributes != null && vFile == null) {
            VirtualFile parent = fs.refreshAndFindFileByPath(PathUtil.getParentPath(outputRoot));
            if (parent != null && toRefresh.add(parent)) {
                parent.getChildren();
            }
        } else if (attributes == null && vFile != null || attributes != null && attributes.isDirectory() != vFile.isDirectory()) {
            toRefresh.add(vFile);
        }
    }
    if (!toRefresh.isEmpty()) {
        RefreshQueue.getInstance().refresh(false, false, null, toRefresh);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) FileAttributes(com.intellij.openapi.util.io.FileAttributes)

Example 2 with FileAttributes

use of com.intellij.openapi.util.io.FileAttributes in project intellij-community by JetBrains.

the class JarFileSystemTest method testJarHandlerDoNotCreateCopyWhenListingArchive.

@Test
public void testJarHandlerDoNotCreateCopyWhenListingArchive() throws Exception {
    File jar = IoTestUtil.createTestJar(tempDir.newFile("test.jar"));
    JarHandler handler = new JarHandler(jar.getPath());
    FileAttributes attributes = handler.getAttributes(JarFile.MANIFEST_NAME);
    assertNotNull(attributes);
    assertEquals(0, attributes.length);
    assertTimestampsEqual(jar.lastModified(), attributes.lastModified);
    if (((JarFileSystemImpl) JarFileSystem.getInstance()).isMakeCopyOfJar(jar)) {
        // for performance reasons we create file copy on windows when we read contents and have the handle open to the copy
        Field resolved = handler.getClass().getDeclaredField("myFileWithMirrorResolved");
        resolved.setAccessible(true);
        assertTrue(resolved.get(handler) == null);
    }
}
Also used : Field(java.lang.reflect.Field) JarFileSystemImpl(com.intellij.openapi.vfs.impl.jar.JarFileSystemImpl) JarFile(java.util.jar.JarFile) JarHandler(com.intellij.openapi.vfs.impl.jar.JarHandler) FileAttributes(com.intellij.openapi.util.io.FileAttributes) Test(org.junit.Test)

Example 3 with FileAttributes

use of com.intellij.openapi.util.io.FileAttributes in project intellij-community by JetBrains.

the class LocalFileSystemTest method testWindowsHiddenDirectory.

public void testWindowsHiddenDirectory() throws Exception {
    if (!SystemInfo.isWindows) {
        System.err.println(getName() + " skipped: " + SystemInfo.OS_NAME);
        return;
    }
    File file = new File("C:\\Documents and Settings\\desktop.ini");
    if (!file.exists()) {
        System.err.println(getName() + " skipped: missing " + file);
        return;
    }
    String parent = FileUtil.toSystemIndependentName(file.getParent());
    VfsRootAccess.allowRootAccess(getTestRootDisposable(), parent);
    VirtualFile virtualFile = myFS.refreshAndFindFileByIoFile(file);
    assertNotNull(virtualFile);
    NewVirtualFileSystem fs = (NewVirtualFileSystem) virtualFile.getFileSystem();
    FileAttributes attributes = fs.getAttributes(virtualFile);
    assertNotNull(attributes);
    assertEquals(FileAttributes.Type.FILE, attributes.type);
    assertEquals(FileAttributes.HIDDEN, attributes.flags);
}
Also used : File(java.io.File) FileAttributes(com.intellij.openapi.util.io.FileAttributes)

Example 4 with FileAttributes

use of com.intellij.openapi.util.io.FileAttributes in project intellij-community by JetBrains.

the class JarHandler method getMirrorFile.

private File getMirrorFile(@NotNull File originalFile) {
    if (!myFileSystem.isMakeCopyOfJar(originalFile))
        return originalFile;
    final FileAttributes originalAttributes = FileSystemUtil.getAttributes(originalFile);
    if (originalAttributes == null)
        return originalFile;
    final String folderPath = getJarsDir();
    if (!new File(folderPath).exists() && !new File(folderPath).mkdirs()) {
        return originalFile;
    }
    if (FSRecords.weHaveContentHashes) {
        return getMirrorWithContentHash(originalFile, originalAttributes);
    }
    final String mirrorName = originalFile.getName() + "." + Integer.toHexString(originalFile.getPath().hashCode());
    final File mirrorFile = new File(folderPath, mirrorName);
    final FileAttributes mirrorAttributes = FileSystemUtil.getAttributes(mirrorFile);
    return mirrorDiffers(originalAttributes, mirrorAttributes, false) ? copyToMirror(originalFile, mirrorFile) : mirrorFile;
}
Also used : FileAttributes(com.intellij.openapi.util.io.FileAttributes) ZipFile(java.util.zip.ZipFile)

Example 5 with FileAttributes

use of com.intellij.openapi.util.io.FileAttributes in project intellij-community by JetBrains.

the class JarHandler method getMirrorWithContentHash.

private File getMirrorWithContentHash(File originalFile, FileAttributes originalAttributes) {
    File mirrorFile = null;
    String jarDir = getJarsDir();
    try {
        String path = originalFile.getPath();
        CacheLibraryInfo info = CacheLibraryInfo.ourCachedLibraryInfo.get(path);
        if (info != null && originalAttributes.length == info.myFileLength && Math.abs(originalAttributes.lastModified - info.myModificationTime) <= FS_TIME_RESOLUTION) {
            mirrorFile = new File(jarDir, info.mySnapshotPath);
            if (!mirrorDiffers(originalAttributes, FileSystemUtil.getAttributes(mirrorFile), true)) {
                return mirrorFile;
            }
        }
        MessageDigest sha1;
        File tempJarFile = null;
        try {
            tempJarFile = FileUtil.createTempFile(new File(jarDir), originalFile.getName(), "", true, false);
            try (DataOutputStream os = new DataOutputStream(new FileOutputStream(tempJarFile));
                FileInputStream is = new FileInputStream(originalFile)) {
                sha1 = MessageDigest.getInstance("SHA1");
                sha1.update(String.valueOf(originalAttributes.length).getBytes(Charset.defaultCharset()));
                sha1.update((byte) 0);
                byte[] buffer = new byte[8192];
                long totalBytes = 0;
                while (true) {
                    int read = is.read(buffer);
                    if (read < 0)
                        break;
                    totalBytes += read;
                    sha1.update(buffer, 0, read);
                    os.write(buffer, 0, read);
                    if (totalBytes == originalAttributes.length)
                        break;
                }
            }
        } catch (IOException ex) {
            File target = mirrorFile != null ? mirrorFile : tempJarFile != null ? tempJarFile : new File(jarDir);
            reportIOErrorWithJars(originalFile, target, ex);
            return originalFile;
        } catch (NoSuchAlgorithmException ex) {
            LOG.error(ex);
            // should never happen for sha1
            return originalFile;
        }
        String mirrorName = getSnapshotName(originalFile.getName(), sha1.digest());
        mirrorFile = new File(jarDir, mirrorName);
        FileAttributes mirrorFileAttributes = FileSystemUtil.getAttributes(mirrorFile);
        if (mirrorFileAttributes == null) {
            try {
                FileUtil.rename(tempJarFile, mirrorFile);
                FileUtil.setLastModified(mirrorFile, originalAttributes.lastModified);
            } catch (IOException ex) {
                reportIOErrorWithJars(originalFile, mirrorFile, ex);
                return originalFile;
            }
        } else {
            FileUtil.delete(tempJarFile);
        }
        info = new CacheLibraryInfo(mirrorFile.getName(), originalAttributes.lastModified, originalAttributes.length);
        CacheLibraryInfo.ourCachedLibraryInfo.put(path, info);
        return mirrorFile;
    } catch (IOException ex) {
        CacheLibraryInfo.ourCachedLibraryInfo.markCorrupted();
        reportIOErrorWithJars(originalFile, mirrorFile != null ? mirrorFile : new File(jarDir, originalFile.getName()), ex);
        return originalFile;
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) ZipFile(java.util.zip.ZipFile) FileAttributes(com.intellij.openapi.util.io.FileAttributes)

Aggregations

FileAttributes (com.intellij.openapi.util.io.FileAttributes)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)4 AccessToken (com.intellij.openapi.application.AccessToken)2 Pair (com.intellij.openapi.util.Pair)2 FileInfo (com.intellij.openapi.util.io.win32.FileInfo)2 VirtualFileWithId (com.intellij.openapi.vfs.VirtualFileWithId)2 FakeVirtualFile (com.intellij.openapi.vfs.newvfs.impl.FakeVirtualFile)2 THashMap (gnu.trove.THashMap)2 ZipFile (java.util.zip.ZipFile)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)1 JarFileSystemImpl (com.intellij.openapi.vfs.impl.jar.JarFileSystemImpl)1 JarHandler (com.intellij.openapi.vfs.impl.jar.JarHandler)1 VFileCreateEvent (com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent)1 VirtualDirectoryImpl (com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 Field (java.lang.reflect.Field)1