Changeset 29107 in osm for applications


Ignore:
Timestamp:
2012-12-22T17:02:35+01:00 (12 years ago)
Author:
donvip
Message:

[josm_opendata] correctly delete temp directories

Location:
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/OdPlugin.java

    r28696 r29107  
    6060import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleHandler;
    6161import org.openstreetmap.josm.plugins.opendata.core.modules.ModuleInformation;
     62import org.openstreetmap.josm.plugins.opendata.core.util.OdUtils;
    6263import org.openstreetmap.josm.tools.Pair;
    6364
     
    9596        // Add download task
    9697        Main.main.menu.openLocation.addDownloadTaskClass(DownloadDataTask.class);
     98        // Delete previous temp dirs if any (old plugin versions did not remove them correctly)
     99        OdUtils.deletePreviousTempDirs();
    97100        }
    98101       
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java

    r28696 r29107  
    4949import org.openstreetmap.josm.plugins.opendata.core.io.tabular.OdsReader;
    5050import org.openstreetmap.josm.plugins.opendata.core.io.tabular.XlsReader;
     51import org.openstreetmap.josm.plugins.opendata.core.util.OdUtils;
    5152
    5253public class ZipReader extends AbstractReader implements OdConstants {
     
    7576        }
    7677       
    77         private static final File createTempDir() throws IOException {
    78             final File temp = File.createTempFile("josm_opendata_temp_", Long.toString(System.nanoTime()));
    79 
    80             if (!temp.delete()) {
    81                 throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
    82             }
    83 
    84             if (!temp.mkdir()) {
    85                 throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
    86             }
    87            
    88             return temp;
    89         }
    90        
    91         private static final void deleteDir(File dir) {
    92         for (File file : dir.listFiles()) {
    93             file.delete();
    94         }
    95                 dir.delete();
    96         }
    97 
    9878        public DataSet parseDoc(final ProgressMonitor progressMonitor) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException  {
    9979               
    100             final File temp = createTempDir();
     80            final File temp = OdUtils.createTempDir();
    10181            final List<File> candidates = new ArrayList<File>();
    10282           
     
    217197                System.err.println(e.getMessage());
    218198            } finally {
    219                 deleteDir(temp);
     199                OdUtils.deleteDir(temp);
    220200                if (progressMonitor != null) {
    221201                        progressMonitor.finishTask();
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/util/OdUtils.java

    r28364 r29107  
    1616package org.openstreetmap.josm.plugins.opendata.core.util;
    1717
     18import java.io.File;
     19import java.io.FilenameFilter;
     20import java.io.IOException;
    1821import java.util.ArrayList;
    1922import java.util.List;
     
    3134public abstract class OdUtils {
    3235       
     36    private static final String TEMP_DIR_PREFIX = "josm_opendata_temp_";
     37   
    3338    public static final boolean isMultipolygon(OsmPrimitive p) {
    3439        return p instanceof Relation && ((Relation) p).isMultipolygon();
     
    8085        return degree + convertMinuteSecond(minute, second);
    8186    }
     87   
     88    public static final File createTempDir() throws IOException {
     89        final File temp = File.createTempFile(TEMP_DIR_PREFIX, Long.toString(System.nanoTime()));
     90
     91        if (!temp.delete()) {
     92            throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
     93        }
     94
     95        if (!temp.mkdir()) {
     96            throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
     97        }
     98       
     99        return temp;
     100    }
     101   
     102    public static final void deleteDir(File dir) {
     103        for (File file : dir.listFiles()) {
     104            if (!file.delete()) {
     105                file.deleteOnExit();
     106            }
     107        }
     108        if (!dir.delete()) {
     109            dir.deleteOnExit();
     110        }
     111    }
     112   
     113    public static final void deletePreviousTempDirs() {
     114        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
     115        if (tmpDir.exists() && tmpDir.isDirectory()) {
     116            for (File dir : tmpDir.listFiles(new FilenameFilter() {
     117                @Override
     118                public boolean accept(File dir, String name) {
     119                    return name.startsWith(TEMP_DIR_PREFIX);
     120                }
     121            })) {
     122                deleteDir(dir);
     123            }
     124        }
     125    }
    82126}
Note: See TracChangeset for help on using the changeset viewer.