| 1 | /* |
| 2 | * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.tools; |
| 20 | |
| 21 | import static org.lttng.tools.utils.ShellUtils.executeCommand; |
| 22 | |
| 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; |
| 31 | |
| 32 | /** |
| 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}. |
| 35 | * |
| 36 | * @author Alexandre Montplaisir |
| 37 | */ |
| 38 | public final class LttngToolsHelper { |
| 39 | |
| 40 | private LttngToolsHelper() {} |
| 41 | |
| 42 | /** |
| 43 | * Utility method to destroy all existing sessions. Useful when first |
| 44 | * setting up a test to make sure no existing session interferes. |
| 45 | * |
| 46 | * @return If the command completed successfully |
| 47 | */ |
| 48 | public static boolean destroyAllSessions() { |
| 49 | return executeCommand(Arrays.asList("lttng", "destroy", "-a")); |
| 50 | } |
| 51 | |
| 52 | /** |
| 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. |
| 56 | * |
| 57 | * @return True if the command completes successfully, false if there was an |
| 58 | * error. |
| 59 | */ |
| 60 | public static boolean deleteAllTraces() { |
| 61 | String tracesDir = new String(System.getProperty("user.home") + "/lttng-traces/"); |
| 62 | return deleteDirectory(Paths.get(tracesDir)); |
| 63 | } |
| 64 | |
| 65 | // ------------------------------------------------------------------------ |
| 66 | // Private helper methods |
| 67 | // ------------------------------------------------------------------------ |
| 68 | |
| 69 | private static boolean deleteDirectory(Path directory) { |
| 70 | try { |
| 71 | Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { |
| 72 | @Override |
| 73 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 74 | Files.delete(file); |
| 75 | return FileVisitResult.CONTINUE; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 80 | Files.delete(dir); |
| 81 | return FileVisitResult.CONTINUE; |
| 82 | } |
| 83 | }); |
| 84 | } catch (IOException e) { |
| 85 | /* At least we tried... */ |
| 86 | return false; |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | } |