File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\FileLister.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store;
8
9 import java.sql.SQLException;
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.h2.constant.ErrorCode;
13 import org.h2.engine.Constants;
14 import org.h2.message.DbException;
15 import org.h2.message.TraceSystem;
16 import org.h2.store.fs.FileUtils;
17 import org.h2.util.New;
18
19 /**
20 * Utility class to list the files of a database.
21 */
22 public class FileLister {
23
24 private FileLister() {
25 // utility class
26 }
27
28 /**
29 * Try to lock the database, and then unlock it. If this worked, the
30 * .lock.db file will be removed.
31 *
32 * @param files the database files to check
33 * @param message the text to include in the error message
34 * @throws SQLException if it failed
35 */
36 public static void tryUnlockDatabase(List<String> files, String message) throws SQLException {
37 for (String fileName : files) {
38 if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) {
39 FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP);
40 try {
41 lock.lock(FileLock.LOCK_FILE);
42 lock.unlock();
43 } catch (DbException e) {
44 throw DbException.get(
45 ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, message).getSQLException();
46 }
47 }
48 }
49 }
50
51 /**
52 * Normalize the directory name.
53 *
54 * @param dir the directory (null for the current directory)
55 * @return the normalized directory name
56 */
57 public static String getDir(String dir) {
58 if (dir == null || dir.equals("")) {
59 return ".";
60 }
61 return FileUtils.toRealPath(dir);
62 }
63
64 /**
65 * Get the list of database files.
66 *
67 * @param dir the directory (must be normalized)
68 * @param db the database name (null for all databases)
69 * @param all if true, files such as the lock, trace, and lob
70 * files are included. If false, only data, index, log,
71 * and lob files are returned
72 * @return the list of files
73 */
74 public static ArrayList<String> getDatabaseFiles(String dir, String db, boolean all) {
75 ArrayList<String> files = New.arrayList();
76 // for Windows, File.getCanonicalPath("...b.") returns just "...b"
77 String start = db == null ? null : (FileUtils.toRealPath(dir + "/" + db) + ".");
78 for (String f : FileUtils.newDirectoryStream(dir)) {
79 boolean ok = false;
80 if (f.endsWith(Constants.SUFFIX_LOBS_DIRECTORY)) {
81 if (start == null || f.startsWith(start)) {
82 files.addAll(getDatabaseFiles(f, null, all));
83 ok = true;
84 }
85 } else if (f.endsWith(Constants.SUFFIX_LOB_FILE)) {
86 ok = true;
87 } else if (f.endsWith(Constants.SUFFIX_PAGE_FILE)) {
88 ok = true;
89 } else if (all) {
90 if (f.endsWith(Constants.SUFFIX_LOCK_FILE)) {
91 ok = true;
92 } else if (f.endsWith(Constants.SUFFIX_TEMP_FILE)) {
93 ok = true;
94 } else if (f.endsWith(Constants.SUFFIX_TRACE_FILE)) {
95 ok = true;
96 }
97 }
98 if (ok) {
99 if (db == null || f.startsWith(start)) {
100 String fileName = f;
101 files.add(fileName);
102 }
103 }
104 }
105 return files;
106 }
107
108 }
109