summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/ImageValidCheck.java
blob: 0003e0dd5c63a30f5241815cb7332c16d61a9a51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.openslx.bwlp.sat.maintenance;

import java.io.File;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.database.mappers.DbImage;
import org.openslx.bwlp.sat.database.models.LocalImageVersion;
import org.openslx.bwlp.sat.util.FileSystem;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.util.Util;

public class ImageValidCheck implements Runnable {

	private static final Logger LOGGER = Logger.getLogger(ImageValidCheck.class);

	private final String versionId;

	public static void check(String versionId) {
		if (versionId == null)
			return;
		Maintenance.trySubmit(new ImageValidCheck(versionId));
	}

	private ImageValidCheck(String versionId) {
		this.versionId = versionId;
	}

	@Override
	public void run() {
		if (!FileSystem.waitForStorage()) {
			LOGGER.warn("Will not check " + versionId + ": Storage not online");
			return;
		}
		LocalImageVersion imageVersion;
		try {
			imageVersion = DbImage.getLocalImageData(versionId);
		} catch (SQLException e) {
			return;
		} catch (TNotFoundException e) {
			LOGGER.warn("Cannot check validity of image version - not found: " + versionId);
			return;
		}
		boolean valid = checkValid(imageVersion);
		// TODO: We could check the checksums too to be extra safe
		/*
		ImageVersionMeta versionDetails;
		try {
			versionDetails = DbImage.getVersionDetails(versionId);
		} catch (TNotFoundException e) {
			LOGGER.warn("Cannot check validity of image version - not found: " + versionId);
			return;
		} catch (SQLException e) {
			return;
		}
		*/
		if (imageVersion.isValid == valid)
			return; // nothing changed
		// Update
		try {
			DbImage.markValid(valid, false, imageVersion);
		} catch (SQLException e) {
		}
	}

	private boolean checkValid(LocalImageVersion imageVersion) {
		if (imageVersion == null)
			return false;
		if (imageVersion.expireTime < Util.unixTime()) {
			LOGGER.info(versionId + ": expired");
			return false;
		}
		if (imageVersion.filePath == null || imageVersion.filePath.isEmpty()) {
			LOGGER.info(versionId + ": DB does not contain a path");
			return false;
		}
		File path = FileSystem.composeAbsoluteImagePath(imageVersion);
		if (path == null) {
			LOGGER.info(versionId + ": path from DB is not valid");
			return false;
		}
		if (!path.exists()) {
			LOGGER.info(versionId + ": File does not exist (" + path.getAbsolutePath() + ")");
			return false;
		}
		if (!path.canRead()) {
			LOGGER.info(versionId + ": File exists but not readable (" + path.getAbsolutePath() + ")");
			return false;
		}
		if (path.length() != imageVersion.fileSize) {
			LOGGER.info(versionId + ": File exists but has wrong size (expected: " + imageVersion.fileSize
					+ ", found: " + path.length() + ")");
			return false;
		}
		return true;
	}

}