1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.tools; |
7 | |
8 | import java.sql.SQLException; |
9 | import java.util.ArrayList; |
10 | |
11 | import org.h2.engine.Constants; |
12 | import org.h2.store.FileLister; |
13 | import org.h2.store.fs.FileUtils; |
14 | import org.h2.util.Tool; |
15 | |
16 | /** |
17 | * Deletes all files belonging to a database. |
18 | * <br /> |
19 | * The database must be closed before calling this tool. |
20 | * @h2.resource |
21 | */ |
22 | public class DeleteDbFiles extends Tool { |
23 | |
24 | /** |
25 | * Options are case sensitive. Supported options are: |
26 | * <table> |
27 | * <tr><td>[-help] or [-?]</td> |
28 | * <td>Print the list of options</td></tr> |
29 | * <tr><td>[-dir <dir>]</td> |
30 | * <td>The directory (default: .)</td></tr> |
31 | * <tr><td>[-db <database>]</td> |
32 | * <td>The database name</td></tr> |
33 | * <tr><td>[-quiet]</td> |
34 | * <td>Do not print progress information</td></tr> |
35 | * </table> |
36 | * @h2.resource |
37 | * |
38 | * @param args the command line arguments |
39 | */ |
40 | public static void main(String... args) throws SQLException { |
41 | new DeleteDbFiles().runTool(args); |
42 | } |
43 | |
44 | @Override |
45 | public void runTool(String... args) throws SQLException { |
46 | String dir = "."; |
47 | String db = null; |
48 | boolean quiet = false; |
49 | for (int i = 0; args != null && i < args.length; i++) { |
50 | String arg = args[i]; |
51 | if (arg.equals("-dir")) { |
52 | dir = args[++i]; |
53 | } else if (arg.equals("-db")) { |
54 | db = args[++i]; |
55 | } else if (arg.equals("-quiet")) { |
56 | quiet = true; |
57 | } else if (arg.equals("-help") || arg.equals("-?")) { |
58 | showUsage(); |
59 | return; |
60 | } else { |
61 | showUsageAndThrowUnsupportedOption(arg); |
62 | } |
63 | } |
64 | process(dir, db, quiet); |
65 | } |
66 | |
67 | /** |
68 | * Deletes the database files. |
69 | * |
70 | * @param dir the directory |
71 | * @param db the database name (null for all databases) |
72 | * @param quiet don't print progress information |
73 | */ |
74 | public static void execute(String dir, String db, boolean quiet) { |
75 | new DeleteDbFiles().process(dir, db, quiet); |
76 | } |
77 | |
78 | /** |
79 | * Deletes the database files. |
80 | * |
81 | * @param dir the directory |
82 | * @param db the database name (null for all databases) |
83 | * @param quiet don't print progress information |
84 | */ |
85 | private void process(String dir, String db, boolean quiet) { |
86 | ArrayList<String> files = FileLister.getDatabaseFiles(dir, db, true); |
87 | if (files.size() == 0 && !quiet) { |
88 | printNoDatabaseFilesFound(dir, db); |
89 | } |
90 | for (String fileName : files) { |
91 | process(fileName, quiet); |
92 | if (!quiet) { |
93 | out.println("Processed: " + fileName); |
94 | } |
95 | } |
96 | } |
97 | |
98 | private static void process(String fileName, boolean quiet) { |
99 | if (FileUtils.isDirectory(fileName)) { |
100 | // only delete empty directories |
101 | FileUtils.tryDelete(fileName); |
102 | } else if (quiet || fileName.endsWith(Constants.SUFFIX_TEMP_FILE) || |
103 | fileName.endsWith(Constants.SUFFIX_TRACE_FILE)) { |
104 | FileUtils.tryDelete(fileName); |
105 | } else { |
106 | FileUtils.delete(fileName); |
107 | } |
108 | } |
109 | |
110 | } |