01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.store;
07:
08: import java.sql.SQLException;
09: import java.util.ArrayList;
10:
11: import org.h2.engine.Constants;
12: import org.h2.util.FileUtils;
13:
14: /**
15: * Utility class to list the files of a database.
16: */
17: public class FileLister {
18:
19: public static String getDatabaseNameFromFileName(String fileName) {
20: if (fileName.endsWith(Constants.SUFFIX_DATA_FILE)) {
21: return fileName.substring(0, fileName.length()
22: - Constants.SUFFIX_DATA_FILE.length());
23: }
24: return null;
25: }
26:
27: /**
28: * Get the list of database files.
29: *
30: * @param dir the directory (null for the current directory)
31: * @param db the database name (null for all databases)
32: * @param all if true, files such as the lock, trace, hash index, and lob
33: * files are included. If false, only data, index and log files
34: * are returned
35: * @return the list of files
36: */
37: public static ArrayList getDatabaseFiles(String dir, String db,
38: boolean all) throws SQLException {
39: if (dir == null || dir.equals("")) {
40: dir = ".";
41: }
42: dir = FileUtils.normalize(dir);
43: ArrayList files = new ArrayList();
44: String start = db == null ? null : FileUtils.normalize(dir
45: + "/" + db);
46: String[] list = FileUtils.listFiles(dir);
47: for (int i = 0; list != null && i < list.length; i++) {
48: String f = list[i];
49: boolean ok = false;
50: if (f.endsWith(Constants.SUFFIX_DATA_FILE)) {
51: ok = true;
52: } else if (f.endsWith(Constants.SUFFIX_INDEX_FILE)) {
53: ok = true;
54: } else if (f.endsWith(Constants.SUFFIX_LOG_FILE)) {
55: ok = true;
56: } else if (f.endsWith(Constants.SUFFIX_HASH_FILE)) {
57: ok = true;
58: } else if (f.endsWith(Constants.SUFFIX_LOBS_DIRECTORY)) {
59: files.addAll(getDatabaseFiles(f, null, all));
60: ok = true;
61: } else if (f.endsWith(Constants.SUFFIX_LOB_FILE)) {
62: ok = true;
63: } else if (all) {
64: if (f.endsWith(Constants.SUFFIX_LOCK_FILE)) {
65: ok = true;
66: } else if (f.endsWith(Constants.SUFFIX_TEMP_FILE)) {
67: ok = true;
68: } else if (f.endsWith(Constants.SUFFIX_TRACE_FILE)) {
69: ok = true;
70: }
71: }
72: if (ok) {
73: if (db == null
74: || FileUtils.fileStartsWith(f, start + ".")) {
75: String fileName = f;
76: files.add(fileName);
77: }
78: }
79: }
80: return files;
81: }
82:
83: }
|