Changeset 4200 in josm


Ignore:
Timestamp:
Jul 3, 2011 8:08:43 PM (23 months ago)
Author:
jttt
Message:

Fix #4667 Make preferences file only readable by the owner

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r4191 r4200  
    2525import java.util.List; 
    2626import java.util.Map; 
     27import java.util.Map.Entry; 
    2728import java.util.Properties; 
    2829import java.util.SortedMap; 
    2930import java.util.TreeMap; 
    30 import java.util.Map.Entry; 
    3131import java.util.concurrent.CopyOnWriteArrayList; 
    3232import java.util.regex.Matcher; 
     
    3535import javax.swing.JOptionPane; 
    3636 
     37import org.openstreetmap.josm.Main; 
    3738import org.openstreetmap.josm.io.XmlWriter; 
    38 import org.openstreetmap.josm.Main; 
    3939import org.openstreetmap.josm.tools.ColorHelper; 
    4040import org.openstreetmap.josm.tools.Utils; 
     
    365365        if(Main.applet) 
    366366            return; 
    367         File prefFile = new File(getPreferencesDirFile(), "preferences"); 
     367 
     368        File prefFile = getPreferenceFile(); 
     369        File backupFile = new File(prefFile + "_backup"); 
    368370 
    369371        // Backup old preferences if there are old preferences 
    370372        if(prefFile.exists()) { 
    371             copyFile(prefFile, new File(prefFile + "_backup")); 
     373            copyFile(prefFile, backupFile); 
    372374        } 
    373375 
     
    386388        copyFile(tmpFile, prefFile); 
    387389        tmpFile.delete(); 
     390 
     391        setCorrectPermissions(prefFile); 
     392        setCorrectPermissions(backupFile); 
     393    } 
     394 
     395 
     396    private void setCorrectPermissions(File file) { 
     397        file.setReadable(false, false); 
     398        file.setWritable(false, false); 
     399        file.setExecutable(false, false); 
     400        file.setReadable(true, true); 
     401        file.setWritable(true, true); 
    388402    } 
    389403 
     
    560574    synchronized public Color getColor(String colName, String specName, Color def) { 
    561575        String colKey = colName.toLowerCase().replaceAll("[^a-z0-9]+","."); 
    562         if(!colKey.equals(colName)) 
     576        if(!colKey.equals(colName)) { 
    563577            colornames.put(colKey, colName); 
     578        } 
    564579        putDefault("color."+colKey, ColorHelper.color2html(def)); 
    565580        String colStr = specName != null ? get("color."+specName) : ""; 
     
    709724     * Used to read a 2-dimensional array of strings from the preference file. 
    710725     * If not a single entry could be found, def is returned. 
    711      */  
     726     */ 
    712727    synchronized public Collection<Collection<String>> getArray(String key, 
    713728            Collection<Collection<String>> def) 
    714     { 
    715         if(def != null) 
     729            { 
     730        if(def != null) { 
    716731            putArrayDefault(key, def); 
     732        } 
    717733        key += "."; 
    718734        int num = 0; 
     
    722738        } 
    723739        return num == 0 ? def : col; 
    724     } 
    725      
     740            } 
     741 
    726742    synchronized public boolean putArray(String key, Collection<Collection<String>> val) { 
    727743        boolean changed = false; 
     
    796812    public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) { 
    797813        Collection<Collection<String>> array = 
    798                 getArray(key, def == null ? null : serializeListOfStructs(def, klass)); 
     814            getArray(key, def == null ? null : serializeListOfStructs(def, klass)); 
    799815        if (array == null) 
    800816            return def == null ? null : new ArrayList<T>(def); 
     
    829845        Collection<Collection<String>> vals = new ArrayList<Collection<String>>(); 
    830846        for (T struct : l) { 
    831             if (struct == null) 
     847            if (struct == null) { 
    832848                continue; 
     849            } 
    833850            vals.add(serializeStruct(struct, klass)); 
    834851        } 
     
    937954     */ 
    938955    private final static String[] DEFAULT_PLUGIN_SITE = { 
    939         "http://josm.openstreetmap.de/plugin%<?plugins=>"}; 
     956    "http://josm.openstreetmap.de/plugin%<?plugins=>"}; 
    940957 
    941958    /** 
     
    973990        parser.map("collection", XMLCollection.class); 
    974991        parser.startWithValidation(in, 
    975         "http://josm.openstreetmap.de/preferences-1.0", "resource://data/preferences.xsd"); 
     992                "http://josm.openstreetmap.de/preferences-1.0", "resource://data/preferences.xsd"); 
    976993        LinkedList<String> vals = new LinkedList<String>(); 
    977994        while(parser.hasNext()) { 
     
    9901007    public String toXML(boolean nopass) { 
    9911008        StringBuilder b = new StringBuilder( 
    992         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
     1009                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
    9931010        "<preferences xmlns=\"http://josm.openstreetmap.de/preferences-1.0\">\n"); 
    9941011        for (Entry<String, String> p : properties.entrySet()) { 
Note: See TracChangeset for help on using the changeset viewer.