summaryrefslogtreecommitdiff
path: root/src/main/java/de/waldheinz/fs/fat/FatLfnDirectory.java
blob: 9f4aa9be7055532537222d035bfa6b6826ccc6d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (C) 2003-2009 JNode.org
 *               2009,2010 Matthias Treydte <mt@waldheinz.de>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package de.waldheinz.fs.fat;

import de.waldheinz.fs.AbstractFsObject;
import de.waldheinz.fs.FsDirectory;
import de.waldheinz.fs.FsDirectoryEntry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * The {@link FsDirectory} implementation for FAT file systems. This
 * implementation aims to fully comply to the FAT specification, including
 * the quite complex naming system regarding the long file names (LFNs) and
 * their corresponding 8+3 short file names. This also means that an
 * {@code FatLfnDirectory} is case-preserving but <em>not</em> case-sensitive.
 * 
 * @author gbin
 * @author Matthias Treydte &lt;waldheinz at gmail.com&gt;
 * @since 0.6
 */
public final class FatLfnDirectory
        extends AbstractFsObject
        implements FsDirectory {
    
    /**
     * This set is used to check if a file name is already in use in this
     * directory. The FAT specification says that file names must be unique
     * ignoring the case, so this set contains all names converted to
     * lower-case, and all checks must be performed using lower-case strings.
     */
    private final Set<String> usedNames;
    private final Fat fat;
    private final Map<ShortName, FatLfnDirectoryEntry> shortNameIndex;
    private final Map<String, FatLfnDirectoryEntry> longNameIndex;
    private final Map<FatDirectoryEntry, FatFile> entryToFile;
    private final Map<FatDirectoryEntry, FatLfnDirectory> entryToDirectory;
    private Dummy83BufferGenerator dbg;
    
    final AbstractDirectory dir;
    
    FatLfnDirectory(AbstractDirectory dir, Fat fat, boolean readOnly)
            throws IOException {
        
        super(readOnly);
        
        if ((dir == null) || (fat == null)) throw new NullPointerException();
        
        this.fat = fat;
        this.dir = dir;
        
        this.shortNameIndex =
                new LinkedHashMap<ShortName, FatLfnDirectoryEntry>();
                
        this.longNameIndex =
                new LinkedHashMap<String, FatLfnDirectoryEntry>();
                
        this.entryToFile =
                new LinkedHashMap<FatDirectoryEntry, FatFile>();
                
        this.entryToDirectory =
                new LinkedHashMap<FatDirectoryEntry, FatLfnDirectory>();
                
        this.usedNames = new HashSet<String>();
        this.dbg = new Dummy83BufferGenerator();
        
        parseLfn();
    }
    
    FatFile getFile(FatDirectoryEntry entry) throws IOException {
        FatFile file = entryToFile.get(entry);

        if (file == null) {
            file = FatFile.get(fat, entry);
            entryToFile.put(entry, file);
        }
        
        return file;
    }
    
    FatLfnDirectory getDirectory(FatDirectoryEntry entry) throws IOException {
        FatLfnDirectory result = entryToDirectory.get(entry);

        if (result == null) {
            final ClusterChainDirectory storage = read(entry, fat);
            result = new FatLfnDirectory(storage, fat, isReadOnly());
            entryToDirectory.put(entry, result);
        }
        
        return result;
    }
    
    /**
     * <p>
     * {@inheritDoc}
     * </p><p>
     * According to the FAT file system specification, leading and trailing
     * spaces in the {@code name} are ignored by this method.
     * </p>
     * 
     * @param name {@inheritDoc}
     * @return {@inheritDoc}
     * @throws IOException {@inheritDoc}
     */
    @Override
    public FatLfnDirectoryEntry addFile(String name) throws IOException {
        checkWritable();
        checkUniqueName(name);
        
        name = name.trim();
        final ShortName sn = makeShortName(name, false);
        
        final FatLfnDirectoryEntry entry =
                new FatLfnDirectoryEntry(name, sn, this, false);

        dir.addEntries(entry.compactForm());
        
        shortNameIndex.put(sn, entry);
        longNameIndex.put(name.toLowerCase(), entry);

        getFile(entry.realEntry);
        
        dir.setDirty();
        return entry;
    }
    
    boolean isFreeName(String name) {
        return true;
    }
    
    private void checkUniqueName(String name) throws IOException {
    }
    
    private void freeUniqueName(String name) {
    }
    
    private ShortName makeShortName(String name, boolean isDirectory) throws IOException {
        final ShortName result;

        try {
            result = dbg.generate83BufferNew(name);
        } catch (IllegalArgumentException ex) {
            throw new IOException(
                    "could not generate short name for \"" + name + "\"", ex);
        }        
        return result;
    }
    
    /**
     * <p>
     * {@inheritDoc}
     * </p><p>
     * According to the FAT file system specification, leading and trailing
     * spaces in the {@code name} are ignored by this method.
     * </p>
     *
     * @param name {@inheritDoc}
     * @return {@inheritDoc}
     * @throws IOException {@inheritDoc}
     */
    @Override
    public FatLfnDirectoryEntry addDirectory(String name) throws IOException {
        checkWritable();
        checkUniqueName(name);
        
        name = name.trim();
        final ShortName sn = makeShortName(name, true);
        final FatDirectoryEntry real = dir.createSub(fat);
        real.setShortName(sn);
        final FatLfnDirectoryEntry e =
                new FatLfnDirectoryEntry(this, real, name);
        
        try {
            dir.addEntries(e.compactForm());
        } catch (IOException ex) {
            final ClusterChain cc =
                    new ClusterChain(fat, real.getStartCluster(), false);
            cc.setChainLength(0);
            dir.removeEntry(real);
            throw ex;
        }
        
        shortNameIndex.put(sn, e);
        longNameIndex.put(name.toLowerCase(), e);

        getDirectory(real);
        
        flush();
        return e;
    }
    
    /**
     * <p>
     * {@inheritDoc}
     * </p><p>
     * According to the FAT file system specification, leading and trailing
     * spaces in the {@code name} are ignored by this method.
     * </p>
     *
     * @param name {@inheritDoc}
     * @return {@inheritDoc}
     */
    @Override
    public FatLfnDirectoryEntry getEntry(String name) {
        name = name.trim().toLowerCase();
        
        final FatLfnDirectoryEntry entry = longNameIndex.get(name);
        
        if (entry == null) {
            if (!ShortName.canConvert(name)) return null;
            return shortNameIndex.get(ShortName.get(name));
        } else {
            return entry;
        }
    }
    
    private void parseLfn() throws IOException {
        int i = 0;
        final int size = dir.getEntryCount();
        
        while (i < size) {
            // jump over empty entries
            while (i < size && dir.getEntry(i) == null) {
                i++;
            }

            if (i >= size) {
                break;
            }

            int offset = i; // beginning of the entry
            // check when we reach a real entry
            while (dir.getEntry(i).isLfnEntry()) {
                i++;
                if (i >= size) {
                    // This is a cutted entry, forgive it
                    break;
                }
            }
            
            if (i >= size) {
                // This is a cutted entry, forgive it
                break;
            }
            
            final FatLfnDirectoryEntry current =
                    FatLfnDirectoryEntry.extract(this, offset, ++i - offset);
            
            if (!current.realEntry.isDeleted() && current.isValid()) {
                checkUniqueName(current.getName());
                
                shortNameIndex.put(current.realEntry.getShortName(), current);
                longNameIndex.put(current.getName().toLowerCase(), current);
            }
        }
    }
    
    private void updateLFN() throws IOException {
        ArrayList<FatDirectoryEntry> dest =
                new ArrayList<FatDirectoryEntry>();

        for (FatLfnDirectoryEntry currentEntry : shortNameIndex.values()) {
            FatDirectoryEntry[] encoded = currentEntry.compactForm();
            dest.addAll(Arrays.asList(encoded));
        }
        
        final int size = dest.size();

        dir.changeSize(size);
        dir.setEntries(dest);
    }

    @Override
    public void flush() throws IOException {
        checkWritable();
        
        for (FatFile f : entryToFile.values()) {
            f.flush();
        }
        
        for (FatLfnDirectory d : entryToDirectory.values()) {
            d.flush();
        }
        
        updateLFN();
        dir.flush();
    }

    @Override
    public Iterator<FsDirectoryEntry> iterator() {
        return new Iterator<FsDirectoryEntry>() {

            final Iterator<FatLfnDirectoryEntry> it =
                    shortNameIndex.values().iterator();

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public FsDirectoryEntry next() {
                return it.next();
            }

            /**
             * @see java.util.Iterator#remove()
             */
            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    /**
     * Remove the entry with the given name from this directory.
     * 
     * @param name the name of the entry to remove
     * @throws IOException on error removing the entry
     * @throws IllegalArgumentException on an attempt to remove the dot entries
     */
    @Override
    public void remove(String name)
            throws IOException, IllegalArgumentException {
        
        checkWritable();
        
        final FatLfnDirectoryEntry entry = getEntry(name);
        if (entry == null) return;
        
        unlinkEntry(entry);
        
        final ClusterChain cc = new ClusterChain(
                fat, entry.realEntry.getStartCluster(), false);

        cc.setChainLength(0);
        
        freeUniqueName(name);
        updateLFN();
    }
    
    /**
     * Unlinks the specified entry from this directory without actually
     * deleting it.
     *
     * @param e the entry to be unlinked
     * @see #linkEntry(de.waldheinz.fs.fat.FatLfnDirectoryEntry) 
     */
    void unlinkEntry(FatLfnDirectoryEntry entry) {
        final ShortName sn = entry.realEntry.getShortName();
        
        if (sn.equals(ShortName.DOT) || sn.equals(ShortName.DOT_DOT)) throw
                new IllegalArgumentException(
                    "the dot entries can not be removed");

        final String lowerName = entry.getName().toLowerCase();

        assert (this.longNameIndex.containsKey(lowerName));
        this.longNameIndex.remove(lowerName);
        
        assert (this.shortNameIndex.containsKey(sn));
        this.shortNameIndex.remove(sn);
        
        if (entry.isFile()) {
            this.entryToFile.remove(entry.realEntry);
        } else {
            this.entryToDirectory.remove(entry.realEntry);
        }
    }
    
    /**
     * Links the specified entry to this directory, updating the entrie's
     * short name.
     *
     * @param entry the entry to be linked (added) to this directory
     * @see #unlinkEntry(de.waldheinz.fs.fat.FatLfnDirectoryEntry) 
     */
    void linkEntry(FatLfnDirectoryEntry entry) throws IOException {
        checkUniqueName(entry.getName());
        ShortName name;
        name = this.dbg.generate83BufferNew(entry.getName());
        entry.realEntry.setShortName(name);
        
        this.longNameIndex.put(entry.getName().toLowerCase(), entry);
        this.shortNameIndex.put(entry.realEntry.getShortName(), entry);
        
        updateLFN();
    }
    
    @Override
    public String toString() {
        return getClass().getSimpleName() +
                " [size=" + shortNameIndex.size() + //NOI18N
                ", dir=" + dir + "]"; //NOI18N
    }
    
    private static ClusterChainDirectory read(FatDirectoryEntry entry, Fat fat)
            throws IOException {

        if (!entry.isDirectory()) throw
                new IllegalArgumentException(entry + " is no directory");

        final ClusterChain chain = new ClusterChain(
                fat, entry.getStartCluster(),
                entry.isReadonlyFlag());

        final ClusterChainDirectory result =
                new ClusterChainDirectory(chain, false);

        result.read();
        return result;
    }
    
}