filemanager.java

### File Source: filemanager.java

             /*
        P/P   *  Method: net.sourceforge.pebble.domain.FileManager__static_init
              */
         1  /*
         2   * Copyright (c) 2003-2006, Simon Brown
         3   * All rights reserved.
         4   *
         5   * Redistribution and use in source and binary forms, with or without
         6   * modification, are permitted provided that the following conditions are met:
         7   *
         8   *   - Redistributions of source code must retain the above copyright
         9   *     notice, this list of conditions and the following disclaimer.
        10   *
        11   *   - Redistributions in binary form must reproduce the above copyright
        12   *     notice, this list of conditions and the following disclaimer in
        13   *     the documentation and/or other materials provided with the
        14   *     distribution.
        15   *
        16   *   - Neither the name of Pebble nor the names of its contributors may
        17   *     be used to endorse or promote products derived from this software
        18   *     without specific prior written permission.
        19   *
        20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
        24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        30   * POSSIBILITY OF SUCH DAMAGE.
        31   */
        32  package net.sourceforge.pebble.domain;
        33
        34  import net.sourceforge.pebble.comparator.FileMetaDataComparator;
        35  import net.sourceforge.pebble.util.FileUtils;
        36  import net.sourceforge.pebble.PebbleContext;
        37
        38  import java.io.*;
        39  import java.util.*;
        40
        41  /**
        42   * Encapsulates methods for managing and manipulating files under the
        43   * following locations:
        44   * <ul>
        45   * <li>${blog.dir}/images</li>
        46   * <li>${blog.dir}/files</li>
        47   * <li>${editableTheme}</li>
        48   * </ul>
        49   *
        50   * @author Simon Brown
        51   */
        52  public class FileManager {
        53
        54    /** the type of files being managed */
        55    private String type;
        56
        57    /** the root directory for the particular file type */
        58    private File root;
        59
        60    /**
        61     * Creates a new instande for the specified blog and type.
        62     *
        63     * @param blog    the blog that this manager refers to
        64     * @param type    the type of files to manage
        65     */
               /*
        P/P     *  Method: void net.sourceforge.pebble.domain.FileManager(Blog, String)
                *
                *  Preconditions:
                *    blog != null
                *    type != null
                *
                *  Postconditions:
                *    this.root in Addr_Set{&new File(FileManager#3),&new File(FileManager#2),&new File(getPathToLiveTheme#1),&new File(FileManager#1)}
                *    this.type == type
                *    this.type != null
                *    new File(FileManager#1) num objects <= 1
                *    new File(FileManager#2) num objects <= 1
                *    new File(FileManager#3) num objects <= 1
                *    new File(getPathToLiveTheme#1) num objects <= 1
                *
                *  Test Vectors:
                *    java.lang.String:equals(...)@70: {0}, {1}
                *    java.lang.String:equals(...)@72: {0}, {1}
                *    java.lang.String:equals(...)@74: {0}, {1}
                *
                *  Preconditions:
                *    (soft) blog.editableTheme != null
                *    (soft) init'ed(blog.editableTheme.name)
                *    (soft) init'ed(blog.editableTheme.pathToLiveThemes)
                *    (soft) init'ed(blog.root)
                */
        66    public FileManager(Blog blog, String type) {
        67      this.type = type;
        68
        69      // which directory are we looking at?
        70      if (type.equals(FileMetaData.BLOG_IMAGE)) {
        71        this.root = new File(blog.getImagesDirectory());
        72      } else if (type.equals(FileMetaData.THEME_FILE)) {
        73        this.root = blog.getEditableTheme().getPathToLiveTheme();
        74      } else if (type.equals(FileMetaData.BLOG_DATA)) {
        75        this.root = new File(blog.getRoot());
        76      } else {
        77        this.root = new File(blog.getFilesDirectory());
        78      }
        79    }
        80
        81    /**
        82     * Gets the root directory that this class is managing.
        83     *
        84     * @return  a File instance
        85     */
        86    public File getRootDirectory() {
                 /*
        P/P       *  Method: File getRootDirectory()
                  *
                  *  Preconditions:
                  *    init'ed(this.root)
                  *
                  *  Postconditions:
                  *    return_value == this.root
                  *    init'ed(return_value)
                  */
        87      return this.root;
        88    }
        89
        90    /**
        91     * Gets meta data about a specific file or directory.
        92     *
        93     * @param path    the path of the file/directory
        94     * @return        a FileMetaData object
        95     */
        96    public FileMetaData getFileMetaData(String path) {
                 /*
        P/P       *  Method: FileMetaData getFileMetaData(String)
                  *
                  *  Preconditions:
                  *    init'ed(this.type)
                  *    (soft) init'ed(this.root)
                  *
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@110 != null
                  *
                  *  Postconditions:
                  *    return_value == &new FileMetaData(getFileMetaData#1)
                  *    new Date(getFileMetaData#2) num objects <= 1
                  *    new FileMetaData(getFileMetaData#1) num objects == 1
                  *    return_value.context == this
                  *    return_value.context != null
                  *    init'ed(return_value.directory)
                  *    init'ed(return_value.lastModified)
                  *    return_value.name != null
                  *    return_value.path != null
                  *    init'ed(return_value.size)
                  *    ...
                  *
                  *  Test Vectors:
                  *    java.io.File:exists(...)@102: {0}, {1}
                  *    java.io.File:isDirectory(...)@103: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@109: {1}, {0}
                  */
        97      FileMetaData metaData = new FileMetaData(this, path);
        98      metaData.setType(type);
        99
       100      // is it a directory?
       101      File file = getFile(metaData);
       102      if (file.exists()) {
       103        if (file.isDirectory()) {
       104          metaData.setDirectory(true);
       105          try {
       106            List files = getFiles(metaData, true);
       107            long size = 0;
       108            Iterator it = files.iterator();
       109            while (it.hasNext()) {
       110              size += ((FileMetaData)it.next()).getSize();
       111            }
       112            metaData.setSize(size);
       113          } catch (IllegalFileAccessException ifae) {
       114            // do nothing
       115          }
       116        } else {
       117          metaData.setSize(file.length());
       118        }
       119
       120        metaData.setLastModified(new Date(file.lastModified()));
       121      }
       122
       123      return metaData;
       124    }
       125
       126    public FileMetaData getParent(FileMetaData file) {
                 /*
        P/P       *  Method: FileMetaData getParent(FileMetaData)
                  *
                  *  Preconditions:
                  *    file != null
                  *    init'ed(file.name)
                  *    file.path != null
                  *    (soft) init'ed(this.root)
                  *    (soft) init'ed(this.type)
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{null,&new FileMetaData(getFileMetaData#1)}
                  *    new Date(getFileMetaData#2) num objects <= 1
                  *    new FileMetaData(getFileMetaData#1) num objects <= 1
                  *    new FileMetaData(getFileMetaData#1).context == this
                  *    new FileMetaData(getFileMetaData#1).context != null
                  *    init'ed(new FileMetaData(getFileMetaData#1).directory)
                  *    init'ed(new FileMetaData(getFileMetaData#1).lastModified)
                  *    new FileMetaData(getFileMetaData#1).name != null
                  *    new FileMetaData(getFileMetaData#1).path != null
                  *    init'ed(new FileMetaData(getFileMetaData#1).size)
                  *    ...
                  *
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@127: {0}, {1}
                  */
       127      if (file.getAbsolutePath().equals("/")) {
       128        return null;
       129      } else {
       130        return getFileMetaData(file.getPath());
       131      }
       132    }
       133
       134    /**
       135     * Gets meta data about a specific file or directory.
       136     *
       137     * @param path    the path of the file/directory
       138     * @param name    the name of the file/directory
       139     * @return        a FileMetaData object
       140     */
       141    public FileMetaData getFileMetaData(String path, String name) {
                 /*
        P/P       *  Method: FileMetaData getFileMetaData(String, String)
                  *
                  *  Preconditions:
                  *    init'ed(this.type)
                  *    (soft) init'ed(this.root)
                  *
                  *  Postconditions:
                  *    return_value == &new FileMetaData(getFileMetaData#1*)
                  *    new Date(getFileMetaData#2*) num objects <= 1
                  *    new FileMetaData(getFileMetaData#1*) num objects == 1
                  *    new FileMetaData(getFileMetaData#1*).context != null
                  *    init'ed(new FileMetaData(getFileMetaData#1*).directory)
                  *    init'ed(new FileMetaData(getFileMetaData#1*).lastModified)
                  *    new FileMetaData(getFileMetaData#1*).name != null
                  *    new FileMetaData(getFileMetaData#1*).path != null
                  *    init'ed(new FileMetaData(getFileMetaData#1*).size)
                  *    init'ed(new FileMetaData(getFileMetaData#1*).type)
                  *
                  *  Test Vectors:
                  *    path: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:endsWith(...)@142: {0}, {1}
                  */
       142      if (path != null && path.endsWith("/")) {
       143        return getFileMetaData(path + name);
       144      } else {
       145        return getFileMetaData(path + "/" + name);
       146      }
       147    }
       148
       149    /**
       150     * Gets a java.io.File reference to the specified path, regardless
       151     * of whether it represents a file or directory.
       152     *
       153     * @param path    an absolute path from the root
       154     * @return    a java.io.File instance
       155     */
       156    public File getFile(String path) {
       157      String relativePath;
                 /*
        P/P       *  Method: File getFile(String)
                  *
                  *  Preconditions:
                  *    init'ed(this.root)
                  *
                  *  Postconditions:
                  *    return_value == &new File(getFile#1)
                  *    new File(getFile#1) num objects == 1
                  *
                  *  Test Vectors:
                  *    path: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:startsWith(...)@158: {0}, {1}
                  */
       158      if (path != null && path.startsWith("/")) {
       159        relativePath = path.substring(1);
       160      } else {
       161        relativePath = path;
       162      }
       163
       164      return new File(root, relativePath);
       165    }
       166
       167    /**
       168     * Convenience method to get a java.io.File reference to the file represented
       169     * by the specified FileMetaData object.
       170     *
       171     * @param file    the FileMetaData object representing the path
       172     * @return    a java.io.File instance
       173     */
       174    private File getFile(FileMetaData file) {
                 /*
        P/P       *  Method: File getFile(FileMetaData)
                  *
                  *  Preconditions:
                  *    file != null
                  *    init'ed(file.name)
                  *    file.path != null
                  *    init'ed(this.root)
                  *
                  *  Postconditions:
                  *    return_value == &new File(getFile#1)
                  *    new File(getFile#1) num objects == 1
                  */
       175      String relativePath = file.getAbsolutePath().substring(1);
       176      return new File(root, relativePath);
       177    }
       178
       179    /**
       180     * Determines whether the specified file is underneath the root directory
       181     * for this file manager.
       182     *
       183     * @param file    the java.io.File to test
       184     * @return    true if the file is underneath the root, false otherwise
       185     */
       186    public boolean isUnderneathRootDirectory(File file) {
                 /*
        P/P       *  Method: bool isUnderneathRootDirectory(File)
                  *
                  *  Preconditions:
                  *    init'ed(this.root)
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       187      return FileUtils.underneathRoot(root, file);
       188    }
       189
       190    /**
       191     * Creates a new directory with the specified name underneath the given path.
       192     *
       193     * @param path    the path under which to create the directory
       194     * @param name    the name of the directory
       195     * @return    a java.io.File instance representing the new directory
       196     */
       197    public File createDirectory(String path, String name) throws IllegalFileAccessException {
                 /*
        P/P       *  Method: File createDirectory(String, String)
                  *
                  *  Preconditions:
                  *    init'ed(this.type)
                  *    (soft) init'ed(this.root)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  *
                  *  Postconditions:
                  *    return_value == &new File(createDirectory#1)
                  *    new File(createDirectory#1) num objects == 1
                  */
       198      FileMetaData subDirectory = getFileMetaData(path);
       199
       200      File newDirectory = new File(getFile(subDirectory), name);
       201      if (!isUnderneathRootDirectory(newDirectory)) {
       202        throw new IllegalFileAccessException();
       203      } else {
       204        newDirectory.mkdirs();
       205      }
       206
       207      return newDirectory;
       208    }
       209
       210    /**
       211     * Copies a file.
       212     *
       213     * @param path      the path under which the file exists
       214     * @param name      the name of the file
       215     * @param newName   the new name of the file
       216     * @return    a java.io.File instance representing the new file, or null
       217     *            if the file isn't copied because no new name was given, or the
       218     *            new name is the same as the old name
       219     */
       220    public File copyFile(String path, String name, String newName) throws IOException, IllegalFileAccessException {
                 /*
        P/P       *  Method: File copyFile(String, String, String)
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.root)
                  *    (soft) init'ed(this.type)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{null,&new File(copyFile#2)}
                  *    new File(copyFile#2) num objects <= 1
                  *
                  *  Test Vectors:
                  *    newName: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@221: {1}, {0}
                  *    java.lang.String:length(...)@221: {0}, {1..232-1}
                  */
       221      if (newName != null && newName.length() > 0 && !newName.equals(name)) {
       222        FileMetaData subDirectory = getFileMetaData(path);
       223        File originalFile = new File(getFile(subDirectory), name);
       224        File newFile = new File(getFile(subDirectory), newName);
       225
       226        if (!isUnderneathRootDirectory(originalFile) || !isUnderneathRootDirectory(newFile)) {
       227          throw new IllegalFileAccessException();
       228        }
       229
       230        FileUtils.copyFile(originalFile, newFile);
       231
       232        return newFile;
       233      } else {
       234        return null;
       235      }
       236    }
       237
       238    /**
       239     * Renames a file.
       240     *
       241     * @param path      the path under which the file exists
       242     * @param name      the name of the file
       243     * @param newName   the new name of the file
       244     * @return    a java.io.File instance representing the new file, or null
       245     *            if the file isn't copied because no new name was given, or the
       246     *            new name is the same as the old name
       247     */
       248    public File renameFile(String path, String name, String newName) throws IllegalFileAccessException {
                 /*
        P/P       *  Method: File renameFile(String, String, String)
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.root)
                  *    (soft) init'ed(this.type)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  *
                  *  Postconditions:
                  *    return_value in Addr_Set{null,&new File(renameFile#2)}
                  *    new File(renameFile#2) num objects <= 1
                  *
                  *  Test Vectors:
                  *    newName: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:equals(...)@249: {1}, {0}
                  *    java.lang.String:length(...)@249: {0}, {1..232-1}
                  */
       249      if (newName != null && newName.length() > 0 && !newName.equals(name)) {
       250        FileMetaData subDirectory = getFileMetaData(path);
       251        File originalFile = new File(getFile(subDirectory), name);
       252        File newFile = new File(getFile(subDirectory), newName);
       253
       254        // check that newFile is underneath the root directory
       255        if (!isUnderneathRootDirectory(originalFile) || !isUnderneathRootDirectory(newFile)) {
       256          throw new IllegalFileAccessException();
       257        }
       258
       259        originalFile.renameTo(newFile);
       260        return newFile;
       261      } else {
       262        return null;
       263      }
       264    }
       265
       266    /**
       267     * Deletes a file.
       268     *
       269     * @param path      the path under which the file exists
       270     * @param name      the name of the file
       271     */
       272    public void deleteFile(String path, String name) throws IllegalFileAccessException {
                 /*
        P/P       *  Method: void deleteFile(String, String)
                  *
                  *  Preconditions:
                  *    init'ed(this.root)
                  *    init'ed(this.type)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  */
       273      FileMetaData subDirectory = getFileMetaData(path);
       274      File fileToDelete = new File(getFile(subDirectory), name);
       275
       276      if (!isUnderneathRootDirectory(fileToDelete)) {
       277        throw new IllegalFileAccessException();
       278      }
       279
       280      FileUtils.deleteFile(fileToDelete);
       281    }
       282
       283    /**
       284     * Loads a file into a String.
       285     *
       286     * @param path      the path under which the file exists
       287     * @param name      the name of the file
       288     * @return  a String containing the content of the specified file
       289     */
       290    public String loadFile(String path, String name) throws IllegalFileAccessException {
                 /*
        P/P       *  Method: String loadFile(String, String)
                  *
                  *  Preconditions:
                  *    init'ed(this.root)
                  *    init'ed(this.type)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  *
                  *  Postconditions:
                  *    return_value != null
                  *
                  *  Test Vectors:
                  *    java.io.BufferedReader:readLine(...)@306: Addr_Set{null}, Inverse{null}
                  */
       291      StringBuffer content = new StringBuffer();
       292
       293      FileMetaData subDirectory = getFileMetaData(path);
       294      File fileToLoad = new File(getFile(subDirectory), name);
       295
       296      if (!isUnderneathRootDirectory(fileToLoad)) {
       297        throw new IllegalFileAccessException();
       298      }
       299
       300      try {
       301        BufferedReader reader = new BufferedReader(new FileReader(fileToLoad));
       302        String line = reader.readLine();
       303        while (line != null) {
       304          content.append(line);
       305
       306          line = reader.readLine();
       307          if (line != null) {
       308            content.append(System.getProperty("line.separator"));
       309          }
       310        }
       311        reader.close();
       312      } catch (IOException ioe) {
       313        ioe.printStackTrace();
       314      }
       315
       316      return content.toString();
       317    }
       318
       319    /**
       320     * Saves a file with the given content.
       321     *
       322     * @param path      the path under which the file exists
       323     * @param name      the name of the file
       324     * @param content   the content as a String
       325     */
       326    public void saveFile(String path, String name, String content) throws IOException, IllegalFileAccessException {
                 /*
        P/P       *  Method: void saveFile(String, String, String)
                  *
                  *  Preconditions:
                  *    init'ed(this.type)
                  *    (soft) init'ed(this.root)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  */
       327      FileMetaData subDirectory = getFileMetaData(path);
       328      File fileToSave = new File(getFile(subDirectory), name);
       329
       330      if (!isUnderneathRootDirectory(fileToSave)) {
       331        throw new IllegalFileAccessException();
       332      }
       333
       334      BufferedWriter writer = new BufferedWriter(new FileWriter(fileToSave));
       335      writer.write(content);
       336      writer.flush();
       337      writer.close();
       338    }
       339
       340    /**
       341     * Saves a file with the given binary content.
       342     *
       343     * @param name      the name of the file
       344     * @param content   the binary content
       345     * @return  a FileMetaData instance representing the saved file
       346     */
       347    public FileMetaData saveFile(String name, byte[] content) throws IOException, IllegalFileAccessException {
                 /*
        P/P       *  Method: FileMetaData saveFile(String, byte[])
                  *
                  *  Preconditions:
                  *    init'ed(this.root)
                  *    init'ed(this.type)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  *
                  *  Postconditions:
                  *    return_value == &new FileMetaData(getFileMetaData#1)
                  *    new Date(getFileMetaData#2) num objects <= 1
                  *    new FileMetaData(getFileMetaData#1) num objects == 1
                  *    return_value.context == this
                  *    return_value.context != null
                  *    init'ed(return_value.directory)
                  *    init'ed(return_value.lastModified)
                  *    return_value.name != null
                  *    return_value.path != null
                  *    init'ed(return_value.size)
                  *    ...
                  */
       348      FileMetaData file = getFileMetaData(name);
       349      File fileToSave = getFile(name);
       350
       351      if (!isUnderneathRootDirectory(fileToSave)) {
       352        throw new IllegalFileAccessException();
       353      }
       354
       355      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileToSave));
       356      out.write(content);
       357      out.flush();
       358      out.close();
       359
       360      return file;
       361    }
       362
       363    /**
       364     * Gets a list of files that reside under a given path.
       365     *
       366     * @param path      the path under which the file exists
       367     * @return  a List of FileMetaData instances
       368     * @throws IllegalFileAccessException   if trying to access a file outside the root
       369     */
       370    public List getFiles(String path) throws IllegalFileAccessException {
                 /*
        P/P       *  Method: List getFiles(String)
                  *
                  *  Preconditions:
                  *    init'ed(this.type)
                  *    (soft) init'ed(this.root)
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getFiles#1*)
                  *    new ArrayList(getFiles#1*) num objects == 1
                  */
       371      return getFiles(path, false);
       372    }
       373
       374    public List getFiles(String path, boolean includeChildren) throws IllegalFileAccessException {
                 /*
        P/P       *  Method: List getFiles(String, bool)
                  *
                  *  Preconditions:
                  *    init'ed(this.type)
                  *    (soft) init'ed(this.root)
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getFiles#1*)
                  *    new ArrayList(getFiles#1*) num objects == 1
                  */
       375      FileMetaData subDirectory = getFileMetaData(path);
       376      return getFiles(subDirectory, includeChildren);
       377    }
       378
       379    private List getFiles(FileMetaData path, boolean includeChildren) throws IllegalFileAccessException {
                 /*
        P/P       *  Method: List getFiles(FileMetaData, bool)
                  *
                  *  Preconditions:
                  *    init'ed(this.root)
                  *    (soft) path != null
                  *    (soft) init'ed(path.name)
                  *    (soft) path.path != null
                  *    (soft) init'ed(this.type)
                  *
                  *  Presumptions:
                  *    f.length@389 <= 232-1
                  *    f[i]@389 != null
                  *    net.sourceforge.pebble.util.FileUtils:underneathRoot(...)@187 == 1
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getFiles#1)
                  *    new ArrayList(getFiles#1) num objects == 1
                  *
                  *  Test Vectors:
                  *    includeChildren: {0}, {1}
                  *    java.io.File:isDirectory(...)@394: {0}, {1}
                  *    java.io.File:isFile(...)@408: {0}, {1}
                  *    java.io.File:listFiles(...)@389: Addr_Set{null}, Inverse{null}
                  */
       380      File directoryToView = getFile(path);
       381
       382      if (!isUnderneathRootDirectory(directoryToView)) {
       383        throw new IllegalFileAccessException();
       384      }
       385
       386      List directoriesAndFiles = new ArrayList();
       387      List files = new ArrayList();
       388      List directories = new ArrayList();
       389      File f[] = directoryToView.listFiles();
       390      if (f != null) {
       391        File file;
       392        for (int i = 0; i < f.length; i++) {
       393          file = f[i];
       394          if (file.isDirectory()) {
       395            FileMetaData metaData = getFileMetaData(path.getAbsolutePath(), file.getName());
       396            directories.add(metaData);
       397
       398            if (includeChildren) {
       399              directories.addAll(getFiles(metaData.getAbsolutePath(), true));
       400            } else {
       401              Collections.sort(directories, new FileMetaDataComparator());
       402            }
       403          }
       404        }
       405
       406        for (int i = 0; i < f.length; i++) {
       407          file = f[i];
       408          if (file.isFile()) {
       409            FileMetaData metaData = getFileMetaData(path.getAbsolutePath(), file.getName());
       410            files.add(metaData);
       411          }
       412        }
       413
       414        Collections.sort(files, new FileMetaDataComparator());
       415      }
       416
       417      directoriesAndFiles.addAll(directories);
       418      directoriesAndFiles.addAll(files);
       419
       420      return directoriesAndFiles;
       421    }
       422
       423    /**
       424     * Determines how much space is being used in files, images and theme.
       425     *
       426     * @param blog    the blog to check against
       427     * @return  the number of KB
       428     */
       429    public static double getCurrentUsage(Blog blog) {
                 /*
        P/P       *  Method: double getCurrentUsage(Blog)
                  *
                  *  Preconditions:
                  *    blog != null
                  *
                  *  Postconditions:
                  *    return_value in (-Inf..+Inf)
                  *
                  *  Preconditions:
                  *    (soft) blog.editableTheme != null
                  *    (soft) init'ed(blog.editableTheme.name)
                  *    (soft) init'ed(blog.editableTheme.pathToLiveThemes)
                  *    (soft) init'ed(blog.root)
                  */
       430      FileManager imagesFileManager = new FileManager(blog, FileMetaData.BLOG_IMAGE);
       431      FileManager filesFileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
       432      FileManager themeFileManager = new FileManager(blog, FileMetaData.THEME_FILE);
       433      return  imagesFileManager.getFileMetaData("/").getSizeInKB() +
       434              filesFileManager.getFileMetaData("/").getSizeInKB() +
       435              themeFileManager.getFileMetaData("/").getSizeInKB();
       436    }
       437
       438
       439    /**
       440     * Determines whether there is enough space to store the given number of KB.
       441     *
       442     * @param blog    the blog to check against
       443     * @param itemSize  the size of the item to be written
       444     * @return  true if there is enough space or quotas aren't active
       445     */
       446    public static boolean hasEnoughSpace(Blog blog, double itemSize) {
                 /*
        P/P       *  Method: bool hasEnoughSpace(Blog, double)
                  *
                  *  Preconditions:
                  *    (soft) blog != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Preconditions:
                  *    (soft) blog.editableTheme != null
                  *    (soft) init'ed(blog.editableTheme.name)
                  *    (soft) init'ed(blog.editableTheme.pathToLiveThemes)
                  *    (soft) init'ed(blog.root)
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.PebbleContext:getConfiguration(...)@447 != null
                  *    net.sourceforge.pebble.PebbleContext:getInstance(...)@447 != null
                  *
                  *  Test Vectors:
                  *    net.sourceforge.pebble.PebbleContext:getConfiguration(...).fileUploadQuota@447: {-1}, {-263..-2, 0..264-1}
                  */
       447      long quota = PebbleContext.getInstance().getConfiguration().getFileUploadQuota();
       448
       449      return (quota == -1) || ((quota - getCurrentUsage(blog)) > itemSize);
       450    }
       451
       452  }

SofCheck Inspector Build Version : 2.22510

filemanager.java | 2010-Jun-25 19:40:32
filemanager.class | 2010-Jul-19 20:23:40