You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pa...@apache.org on 2020/09/21 15:32:59 UTC

[felix-dev] branch connect updated: Cleanup file handling.

This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch connect
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/connect by this push:
     new 5560298  Cleanup file handling.
5560298 is described below

commit 5560298339e2d1c9718449e2bea2b360aa3e2b86
Author: Karl Pauls <ka...@gmail.com>
AuthorDate: Mon Sep 21 17:32:45 2020 +0200

    Cleanup file handling.
---
 framework/pom.xml                                  |   2 +-
 .../felix/framework/BundleProtectionDomain.java    |  11 +-
 .../apache/felix/framework/ExtensionManager.java   |  10 +-
 .../java/org/apache/felix/framework/Felix.java     |  10 +-
 .../felix/framework/cache/BundleArchive.java       |  12 +--
 .../apache/felix/framework/cache/BundleCache.java  |  16 +--
 .../felix/framework/cache/DirectoryContent.java    |   6 +-
 .../felix/framework/cache/DirectoryRevision.java   |   2 +-
 .../apache/felix/framework/cache/JarRevision.java  |   2 +-
 .../apache/felix/framework/util/SecureAction.java  | 118 ++++++++++++++++++++-
 10 files changed, 137 insertions(+), 52 deletions(-)

diff --git a/framework/pom.xml b/framework/pom.xml
index a5a6197..52b0b24 100644
--- a/framework/pom.xml
+++ b/framework/pom.xml
@@ -21,7 +21,7 @@
     <groupId>org.apache.felix</groupId>
     <artifactId>felix-parent</artifactId>
     <version>6</version>
-    <relativePath>../../pom/pom.xml</relativePath>
+    <relativePath>../pom/pom.xml</relativePath>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <packaging>bundle</packaging>
diff --git a/framework/src/main/java/org/apache/felix/framework/BundleProtectionDomain.java b/framework/src/main/java/org/apache/felix/framework/BundleProtectionDomain.java
index 15d7145..9d51085 100644
--- a/framework/src/main/java/org/apache/felix/framework/BundleProtectionDomain.java
+++ b/framework/src/main/java/org/apache/felix/framework/BundleProtectionDomain.java
@@ -21,13 +21,10 @@ package org.apache.felix.framework;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.ref.WeakReference;
-import java.lang.reflect.InvocationTargetException;
-import java.net.JarURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
@@ -45,16 +42,12 @@ import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import org.apache.felix.framework.cache.Content;
 import org.apache.felix.framework.cache.JarContent;
 import org.apache.felix.framework.util.FelixConstants;
-import org.osgi.framework.Bundle;
 import org.osgi.framework.PackagePermission;
 
-import org.osgi.framework.wiring.BundleRevision;
-
 public class BundleProtectionDomain extends ProtectionDomain
 {
     private static final class BundleInputStream extends InputStream
@@ -264,12 +257,12 @@ public class BundleProtectionDomain extends ProtectionDomain
                 {
                     target = Felix.m_secureAction.createTempFile("jar", null, null);
                     Felix.m_secureAction.deleteFileOnExit(target);
-                    FileOutputStream output = null;
+                    OutputStream output = null;
                     InputStream input = null;
                     IOException rethrow = null;
                     try
                     {
-                        output = new FileOutputStream(target);
+                        output = Felix.m_secureAction.getOutputStream(target);
                         input = new BundleInputStream(content);
                         byte[] buffer = new byte[64 * 1024];
                         for (int i = input.read(buffer);i != -1; i = input.read(buffer))
diff --git a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
index 26c4077..001d2c9 100644
--- a/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
+++ b/framework/src/main/java/org/apache/felix/framework/ExtensionManager.java
@@ -54,8 +54,6 @@ import org.osgi.framework.wiring.BundleWire;
 import org.osgi.framework.wiring.BundleWiring;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -287,11 +285,11 @@ class ExtensionManager implements Content
                         {
                             Properties cachedProps = new Properties();
                             File modulesDir = felix.getDataFile(felix, "modules");
-                            modulesDir.mkdirs();
+                            Felix.m_secureAction.mkdirs(modulesDir);
                             File cached = new File(modulesDir, moduleKey + ".properties");
-                            if (cached.isFile())
+                            if (Felix.m_secureAction.isFile(cached))
                             {
-                                FileInputStream input = new FileInputStream(cached);
+                                InputStream input = Felix.m_secureAction.getInputStream(cached);
                                 cachedProps.load(new InputStreamReader(input, "UTF-8"));
                                 input.close();
                                 for (Enumeration<?> keys = cachedProps.propertyNames(); keys.hasMoreElements();)
@@ -313,7 +311,7 @@ class ExtensionManager implements Content
                                         cachedProps.setProperty(pkg, String.join(",", uses));
                                     }
                                 }
-                                OutputStream output = new FileOutputStream(cached);
+                                OutputStream output = Felix.m_secureAction.getOutputStream(cached);
                                 cachedProps.store(new OutputStreamWriter(output, "UTF-8"), null);
                                 output.close();
                             }
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index c3191de..2bc5865 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -75,9 +75,7 @@ import org.osgi.service.resolver.ResolutionException;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -1006,7 +1004,7 @@ public class Felix extends BundleImpl implements Framework
             BufferedReader input = null;
             try
             {
-                input = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile), "UTF-8"));
+                input = new BufferedReader(new InputStreamReader(m_secureAction.getInputStream(dataFile), "UTF-8"));
                 lastVersion = Version.parseVersion(input.readLine()).getMajor();
             }
             catch (Exception ignore)
@@ -1039,7 +1037,7 @@ public class Felix extends BundleImpl implements Framework
         try
         {
             output = new PrintWriter(new OutputStreamWriter(
-                new FileOutputStream(getDataFile(this, "last.java.version")), "UTF-8"));
+                m_secureAction.getOutputStream(getDataFile(this, "last.java.version")), "UTF-8"));
             output.println(Integer.toString(currentVersion));
             output.flush();
         }
@@ -5044,7 +5042,7 @@ public class Felix extends BundleImpl implements Framework
             try
             {
                 File file = m_cache.getSystemBundleDataFile("bundle.id");
-                is = m_secureAction.getFileInputStream(file);
+                is = m_secureAction.getInputStream(file);
                 br = new BufferedReader(new InputStreamReader(is));
                 return Long.parseLong(br.readLine());
             }
@@ -5097,7 +5095,7 @@ public class Felix extends BundleImpl implements Framework
             try
             {
                 File file = m_cache.getSystemBundleDataFile("bundle.id");
-                os = m_secureAction.getFileOutputStream(file);
+                os = m_secureAction.getOutputStream(file);
                 bw = new BufferedWriter(new OutputStreamWriter(os));
                 String s = Long.toString(m_nextId);
                 bw.write(s, 0, s.length());
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/BundleArchive.java b/framework/src/main/java/org/apache/felix/framework/cache/BundleArchive.java
index 35026f1..50e4d31 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/BundleArchive.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/BundleArchive.java
@@ -21,13 +21,11 @@ package org.apache.felix.framework.cache;
 import java.io.*;
 
 import java.util.Map;
-import java.util.Optional;
 import java.util.SortedMap;
 import java.util.TreeMap;
 import org.apache.felix.framework.Logger;
 import org.apache.felix.framework.util.WeakZipFileFactory;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.connect.ModuleConnector;
 import org.osgi.framework.connect.ConnectModule;
@@ -193,7 +191,7 @@ public class BundleArchive
         for (File child : children)
         {
             if (child.getName().startsWith(REVISION_DIRECTORY)
-                && child.isDirectory())
+                && BundleCache.getSecureAction().isFileDirectory(child))
             {
                 // Determine the revision number and add it to the revision map.
                 int idx = child.getName().lastIndexOf('.');
@@ -555,7 +553,7 @@ public class BundleArchive
         BufferedReader br = null;
         try
         {
-            is = BundleCache.getSecureAction().getFileInputStream(new File(
+            is = BundleCache.getSecureAction().getInputStream(new File(
                 new File(m_archiveRootDir, REVISION_DIRECTORY +
                 getRefreshCount() + "." + revNum.toString()), REVISION_LOCATION_FILE));
 
@@ -578,7 +576,7 @@ public class BundleArchive
         try
         {
             os = BundleCache.getSecureAction()
-                .getFileOutputStream(new File(
+                .getOutputStream(new File(
                     new File(m_archiveRootDir, REVISION_DIRECTORY +
                     getRefreshCount() + "." + revNum.toString()), REVISION_LOCATION_FILE));
             bw = new BufferedWriter(new OutputStreamWriter(os));
@@ -892,7 +890,7 @@ public class BundleArchive
         try
         {
             is = BundleCache.getSecureAction()
-                .getFileInputStream(infoFile);
+                .getInputStream(infoFile);
             br = new BufferedReader(new InputStreamReader(is));
 
             // Read id.
@@ -923,7 +921,7 @@ public class BundleArchive
         try
         {
             os = BundleCache.getSecureAction()
-                .getFileOutputStream(new File(m_archiveRootDir, BUNDLE_INFO_FILE));
+                .getOutputStream(new File(m_archiveRootDir, BUNDLE_INFO_FILE));
             bw = new BufferedWriter(new OutputStreamWriter(os));
 
             // Write id.
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java b/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
index 022cdb4..73cfae8 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/BundleCache.java
@@ -25,7 +25,6 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.connect.ModuleConnector;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -145,25 +144,14 @@ public class BundleCache
         {
             File lockFile = new File(cacheDir, CACHE_LOCK_NAME);
             FileChannel fc = null;
-            FileOutputStream fos = null;
             try
             {
-                if (!getSecureAction().fileExists(lockFile))
-                {
-                    fos = getSecureAction().getFileOutputStream(lockFile);
-                    fc = fos.getChannel();
-                }
-                else
-                {
-                    fos = getSecureAction().getFileOutputStream(lockFile);
-                    fc = fos.getChannel();
-                }
+                fc = getSecureAction().getFileChannel(lockFile);
             }
             catch (Exception ex)
             {
                 try
                 {
-                    if (fos != null) fos.close();
                     if (fc != null) fc.close();
                 }
                 catch (Exception ex2)
@@ -553,7 +541,7 @@ public class BundleCache
 
         try
         {
-            os = getSecureAction().getFileOutputStream(outputFile);
+            os = getSecureAction().getOutputStream(outputFile);
             for (int i = is.read(bytes);i != -1; i = is.read(bytes))
             {
                 os.write(bytes, 0, i);
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
index 9046e17..0240933 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryContent.java
@@ -102,7 +102,7 @@ public class DirectoryContent implements Content
         try
         {
 
-            return BundleCache.getSecureAction().fileExists(file) ? BundleCache.read(BundleCache.getSecureAction().getFileInputStream(file), file.length()) : null;
+            return BundleCache.getSecureAction().fileExists(file) ? BundleCache.read(BundleCache.getSecureAction().getInputStream(file), file.length()) : null;
         }
         catch (Exception ex)
         {
@@ -121,7 +121,7 @@ public class DirectoryContent implements Content
         File file = new File(m_dir, name);
         try
         {
-            return BundleCache.getSecureAction().fileExists(file) ? BundleCache.getSecureAction().getFileInputStream(file) : null;
+            return BundleCache.getSecureAction().fileExists(file) ? BundleCache.getSecureAction().getInputStream(file) : null;
         }
         catch (Exception ex)
         {
@@ -284,7 +284,7 @@ public class DirectoryContent implements Content
 
                         try
                         {
-                            is = BundleCache.getSecureAction().getFileInputStream(entryFile);
+                            is = BundleCache.getSecureAction().getInputStream(entryFile);
 
                             // Create the file.
                             BundleCache.copyStreamToFile(is, libFile);
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java
index a1cf608..f6d01a9 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/DirectoryRevision.java
@@ -71,7 +71,7 @@ class DirectoryRevision extends BundleArchiveRevision
         throws Exception
     {
         File manifest = new File(m_refDir, "META-INF/MANIFEST.MF");
-        return manifest.isFile() ? BundleCache.getMainAttributes(new StringMap(), BundleCache.getSecureAction().getFileInputStream(manifest), manifest.length()) : null;
+        return BundleCache.getSecureAction().isFile(manifest) ? BundleCache.getMainAttributes(new StringMap(), BundleCache.getSecureAction().getInputStream(manifest), manifest.length()) : null;
     }
 
     public Content getContent() throws Exception
diff --git a/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java b/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java
index ac5c1e8..4ff4e84 100644
--- a/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java
+++ b/framework/src/main/java/org/apache/felix/framework/cache/JarRevision.java
@@ -147,7 +147,7 @@ class JarRevision extends BundleArchiveRevision
                             // set request properties such as proxy auth.
                             URL url = BundleCache.getSecureAction().createURL(
                                 null, getLocation(), null);
-                            conn = url.openConnection();
+                            conn = BundleCache.getSecureAction().openURLConnection(url);
 
                             // Support for http proxy authentication.
                             String auth = BundleCache.getSecureAction()
diff --git a/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java b/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
index a37f524..a88a9cc 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/SecureAction.java
@@ -22,16 +22,16 @@ import java.io.*;
 import java.lang.reflect.*;
 import java.lang.reflect.Proxy;
 import java.net.*;
+import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
 import java.security.*;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Consumer;
 import java.util.jar.JarFile;
-import java.util.stream.Stream;
 import java.util.zip.ZipFile;
 
 import org.osgi.framework.Bundle;
@@ -62,7 +62,8 @@ public class SecureAction
 {
     private static final byte[] accessor;
 
-    static {
+    static
+    {
         byte[] result;
 
         try (ByteArrayOutputStream output = new ByteArrayOutputStream();
@@ -349,6 +350,28 @@ public class SecureAction
         }
     }
 
+    public boolean isFile(File file)
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                Actions actions = (Actions) m_actions.get();
+                actions.set(Actions.FILE_IS_FILE_ACTION, file);
+                return ((Boolean) AccessController.doPrivileged(actions, m_acc))
+                        .booleanValue();
+            }
+            catch (PrivilegedActionException ex)
+            {
+                throw (RuntimeException) ex.getException();
+            }
+        }
+        else
+        {
+            return file.isFile();
+        }
+    }
+
     public boolean isFileDirectory(File file)
     {
         if (System.getSecurityManager() != null)
@@ -458,6 +481,56 @@ public class SecureAction
         }
     }
 
+    public InputStream getInputStream(File file) throws IOException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                Actions actions = (Actions) m_actions.get();
+                actions.set(Actions.GET_INPUT_ACTION, file);
+                return (InputStream) AccessController.doPrivileged(actions, m_acc);
+            }
+            catch (PrivilegedActionException ex)
+            {
+                if (ex.getException() instanceof IOException)
+                {
+                    throw (IOException) ex.getException();
+                }
+                throw (RuntimeException) ex.getException();
+            }
+        }
+        else
+        {
+            return Files.newInputStream(file.toPath());
+        }
+    }
+
+    public OutputStream getOutputStream(File file) throws IOException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                Actions actions = (Actions) m_actions.get();
+                actions.set(Actions.GET_OUTPUT_ACTION, file);
+                return (OutputStream) AccessController.doPrivileged(actions, m_acc);
+            }
+            catch (PrivilegedActionException ex)
+            {
+                if (ex.getException() instanceof IOException)
+                {
+                    throw (IOException) ex.getException();
+                }
+                throw (RuntimeException) ex.getException();
+            }
+        }
+        else
+        {
+            return Files.newOutputStream(file.toPath());
+        }
+    }
+
     public FileInputStream getFileInputStream(File file) throws IOException
     {
         if (System.getSecurityManager() != null)
@@ -508,6 +581,31 @@ public class SecureAction
         }
     }
 
+    public FileChannel getFileChannel(File file) throws IOException
+    {
+        if (System.getSecurityManager() != null)
+        {
+            try
+            {
+                Actions actions = (Actions) m_actions.get();
+                actions.set(Actions.GET_FILE_CHANNEL_ACTION, file);
+                return (FileChannel) AccessController.doPrivileged(actions, m_acc);
+            }
+            catch (PrivilegedActionException ex)
+            {
+                if (ex.getException() instanceof IOException)
+                {
+                    throw (IOException) ex.getException();
+                }
+                throw (RuntimeException) ex.getException();
+            }
+        }
+        else
+        {
+            return FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
+        }
+    }
+
     public URI toURI(File file)
     {
         if (System.getSecurityManager() != null)
@@ -1699,6 +1797,10 @@ public class SecureAction
         public static final int GET_CANONICAL_PATH = 57;
         public static final int CREATE_PROXY = 58;
         public static final int LAST_MODIFIED = 59;
+        public static final int FILE_IS_FILE_ACTION = 60;
+        public static final int GET_FILE_CHANNEL_ACTION = 61;
+        private static final int GET_INPUT_ACTION = 62;
+        private static final int GET_OUTPUT_ACTION = 63;
 
         private int m_action = -1;
         private Object m_arg1 = null;
@@ -1964,6 +2066,14 @@ public class SecureAction
                             (InvocationHandler) arg3);
                 case LAST_MODIFIED:
                     return ((File) arg1).lastModified();
+                case FILE_IS_FILE_ACTION:
+                    return ((File) arg1).isFile() ? Boolean.TRUE : Boolean.FALSE;
+                case GET_FILE_CHANNEL_ACTION:
+                    return FileChannel.open(((File) arg1).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
+                case GET_INPUT_ACTION:
+                    return Files.newInputStream(((File) arg1).toPath());
+                case GET_OUTPUT_ACTION:
+                    return Files.newOutputStream(((File) arg1).toPath());
             }
 
             return null;