Autopsy  4.22.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
Installer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2017-2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.eventlisteners;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Paths;
24 import java.util.Map;
25 import java.util.logging.Level;
26 import org.apache.commons.io.FileUtils;
27 import org.openide.modules.ModuleInstall;
32 
38 public class Installer extends ModuleInstall {
39 
40  private static final String LEGACY_DEFAULT_FOLDER = "central_repository";
41  private static final String LEGACY_DEFAULT_DB_PARENT_PATH = Paths.get(PlatformUtil.getUserDirectory().getAbsolutePath(), LEGACY_DEFAULT_FOLDER).toAbsolutePath().toString();
42  private static final String LEGACY_MODULE_SETTINGS_KEY = "CentralRepository";
43 
44  private static final Logger logger = Logger.getLogger(Installer.class.getName());
45  private static final long serialVersionUID = 1L;
46  private static Installer instance;
47 
56  public synchronized static Installer getDefault() {
57  if (instance == null) {
58  instance = new Installer();
59  }
60  return instance;
61  }
62 
68  private Installer() {
69  super();
70  }
71 
72  /*
73  * Sets up a default, single-user SQLite central
74  * repository if no central repository is configured.
75  *
76  * Called by the registered Installer for the Autopsy-Core module located in
77  * the org.sleuthkit.autopsy.core package when the already installed
78  * Autopsy-Core module is restored (during application startup).
79  */
80  @Override
81  public void restored() {
82  // must happen first to move any legacy settings that exist.
84  }
85 
86 
87 
96  private String getSettingsFilePath(String moduleName) {
97  return Paths.get(PlatformUtil.getUserConfigDirectory(), moduleName + ".properties").toString();
98  }
99 
103  private void upgradeSettingsPath() {
104  File newSettingsFile = new File(getSettingsFilePath(CentralRepoSettings.getInstance().getModuleSettingsKey()));
105  File legacySettingsFile = new File(getSettingsFilePath(LEGACY_MODULE_SETTINGS_KEY));
106  // new config has not been created, but legacy has, copy it.
107  if (!newSettingsFile.exists() && legacySettingsFile.exists()) {
108  Map<String, String> prevSettings = ModuleSettings.getConfigSettings(LEGACY_MODULE_SETTINGS_KEY);
109  String prevPath = prevSettings.get(CentralRepoSettings.getInstance().getDatabasePathKey());
110  File prevDirCheck = new File(prevPath);
111  // if a relative directory, make sure it is relative to user config.
112  if (!prevDirCheck.isAbsolute()) {
113  prevPath = Paths.get(PlatformUtil.getUserDirectory().getAbsolutePath(), prevPath).toAbsolutePath().toString();
114  }
115 
116  // if old path is default path for sqlite db, copy it over to new location and update setting.
117  if (prevPath != null
118  && Paths.get(LEGACY_DEFAULT_DB_PARENT_PATH).toAbsolutePath().toString().equals(Paths.get(prevPath).toAbsolutePath().toString())) {
119  String prevDbName = prevSettings.get(CentralRepoSettings.getInstance().getDatabaseNameKey());
120  File prevDir = new File(prevPath);
121  // copy all files starting with prevDbName in prevPath to new path location.
122  if (prevDir.exists() && prevDir.isDirectory()) {
123  new File(CentralRepoSettings.getInstance().getDefaultDbPath()).mkdirs();
124  try {
125  for (File childFile : prevDir.listFiles((dir, name) -> name.startsWith(prevDbName))) {
126  FileUtils.copyFile(childFile, new File(CentralRepoSettings.getInstance().getDefaultDbPath(), childFile.getName()));
127  }
128  } catch (IOException ex) {
129  logger.log(Level.SEVERE, "There was an error upgrading settings.", ex);
130  }
131  }
132 
133  // get the new relative path to store
134  String newRelPath = PlatformUtil.getUserDirectory().toPath().relativize(Paths.get(CentralRepoSettings.getInstance().getDefaultDbPath())).toString();
135  // update path settings accordingly
136  prevSettings.put(CentralRepoSettings.getInstance().getDatabasePathKey(), newRelPath);
137  }
138 
139  // copy settings
141  }
142  }
143 
144  @Override
145  public void uninstalled() {
146  /*
147  * TODO (Jira-6108): This code is erronoeous. As documented at
148  * http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/ModuleInstall.html#uninstalled--
149  *
150  * "Called when the module is disabled while the application is still
151  * running. Should remove whatever functionality that it had registered
152  * in ModuleInstall.restored().
153  *
154  * Beware: in practice there is no way to
155  * ensure that this method will really be called. The module might
156  * simply be deleted or disabled while the application is not running.
157  * In fact this is always the case in NetBeans 6.0; the Plugin Manager
158  * only uninstalls or disables modules between restarts. This method
159  * will still be called if you reload a module during development."
160  *
161  * THIS CODE IS NEVER EXECUTED.
162  */
163  }
164 }
static synchronized void setConfigSettings(String moduleName, Map< String, String > settings)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static synchronized Map< String, String > getConfigSettings(String moduleName)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on: Mon Mar 17 2025
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.