2016-04-19 22:34:25 +02:00
|
|
|
/*
|
2016-04-26 23:02:26 +02:00
|
|
|
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
|
2016-04-19 22:34:25 +02:00
|
|
|
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
|
|
|
|
*
|
|
|
|
* This file is part of Siba.
|
|
|
|
*
|
|
|
|
* Siba is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-03-29 23:25:18 +02:00
|
|
|
package org.dclermonte.siba.model;
|
2016-03-26 23:26:19 +01:00
|
|
|
|
2016-04-26 23:02:26 +02:00
|
|
|
import java.io.BufferedOutputStream;
|
2016-05-25 18:10:34 +02:00
|
|
|
import java.io.Closeable;
|
2016-03-27 22:32:35 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2016-05-02 22:28:35 +02:00
|
|
|
import java.io.FileReader;
|
2016-03-27 22:32:35 +02:00
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2016-04-01 00:08:32 +02:00
|
|
|
import java.time.LocalDateTime;
|
2016-04-07 00:54:03 +02:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2016-05-19 18:40:00 +02:00
|
|
|
import java.util.ResourceBundle;
|
2016-03-27 22:32:35 +02:00
|
|
|
|
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
2016-04-26 23:02:26 +02:00
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
2016-04-12 00:41:43 +02:00
|
|
|
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
2016-05-18 23:20:29 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2016-05-19 18:40:00 +02:00
|
|
|
import org.dclermonte.siba.SibaException;
|
2016-03-27 22:32:35 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
|
|
|
*
|
2016-05-22 21:47:41 +02:00
|
|
|
* @author dclermonte
|
2016-05-18 23:20:29 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
public class SibaManager
|
2016-03-26 23:26:19 +01:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.model.messages"); //$NON-NLS-1$
|
2016-05-19 18:40:00 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method perform the backup.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param fileToSave
|
|
|
|
* @param target
|
|
|
|
* @return
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-22 21:47:41 +02:00
|
|
|
public static File backup(final File fileToSave, File target) throws SibaException
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
File result;
|
2016-05-22 21:47:41 +02:00
|
|
|
try
|
2016-05-16 23:40:07 +02:00
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
if ((fileToSave == null) || !(fileToSave.exists()))
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("sourceNotExist.text"));
|
|
|
|
}
|
|
|
|
else
|
2016-05-19 18:40:00 +02:00
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
if (!(fileToSave.isDirectory()))
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("NotDirectorySource.text"));
|
|
|
|
}
|
|
|
|
else
|
2016-05-22 21:47:41 +02:00
|
|
|
{
|
|
|
|
if ((target == null))
|
|
|
|
{
|
|
|
|
target = new File(System.getProperty("user.dir"));
|
|
|
|
}
|
2016-05-25 18:10:34 +02:00
|
|
|
if (!(target.isDirectory()))
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("NotDirectoryTarget.text"));
|
|
|
|
}
|
|
|
|
else
|
2016-05-22 21:47:41 +02:00
|
|
|
{
|
|
|
|
int pathLength = fileToSave.getParentFile().getAbsolutePath().length();
|
|
|
|
LocalDateTime date = LocalDateTime.now();
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH'h'mm'mn'ss's'");
|
|
|
|
String textDate = date.format(formatter);
|
|
|
|
String outputFileNameWithoutExtension = fileToSave.getName() + "-" + textDate;
|
|
|
|
|
|
|
|
result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz");
|
|
|
|
OutputStream gzipOutputStream = new GzipCompressorOutputStream(
|
|
|
|
new BufferedOutputStream(new FileOutputStream(result)));
|
|
|
|
TarArchiveOutputStream out = new TarArchiveOutputStream(gzipOutputStream);
|
|
|
|
directoryToSave(fileToSave, out, pathLength);
|
|
|
|
out.close();
|
|
|
|
fileCheckMD5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 18:40:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
catch (IOException ioExceptionBackup)
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("ioExceptionBackup.text"), ioExceptionBackup); //$NON-NLS-1$
|
|
|
|
}
|
2016-05-25 18:10:34 +02:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
Closeable out = null;
|
|
|
|
IOUtils.closeQuietly(out);
|
|
|
|
}
|
|
|
|
//
|
2016-05-22 21:47:41 +02:00
|
|
|
return result;
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method is used to check the file.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param choosenFile
|
|
|
|
* @return
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-22 21:47:41 +02:00
|
|
|
public static boolean check(final File choosenFile) throws SibaException
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
2016-05-25 18:10:34 +02:00
|
|
|
boolean result;
|
2016-05-22 21:47:41 +02:00
|
|
|
try
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
if (choosenFile.exists() && (choosenFile.length() > 32))
|
|
|
|
{
|
|
|
|
FileReader fileReader = new FileReader(choosenFile);
|
|
|
|
char[] md5 = new char[32];
|
|
|
|
byte[] md5byte = new byte[32];
|
|
|
|
char[] fileToCheck = new char[(int) (choosenFile.length()) - 32];
|
|
|
|
String md5String = new String();
|
|
|
|
for (int index = 0; index < 32; index++)
|
|
|
|
{
|
|
|
|
md5[index] = (char) fileReader.read();
|
|
|
|
md5byte[index] = (byte) md5[index];
|
|
|
|
md5String = md5String + md5[index];
|
|
|
|
}
|
|
|
|
fileReader.read();
|
|
|
|
String fileNameToString = new String();
|
|
|
|
for (int index = 36; index < (choosenFile.length() + 2); index++)
|
|
|
|
{
|
|
|
|
fileToCheck[index - 36] = (char) fileReader.read();
|
|
|
|
fileNameToString = fileNameToString + fileToCheck[index - 36];
|
|
|
|
}
|
2016-05-02 22:28:35 +02:00
|
|
|
|
2016-05-22 21:47:41 +02:00
|
|
|
fileReader.close();
|
|
|
|
File fileToCheck1;
|
|
|
|
if (choosenFile.isAbsolute())
|
|
|
|
{
|
|
|
|
fileToCheck1 = new File(choosenFile.getParent() + "/" + fileNameToString);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String path = System.getProperty("user.dir");
|
|
|
|
fileToCheck1 = new File(path + "/" + fileNameToString);
|
|
|
|
}
|
2016-04-26 23:02:26 +02:00
|
|
|
|
2016-05-22 21:47:41 +02:00
|
|
|
if (StringUtils.equals(md5String, md5(fileToCheck1)))
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("NotExistFileToCheck.text"));
|
|
|
|
}
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
catch (IOException ioExceptionCheck)
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
throw new SibaException(BUNDLE.getString("ioExceptionCheck.text"), ioExceptionCheck); //$NON-NLS-1$
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
2016-05-25 18:10:34 +02:00
|
|
|
//
|
2016-04-19 22:34:25 +02:00
|
|
|
return result;
|
2016-04-01 00:08:32 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
2016-05-19 18:40:00 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This method generate ArchiveEntry.
|
|
|
|
*
|
|
|
|
* @param directory
|
|
|
|
* @param outputStream
|
|
|
|
* @param pathLength
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-19 18:40:00 +02:00
|
|
|
*/
|
2016-05-02 22:28:35 +02:00
|
|
|
public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream,
|
2016-05-22 21:47:41 +02:00
|
|
|
final int pathLength) throws SibaException
|
2016-04-07 00:54:03 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
try
|
2016-03-27 22:32:35 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
for (File file : directory.listFiles())
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
|
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
|
|
|
if (file.listFiles().length == 0)
|
|
|
|
{
|
|
|
|
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
|
|
|
|
String pathPartiel = file.getPath().substring(pathLength);
|
|
|
|
tarArchiveEntry.setName(pathPartiel);
|
|
|
|
outputStream.putArchiveEntry(tarArchiveEntry);
|
|
|
|
outputStream.closeArchiveEntry();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
directoryToSave(file, outputStream, pathLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2016-05-17 23:59:42 +02:00
|
|
|
{
|
|
|
|
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
|
|
|
|
String pathPartiel = file.getPath().substring(pathLength);
|
|
|
|
tarArchiveEntry.setName(pathPartiel);
|
|
|
|
outputStream.putArchiveEntry(tarArchiveEntry);
|
2016-05-22 21:47:41 +02:00
|
|
|
IOUtils.copy(new FileInputStream(file), outputStream);
|
2016-05-17 23:59:42 +02:00
|
|
|
outputStream.closeArchiveEntry();
|
2016-04-26 23:02:26 +02:00
|
|
|
|
2016-05-22 21:47:41 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
catch (IOException ioExceptionTarEntry)
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("ioExceptionTarEntry.text"), ioExceptionTarEntry); //$NON-NLS-1$
|
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
*
|
|
|
|
* This method Generate the file with MD5.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param directoryToSave
|
|
|
|
* @param destination
|
|
|
|
* @return
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-22 21:47:41 +02:00
|
|
|
public static File fileCheckMD5(final File directoryToSave, final String destination) throws SibaException
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-04-23 17:50:13 +02:00
|
|
|
File result;
|
2016-05-22 21:47:41 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
result = new File(destination);
|
|
|
|
FileWriter fileWriter = new FileWriter(result);
|
|
|
|
fileWriter.write(md5(directoryToSave));
|
|
|
|
String newLine = System.getProperty("line.separator");
|
|
|
|
fileWriter.append(" " + directoryToSave.getName() + newLine);
|
|
|
|
fileWriter.close();
|
|
|
|
}
|
|
|
|
catch (IOException ioExceptionFileMD5)
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("ioExceptionFileMD5.text"), ioExceptionFileMD5); //$NON-NLS-1$
|
|
|
|
}
|
2016-05-25 18:10:34 +02:00
|
|
|
//
|
2016-05-18 23:20:29 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method calculate the MD5 itself.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param input
|
|
|
|
* @return
|
2016-05-22 21:47:41 +02:00
|
|
|
* @throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-22 21:47:41 +02:00
|
|
|
public static String md5(final File input) throws SibaException
|
2016-05-18 23:20:29 +02:00
|
|
|
{
|
|
|
|
String result;
|
|
|
|
StringBuilder hashString = new StringBuilder();
|
2016-04-12 00:41:43 +02:00
|
|
|
try
|
|
|
|
{
|
2016-05-18 23:20:29 +02:00
|
|
|
byte[] byteInput = new byte[(int) input.length()];
|
|
|
|
FileInputStream fileInputStream = new FileInputStream(input);
|
|
|
|
for (int index1 = 0; index1 < input.length(); index1++)
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-05-18 23:20:29 +02:00
|
|
|
byteInput[index1] = (byte) fileInputStream.read();
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
|
|
|
byte[] hash = null;
|
|
|
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
2016-05-18 23:20:29 +02:00
|
|
|
hash = messageDigest.digest(byteInput);
|
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
for (int index = 0; index < hash.length; index++)
|
|
|
|
{
|
|
|
|
String hex = Integer.toHexString(hash[index]);
|
|
|
|
if (hex.length() == 1)
|
|
|
|
{
|
|
|
|
hashString.append('0');
|
|
|
|
hashString.append(hex.charAt(hex.length() - 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hashString.append(hex.substring(hex.length() - 2));
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 22:34:25 +02:00
|
|
|
fileInputStream.close();
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-05-22 21:47:41 +02:00
|
|
|
catch (NoSuchAlgorithmException noSuchAlgorithmException)
|
|
|
|
{
|
|
|
|
throw new SibaException(BUNDLE.getString("noSuchAlgorithm.text"), noSuchAlgorithmException); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
catch (IOException ioException)
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-05-22 21:47:41 +02:00
|
|
|
throw new SibaException(BUNDLE.getString("ioExceptionMD5.text"), ioException); //$NON-NLS-1$
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-05-18 23:20:29 +02:00
|
|
|
result = hashString.toString();
|
2016-05-25 18:10:34 +02:00
|
|
|
//
|
2016-04-19 22:34:25 +02:00
|
|
|
return result;
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-04-26 23:02:26 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|