jdk/src/solaris/classes/java/util/prefs/FileSystemPreferences.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8543 e5ec12a932da
child 22951 5fd21112b2b6
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8543
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.prefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
4230
e208dda74f1c 6899607: Update java.util.prefs.FileSystemPreferences to use PlatformLogger
mchung
parents: 715
diff changeset
    34
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Preferences implementation for Unix.  Preferences are stored in the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * system, with one directory per preferences node.  All of the preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * at each node are stored in a single file.  Atomic file system operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * (e.g. File.renameTo) are used to ensure integrity.  An in-memory cache of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the "explored" portion of the tree is maintained for performance, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * written back to the disk periodically.  File-locking is used to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * reasonable behavior when multiple VMs are running at the same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * (The file lock is obtained only for sync(), flush() and removeNode().)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see     Preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class FileSystemPreferences extends AbstractPreferences {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Sync interval in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int SYNC_INTERVAL = Math.max(1,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    55
        Integer.parseInt(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    56
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    57
                new sun.security.action.GetPropertyAction(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    58
                    "java.util.prefs.syncInterval", "30"))));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Returns logger for error messages. Backing store exceptions are logged at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * WARNING level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
4230
e208dda74f1c 6899607: Update java.util.prefs.FileSystemPreferences to use PlatformLogger
mchung
parents: 715
diff changeset
    64
    private static PlatformLogger getLogger() {
e208dda74f1c 6899607: Update java.util.prefs.FileSystemPreferences to use PlatformLogger
mchung
parents: 715
diff changeset
    65
        return PlatformLogger.getLogger("java.util.prefs");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Directory for system preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static File systemRootDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Flag, indicating whether systemRoot  directory is writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static boolean isSystemRootWritable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Directory for user preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static File userRootDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Flag, indicating whether userRoot  directory is writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static boolean isUserRootWritable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The user root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static Preferences userRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static synchronized Preferences getUserRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (userRoot == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            setupUserRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            userRoot = new FileSystemPreferences(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return userRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static void setupUserRoot() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   102
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   103
            public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                userRootDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                      new File(System.getProperty("java.util.prefs.userRoot",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                      System.getProperty("user.home")), ".java/.userPrefs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                // Attempt to create root dir if it does not yet exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (!userRootDir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    if (userRootDir.mkdirs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            chmod(userRootDir.getCanonicalPath(), USER_RWX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            getLogger().warning("Could not change permissions" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                " on userRoot directory. ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        getLogger().info("Created user preferences directory.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        getLogger().warning("Couldn't create user preferences" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        " directory. User preferences are unusable.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                isUserRootWritable = userRootDir.canWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                String USER_NAME = System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                userLockFile = new File (userRootDir,".user.lock." + USER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                userRootModFile = new File (userRootDir,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                               ".userRootModFile." + USER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (!userRootModFile.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    // create if does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    userRootModFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    // Only user can read/write userRootModFile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    int result = chmod(userRootModFile.getCanonicalPath(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                               USER_READ_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    if (result !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        getLogger().warning("Problem creating userRoot " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                            "mod file. Chmod failed on " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                             userRootModFile.getCanonicalPath() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                             " Unix error code " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    getLogger().warning(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                userRootModTime = userRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * The system root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static Preferences systemRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static synchronized Preferences getSystemRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (systemRoot == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            setupSystemRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            systemRoot = new FileSystemPreferences(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return systemRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static void setupSystemRoot() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   163
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   164
            public Void run() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   165
                String systemPrefsDirName =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                  System.getProperty("java.util.prefs.systemRoot","/etc/.java");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                systemRootDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                     new File(systemPrefsDirName, ".systemPrefs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                // Attempt to create root dir if it does not yet exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (!systemRootDir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    // system root does not exist in /etc/.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    // Switching  to java.home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    systemRootDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                  new File(System.getProperty("java.home"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                                            ".systemPrefs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    if (!systemRootDir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        if (systemRootDir.mkdirs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            getLogger().info(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                "Created system preferences directory "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                + "in java.home.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                chmod(systemRootDir.getCanonicalPath(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                                          USER_RWX_ALL_RX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            getLogger().warning("Could not create "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                + "system preferences directory. System "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                + "preferences are unusable.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                isSystemRootWritable = systemRootDir.canWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                systemLockFile = new File(systemRootDir, ".system.lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                systemRootModFile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                               new File (systemRootDir,".systemRootModFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (!systemRootModFile.exists() && isSystemRootWritable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    // create if does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    systemRootModFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    int result = chmod(systemRootModFile.getCanonicalPath(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                                          USER_RW_ALL_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    if (result !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        getLogger().warning("Chmod failed on " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                               systemRootModFile.getCanonicalPath() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                              " Unix error code " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                } catch (IOException e) { getLogger().warning(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                systemRootModTime = systemRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Unix user write/read permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private static final int USER_READ_WRITE = 0600;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static final int USER_RW_ALL_READ = 0644;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private static final int USER_RWX_ALL_RX = 0755;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static final int USER_RWX = 0700;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The lock file for the user tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    static File userLockFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * The lock file for the system tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    static File systemLockFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Unix lock handle for userRoot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Zero, if unlocked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private static int userRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Unix lock handle for systemRoot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Zero, if unlocked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static int systemRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * The directory representing this preference node.  There is no guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * that this directory exits, as another VM can delete it at any time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * that it (the other VM) holds the file-lock.  While the root node cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * be deleted, it may not yet have been created, or the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * directory could have been deleted accidentally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private final File dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * The file representing this preference node's preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * The file format is undocumented, and subject to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * from release to release, but I'm sure that you can figure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * it out if you try real hard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private final File prefsFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * A temporary file used for saving changes to preferences.  As part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the sync operation, changes are first saved into this file, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * atomically renamed to prefsFile.  This results in an atomic state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * change from one valid set of preferences to another.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * the file-lock is held for the duration of this transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private final File tmpFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * File, which keeps track of global modifications of userRoot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static  File userRootModFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Flag, which indicated whether userRoot was modified by another VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private static boolean isUserRootModified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Keeps track of userRoot modification time. This time is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * zero after UNIX reboot, and is increased by 1 second each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * userRoot is modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private static long userRootModTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * File, which keeps track of global modifications of systemRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private static File systemRootModFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Flag, which indicates whether systemRoot was modified by another VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static boolean isSystemRootModified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Keeps track of systemRoot modification time. This time is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * zero after system reboot, and is increased by 1 second each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * systemRoot is modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private static long systemRootModTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Locally cached preferences for this node (includes uncommitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * changes).  This map is initialized with from disk when the first get or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * put operation occurs on this node.  It is synchronized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * corresponding disk file (prefsFile) by the sync operation.  The initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * value is read *without* acquiring the file-lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   321
    private Map<String, String> prefsCache = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * The last modification time of the file backing this node at the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * that prefCache was last synchronized (or initially read).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * value is set *before* reading the file, so it's conservative; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * actual timestamp could be (slightly) higher.  A value of zero indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * that we were unable to initialize prefsCache from the disk, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * have not yet attempted to do so.  (If prefsCache is non-null, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * indicates the former; if it's null, the latter.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private long lastSyncTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    * Unix error code for locked file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private static final int EAGAIN = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    * Unix error code for denied access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private static final int EACCES = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /* Used to interpret results of native functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static final int LOCK_HANDLE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    private static final int ERROR_CODE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * A list of all uncommitted preference changes.  The elements in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * list are of type PrefChange.  If this node is concurrently modified on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * disk by another VM, the two sets of changes are merged when this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * is sync'ed by overwriting our prefsCache with the preference map last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * written out to disk (by the other VM), and then replaying this change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * log against that map.  The resulting map is then written back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * to the disk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   357
    final List<Change> changeLog = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Represents a change to a preference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private abstract class Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * Reapplies the change to prefsCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        abstract void replay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Represents a preference put.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private class Put extends Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        String key, value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Put(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        void replay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            prefsCache.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Represents a preference remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private class Remove extends Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        Remove(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        void replay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            prefsCache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Represents the creation of this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private class NodeCreate extends Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * Performs no action, but the presence of this object in changeLog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * will force the node and its ancestors to be made permanent at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * next sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        void replay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * NodeCreate object for this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    NodeCreate nodeCreate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Replay changeLog against prefsCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private void replayChanges() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        for (int i = 0, n = changeLog.size(); i<n; i++)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   423
            changeLog.get(i).replay();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private static Timer syncTimer = new Timer(true); // Daemon Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // Add periodic timer task to periodically sync cached prefs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        syncTimer.schedule(new TimerTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                syncWorld();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }, SYNC_INTERVAL*1000, SYNC_INTERVAL*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // Add shutdown hook to flush cached prefs on normal termination
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   437
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   438
            public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                Runtime.getRuntime().addShutdownHook(new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        syncTimer.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        syncWorld();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private static void syncWorld() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * Synchronization necessary because userRoot and systemRoot are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         * lazily initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        Preferences userRt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        Preferences systemRt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        synchronized(FileSystemPreferences.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            userRt   = userRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            systemRt = systemRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (userRt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                userRt.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        } catch(BackingStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            getLogger().warning("Couldn't flush user prefs: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (systemRt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                systemRt.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } catch(BackingStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            getLogger().warning("Couldn't flush system prefs: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    private final boolean isUserNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Special constructor for roots (both user and system).  This constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * will only be called twice, by the static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    private FileSystemPreferences(boolean user) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        super(null, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        isUserNode = user;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        dir = (user ? userRootDir: systemRootDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        prefsFile = new File(dir, "prefs.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        tmpFile   = new File(dir, "prefs.tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Construct a new FileSystemPreferences instance with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * parent node and name.  This constructor, called from childSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * is used to make every node except for the two //roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private FileSystemPreferences(FileSystemPreferences parent, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        super(parent, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        isUserNode = parent.isUserNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        dir  = new File(parent.dir, dirName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        prefsFile = new File(dir, "prefs.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        tmpFile  = new File(dir, "prefs.tmp");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   502
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   503
            public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                newNode = !dir.exists();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (newNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            // These 2 things guarantee node will get wrtten at next flush/sync
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   510
            prefsCache = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            nodeCreate = new NodeCreate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            changeLog.add(nodeCreate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public boolean isUserNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return isUserNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    protected void putSpi(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        initCacheIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        changeLog.add(new Put(key, value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        prefsCache.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    protected String getSpi(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        initCacheIfNecessary();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   528
        return prefsCache.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    protected void removeSpi(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        initCacheIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        changeLog.add(new Remove(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        prefsCache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Initialize prefsCache if it has yet to be initialized.  When this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * returns, prefsCache will be non-null.  If the data was successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * read from the file, lastSyncTime will be updated.  If prefsCache was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * null, but it was impossible to read the file (because it didn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * exist or for any other reason) prefsCache will be initialized to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * empty, modifiable Map, and lastSyncTime remain zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    private void initCacheIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (prefsCache != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            loadCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            // assert lastSyncTime == 0;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   553
            prefsCache = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Attempt to load prefsCache from the backing store.  If the attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * succeeds, lastSyncTime will be updated (the new value will typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * correspond to the data loaded into the map, but it may be less,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * if another VM is updating this node concurrently).  If the attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * fails, a BackingStoreException is thrown and both prefsCache and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * lastSyncTime are unaffected by the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    private void loadCache() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   567
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   568
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   569
                public Void run() throws BackingStoreException {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   570
                    Map<String, String> m = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    long newLastSyncTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                        newLastSyncTime = prefsFile.lastModified();
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   574
                        try (FileInputStream fis = new FileInputStream(prefsFile)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   575
                            XmlSupport.importMap(fis, m);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   576
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        if (e instanceof InvalidPreferencesFormatException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            getLogger().warning("Invalid preferences format in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                                        +  prefsFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            prefsFile.renameTo( new File(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                                    prefsFile.getParentFile(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                                  "IncorrectFormatPrefs.xml"));
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   584
                            m = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        } else if (e instanceof FileNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        getLogger().warning("Prefs file removed in background "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                           + prefsFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                            throw new BackingStoreException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    // Attempt succeeded; update state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    prefsCache = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    lastSyncTime = newLastSyncTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Attempt to write back prefsCache to the backing store.  If the attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * succeeds, lastSyncTime will be updated (the new value will correspond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * exactly to the data thust written back, as we hold the file lock, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * prevents a concurrent write.  If the attempt fails, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * BackingStoreException is thrown and both the backing store (prefsFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * and lastSyncTime will be unaffected by this call.  This call will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * NEVER leave prefsFile in a corrupt state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    private void writeBackCache() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   614
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   615
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   616
                public Void run() throws BackingStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                        if (!dir.exists() && !dir.mkdirs())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                            throw new BackingStoreException(dir +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                                             " create failed.");
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   621
                        try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   622
                            XmlSupport.exportMap(fos, prefsCache);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   623
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        if (!tmpFile.renameTo(prefsFile))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                            throw new BackingStoreException("Can't rename " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                            tmpFile + " to " + prefsFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        if (e instanceof BackingStoreException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                            throw (BackingStoreException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        throw new BackingStoreException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    protected String[] keysSpi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        initCacheIfNecessary();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   642
        return prefsCache.keySet().toArray(new String[prefsCache.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    protected String[] childrenNamesSpi() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   646
        return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   647
            new PrivilegedAction<String[]>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   648
                public String[] run() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   649
                    List<String> result = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    File[] dirContents = dir.listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    if (dirContents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        for (int i = 0; i < dirContents.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                            if (dirContents[i].isDirectory())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                result.add(nodeName(dirContents[i].getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    return result.toArray(EMPTY_STRING_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    protected AbstractPreferences childSpi(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return new FileSystemPreferences(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public void removeNode() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        synchronized (isUserNode()? userLockFile: systemLockFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            // to remove a node we need an exclusive lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            if (!lockFile(false))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                throw(new BackingStoreException("Couldn't get file lock."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                super.removeNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
           } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                unlockFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Called with file lock held (in addition to node locks).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    protected void removeNodeSpi() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   685
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   686
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   687
                public Void run() throws BackingStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    if (changeLog.contains(nodeCreate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        changeLog.remove(nodeCreate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        nodeCreate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    if (!dir.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    prefsFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    tmpFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    // dir should be empty now.  If it's not, empty it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    File[] junk = dir.listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    if (junk.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        getLogger().warning(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                           "Found extraneous files when removing node: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                            + Arrays.asList(junk));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        for (int i=0; i<junk.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                            junk[i].delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    if (!dir.delete())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        throw new BackingStoreException("Couldn't delete dir: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                                                                         + dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    public synchronized void sync() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        boolean userNode = isUserNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        boolean shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (userNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            shared = false; /* use exclusive lock for user prefs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            /* if can write to system root, use exclusive lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
               otherwise use shared lock. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            shared = !isSystemRootWritable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        synchronized (isUserNode()? userLockFile:systemLockFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
           if (!lockFile(shared))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
               throw(new BackingStoreException("Couldn't get file lock."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
           final Long newModTime =
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   732
                AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   733
                    new PrivilegedAction<Long>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   734
               public Long run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                   long nmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                   if (isUserNode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                       nmt = userRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                       isUserRootModified = userRootModTime == nmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                       nmt = systemRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                       isSystemRootModified = systemRootModTime == nmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                   return new Long(nmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
           });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
               super.sync();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   748
               AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   749
                   public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                   if (isUserNode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                       userRootModTime = newModTime.longValue() + 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                       userRootModFile.setLastModified(userRootModTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                       systemRootModTime = newModTime.longValue() + 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                       systemRootModFile.setLastModified(systemRootModTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                   return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
               });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
           } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                unlockFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    protected void syncSpi() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   768
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   769
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   770
                public Void run() throws BackingStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    syncSpiPrivileged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    private void syncSpiPrivileged() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (isRemoved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            throw new IllegalStateException("Node has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (prefsCache == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            return;  // We've never been used, don't bother syncing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        long lastModifiedTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if ((isUserNode() ? isUserRootModified : isSystemRootModified)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            lastModifiedTime = prefsFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            if (lastModifiedTime  != lastSyncTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                // Prefs at this node were externally modified; read in node and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                // playback any local mods since last sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                loadCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                replayChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                lastSyncTime = lastModifiedTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        } else if (lastSyncTime != 0 && !dir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            // This node was removed in the background.  Playback any changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            // against a virgin (empty) Map.
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   797
            prefsCache = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            replayChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (!changeLog.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            writeBackCache();  // Creates directory & file if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
           /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            * Attempt succeeded; it's barely possible that the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            * lastModified might fail (i.e., return 0), but this would not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            * be a disaster, as lastSyncTime is allowed to lag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            lastModifiedTime = prefsFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            /* If lastSyncTime did not change, or went back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
             * increment by 1 second. Since we hold the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
             * lastSyncTime always monotonically encreases in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
             * atomic sense.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            if (lastSyncTime <= lastModifiedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                lastSyncTime = lastModifiedTime + 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                prefsFile.setLastModified(lastSyncTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            changeLog.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    public void flush() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (isRemoved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    protected void flushSpi() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Returns true if the specified character is appropriate for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Unix directory names.  A character is appropriate if it's a printable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * ASCII character (> 0x1f && < 0x7f) and unequal to slash ('/', 0x2f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * dot ('.', 0x2e), or underscore ('_', 0x5f).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    private static boolean isDirChar(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        return ch > 0x1f && ch < 0x7f && ch != '/' && ch != '.' && ch != '_';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Returns the directory name corresponding to the specified node name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Generally, this is just the node name.  If the node name includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * inappropriate characters (as per isDirChar) it is translated to Base64.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * with the underscore  character ('_', 0x5f) prepended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private static String dirName(String nodeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        for (int i=0, n=nodeName.length(); i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (!isDirChar(nodeName.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                return "_" + Base64.byteArrayToAltBase64(byteArray(nodeName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        return nodeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Translate a string into a byte array by translating each character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * into two bytes, high-byte first ("big-endian").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    private static byte[] byteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        byte[] result = new byte[2*len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        for (int i=0, j=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            result[j++] = (byte) (c>>8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            result[j++] = (byte) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Returns the node name corresponding to the specified directory name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
 * (Inverts the transformation of dirName(String).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    private static String nodeName(String dirName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (dirName.charAt(0) != '_')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            return dirName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        byte a[] = Base64.altBase64ToByteArray(dirName.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        StringBuffer result = new StringBuffer(a.length/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        for (int i = 0; i < a.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            int highByte = a[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            int lowByte =  a[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            result.append((char) ((highByte << 8) | lowByte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * Try to acquire the appropriate file lock (user or system).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * the initial attempt fails, several more attempts are made using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * an exponential backoff strategy.  If all attempts fail, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @throws SecurityException if file access denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    private boolean lockFile(boolean shared) throws SecurityException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        boolean usernode = isUserNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        int[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        int errorCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        File lockFile = (usernode ? userLockFile : systemLockFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        long sleepTime = INIT_SLEEP_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        for (int i = 0; i < MAX_ATTEMPTS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                  int perm = (usernode? USER_READ_WRITE: USER_RW_ALL_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                  result = lockFile0(lockFile.getCanonicalPath(), perm, shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                  errorCode = result[ERROR_CODE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                  if (result[LOCK_HANDLE] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                     if (usernode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                         userRootLockHandle = result[LOCK_HANDLE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                     } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                         systemRootLockHandle = result[LOCK_HANDLE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
//                // If at first, you don't succeed...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                Thread.sleep(sleepTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            } catch(InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                checkLockFile0ErrorCode(errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            sleepTime *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        checkLockFile0ErrorCode(errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Checks if unlockFile0() returned an error. Throws a SecurityException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * if access denied. Logs a warning otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    private void checkLockFile0ErrorCode (int errorCode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                                      throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        if (errorCode == EACCES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            throw new SecurityException("Could not lock " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            (isUserNode()? "User prefs." : "System prefs.") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
             " Lock file access denied.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        if (errorCode != EAGAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            getLogger().warning("Could not lock " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                             (isUserNode()? "User prefs. " : "System prefs.") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                             " Unix error code " + errorCode + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * Locks file using UNIX file locking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @param fileName Absolute file name of the lock file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @return Returns a lock handle, used to unlock the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    private static native int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            lockFile0(String fileName, int permission, boolean shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * Unlocks file previously locked by lockFile0().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @param lockHandle Handle to the file lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @return Returns zero if OK, UNIX error code if failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    private  static native int unlockFile0(int lockHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Changes UNIX file permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    private static native int chmod(String fileName, int permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Initial time between lock attempts, in ms.  The time is doubled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * after each failing attempt (except the first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    private static int INIT_SLEEP_TIME = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * Maximum number of lock attempts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    private static int MAX_ATTEMPTS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Release the the appropriate file lock (user or system).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @throws SecurityException if file access denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    private void unlockFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        boolean usernode = isUserNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        File lockFile = (usernode ? userLockFile : systemLockFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        int lockHandle = ( usernode ? userRootLockHandle:systemRootLockHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        if (lockHandle == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            getLogger().warning("Unlock: zero lockHandle for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                           (usernode ? "user":"system") + " preferences.)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        result = unlockFile0(lockHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            getLogger().warning("Could not drop file-lock on " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            (isUserNode() ? "user" : "system") + " preferences." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            " Unix error code " + result + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            if (result == EACCES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                throw new SecurityException("Could not unlock" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                (isUserNode()? "User prefs." : "System prefs.") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                " Lock file access denied.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        if (isUserNode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            userRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            systemRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
}