2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 package org
.lttng
.tools
;
21 import static org
.lttng
.tools
.utils
.ShellUtils
.executeCommand
;
23 import java
.io
.IOException
;
24 import java
.nio
.file
.FileVisitResult
;
25 import java
.nio
.file
.Files
;
26 import java
.nio
.file
.Path
;
27 import java
.nio
.file
.Paths
;
28 import java
.nio
.file
.SimpleFileVisitor
;
29 import java
.nio
.file
.attribute
.BasicFileAttributes
;
30 import java
.util
.Arrays
;
33 * Helper class to issue LTTng commands that do not affect a single session, and
34 * as such, do not fit into the scope of one {@link ILttngSession}.
36 * @author Alexandre Montplaisir
38 public final class LttngToolsHelper
{
40 private LttngToolsHelper() {}
43 * Utility method to destroy all existing sessions. Useful when first
44 * setting up a test to make sure no existing session interferes.
46 * @return If the command completed successfully
48 public static boolean destroyAllSessions() {
49 return executeCommand(Arrays
.asList("lttng", "destroy", "-a"));
53 * Outside of the scope of lttng-tools, but this utility method can be used
54 * to delete all traces currently under ~/lttng-traces/. This can be used by
55 * tests to cleanup a trace they have created.
57 * @return True if the command completes successfully, false if there was an
60 public static boolean deleteAllTraces() {
61 String tracesDir
= new String(System
.getProperty("user.home") + "/lttng-traces/");
62 return deleteDirectory(Paths
.get(tracesDir
));
65 // ------------------------------------------------------------------------
66 // Private helper methods
67 // ------------------------------------------------------------------------
69 private static boolean deleteDirectory(Path directory
) {
71 Files
.walkFileTree(directory
, new SimpleFileVisitor
<Path
>() {
73 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
75 return FileVisitResult
.CONTINUE
;
79 public FileVisitResult
postVisitDirectory(Path dir
, IOException exc
) throws IOException
{
81 return FileVisitResult
.CONTINUE
;
84 } catch (IOException e
) {
85 /* At least we tried... */