~ubuntu-branches/ubuntu/natty/icedtea-web/natty-security

« back to all changes in this revision

Viewing changes to netx/net/sourceforge/jnlp/util/FileUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-02-24 12:57:25 UTC
  • mto: (18.1.1 experimental) (19.1.1 natty-security)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20110224125725-8zq5v35r6o27w8ku
Tags: upstream-1.1~20110320
ImportĀ upstreamĀ versionĀ 1.1~20110320

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
    }
97
97
 
98
98
    /**
 
99
     * Tries to create the ancestor directories of file f. Throws
 
100
     * an IOException if it can't be created (but not if it was
 
101
     * already there).
 
102
     * @param f
 
103
     * @param eMsg - the message to use for the exception. null
 
104
     * if the file name is to be used.
 
105
     * @throws IOException if the directory can't be created and doesn't exist.
 
106
     */
 
107
    public static void createParentDir(File f, String eMsg) throws IOException {
 
108
        File parent = f.getParentFile();
 
109
        if (!parent.isDirectory() && !parent.mkdirs()) {
 
110
            throw new IOException(R("RCantCreateDir",
 
111
                    eMsg == null ? parent : eMsg));
 
112
        }
 
113
    }
 
114
 
 
115
    /**
 
116
     * Tries to create the ancestor directories of file f. Throws
 
117
     * an IOException if it can't be created (but not if it was
 
118
     * already there).
 
119
     * @param f
 
120
     * @throws IOException if the directory can't be created and doesn't exist.
 
121
     */
 
122
    public static void createParentDir(File f) throws IOException {
 
123
        createParentDir(f, null);
 
124
    }
 
125
 
 
126
    /**
 
127
     * Tries to delete file f. If the file exists but couldn't be deleted,
 
128
     * print an error message to stderr with the file name, or eMsg if eMsg
 
129
     * is not null.
 
130
     * @param f the file to be deleted
 
131
     * @param eMsg the message to print on failure (or null to print the
 
132
     * the file name).
 
133
     */
 
134
    public static void deleteWithErrMesg(File f, String eMsg) {
 
135
        if (f.exists()) {
 
136
            if (!f.delete()) {
 
137
                System.err.println(R("RCantDeleteFile", eMsg == null ? f : eMsg));
 
138
            }
 
139
        }
 
140
    }
 
141
 
 
142
    /**
 
143
     * Tries to delete file f. If the file exists but couldn't be deleted,
 
144
     * print an error message to stderr with the file name.
 
145
     * @param f the file to be deleted
 
146
     */
 
147
    public static void deleteWithErrMesg(File f) {
 
148
        deleteWithErrMesg(f, null);
 
149
    }
 
150
 
 
151
    /**
99
152
     * Creates a new file or directory with minimum permissions. The file is not
100
153
     * readable or writable by anyone other than the owner. If writeableByOnwer
101
154
     * is false, even the owner can not write to it. If isDir is true, then the
120
173
        }
121
174
 
122
175
        // remove all permissions
123
 
        tempFile.setExecutable(false, false);
124
 
        tempFile.setReadable(false, false);
125
 
        tempFile.setWritable(false, false);
 
176
        if (!tempFile.setExecutable(false, false)) {
 
177
            throw new IOException(R("RRemoveXPermFailed", tempFile));
 
178
        }
 
179
        if (!tempFile.setReadable(false, false)) {
 
180
            throw new IOException(R("RRemoveRPermFailed", tempFile));
 
181
        }
 
182
        if (!tempFile.setWritable(false, false)) {
 
183
            throw new IOException(R("RRemoveWPermFailed", tempFile));
 
184
        }
126
185
 
127
186
        // allow owner to read
128
 
        tempFile.setReadable(true, true);
 
187
        if (!tempFile.setReadable(true, true)) {
 
188
            throw new IOException(R("RGetRPermFailed", tempFile));
 
189
        }
129
190
 
130
191
        // allow owner to write
131
 
        if (writableByOwner) {
132
 
            tempFile.setWritable(true, true);
 
192
        if (writableByOwner && !tempFile.setWritable(true, true)) {
 
193
            throw new IOException(R("RGetWPermFailed", tempFile));
133
194
        }
134
195
 
135
196
        // allow owner to enter directories
136
 
        if (isDir) {
137
 
            tempFile.setExecutable(true, true);
 
197
        if (isDir && !tempFile.setExecutable(true, true)) {
 
198
            throw new IOException(R("RGetXPermFailed", tempFile));
138
199
        }
139
200
 
140
201
        // rename this file. Unless the file is moved/renamed, any program that