summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/FileSystem.java
blob: dc0cec2d7aeaeaeba6724bd8324dd95b6f82a02a (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package org.openslx.bwlp.sat.util;

import java.io.File;

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.database.models.LocalImageVersion;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

public class FileSystem {

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

	public static String getRelativePath(File absolutePath, File parentDir) {
		String file;
		String dir;
		try {
			file = absolutePath.getCanonicalPath();
			dir = parentDir.getCanonicalPath() + File.separator;
		} catch (Exception e) {
			LOGGER.error("Could not get relative path for " + absolutePath.toString(), e);
			return null;
		}
		if (!file.startsWith(dir))
			return null;
		return file.substring(dir.length());
	}

	/**
	 * Delete given file on the {@link QuickTimer} thread, preventing the
	 * calling thread from freezing/hanging on I/O problems.
	 * 
	 * @param sourceCandidates the file to delete
	 */
	public static void deleteAsync(final File... files) {
		if (files == null || files.length == 0)
			return;
		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				for (File file : files) {
					if (file == null)
						continue;
					if (!file.exists()) {
						continue;
					}
					if (!file.delete()) {
						LOGGER.warn("Could not delete file " + file.getAbsolutePath());
					}
				}
			}
		});
	}

	/**
	 * Checks if the VM store is mounted.
	 * 
	 * @return true if mounted, false otherwise.
	 */
	public static boolean isStorageMounted() {
		File flagFile = new File(Configuration.getVmStoreBasePath(), ".notmounted");
		if (flagFile.exists())
			return false;
		File prodPath = Configuration.getVmStoreProdPath();
		if (prodPath.isDirectory())
			return true;
		return prodPath.mkdir();
	}

	private static Object storageMutex = new Object();

	public static boolean waitForStorage() {
		if (isStorageMounted())
			return true;
		synchronized (storageMutex) {
			if (isStorageMounted())
				return true;
			LOGGER.warn("VM storage gone, waiting for it to reappear...");
			long lastComplain = System.currentTimeMillis();
			do {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					LOGGER.warn("Interrupted while waiting", e);
					return false;
				}
				if (System.currentTimeMillis() - lastComplain > 600000) {
					lastComplain = System.currentTimeMillis();
					LOGGER.warn("Still waiting for storage...");
				}
			} while (!isStorageMounted());
		}
		LOGGER.info("VM storage back online");
		return true;
	}

	/**
	 * Delete the given image file (and all related files) from the storage.
	 * 
	 * @param image The image to be deleted
	 */
	public static void deleteImageRelatedFiles(LocalImageVersion image) {
		File imageFile = composeAbsoluteImagePath(image);
		if (imageFile == null)
			return;
		File metaFile = new File(imageFile.getPath() + ".meta");
		File crcFile = new File(imageFile.getPath() + ".crc");
		File mapFile = new File(imageFile.getPath() + ".map");
		if (!waitForStorage())
			return;
		deleteAsync(imageFile, metaFile, crcFile, mapFile);
	}

	/**
	 * Given a local image version, return a {@link File} object that holds the
	 * full path to the image file in the file system.
	 * 
	 * @param localImageData
	 * @return {@link File} representing the physical image file, or
	 *         <code>null</code> if the path would not be valid
	 */
	public static File composeAbsoluteImagePath(LocalImageVersion localImageData) {
		if (localImageData == null)
			return null;
		File path = composeAbsolutePath(localImageData.filePath);
		if (path == null) {
			LOGGER.warn("ImageVersionId is " + localImageData.imageVersionId);
		}
		return path;
	}

	/**
	 * Given a local relative path for an image, return a {@link File} object
	 * that holds the full path to the image file in the file system.
	 * 
	 * @param relativePath
	 * @return {@link File} representing the physical image file, or
	 *         <code>null</code> if the path would not be valid
	 */
	public static File composeAbsolutePath(String relativePath) {
		if (relativePath != null) {
			relativePath = FilenameUtils.normalize(relativePath);
		}
		if (relativePath == null || relativePath.startsWith("/")) {
			LOGGER.warn("Invalid path for local image: " + relativePath);
			return null;
		}
		return new File(Configuration.getVmStoreBasePath(), relativePath);
	}

	private static long lastStorageFailLog = 0;

	/**
	 * Query how much space is left in the vmstore directory.
	 * 
	 * @return free space in vmstore directory, in bytes, or -1 on error
	 */
	public static long getAvailableStorageBytes() {
		if (!isStorageMounted())
			return -1;
		try {
			return Configuration.getVmStoreProdPath().getUsableSpace();
		} catch (Exception e) {
			long now = System.currentTimeMillis();
			if (now - lastStorageFailLog > 60000) {
				lastStorageFailLog = now;
				LOGGER.warn("Could not determine free space of vmstore", e);
			}
			return -1;
		}
	}

}