summaryrefslogtreecommitdiffstats
path: root/application/service/storage.php
blob: 925827d46641314b93781e690180cc2c50fe5eee (plain)
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
<?php
/*
 * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace service;

/**
 * This class allows to change a temporary file and replace the original one atomically
 */
class storage {
	private $path;
	private $tempfile = NULL;

	public function __construct($path)
	{
		assert(!is_dir($path));

		$this->path = $path;
	}

	/**
	 * Create a temp file which can be written to.
	 *
	 * Call commit() once you are done writing.
	 * Call rollback() to remove the file and throw away any data written.
	 *
	 * Calling this multiple times will automatically rollback previous calls.
	 *
	 * @return temp file path
	 */
	public function begin()
	{
		if($this->tempfile !== NULL) {
			$this->rollback();
		}

		$this->tempfile = $this->create_tempfile();

		return $this->tempfile;
	}

	/**
	 * Create a temporary file. You'll need to remove it yourself when no longer needed.
	 *
	 * @return path to the temporary file
	 */
	private function create_tempfile()
	{
		$dir = dirname($this->get_file());
		$prefix = basename($this->get_file());

		if (!is_dir($dir)) {
			mkdir($dir, 0777, true);
		}
		assert(is_dir($dir));

		return tempnam($dir, $prefix);
	}

	/**
	 * Save the temporary file returned by begin() to the permanent path
	 * (supplied to the constructor) in an atomic fashion.
	 */
	public function commit()
	{
		$ret = rename($this->tempfile, $this->get_file());
		if ($ret) {
			$this->tempfile = NULL;
		}

		return $ret;
	}

	public function exists()
	{
		return file_exists($this->get_file());
	}

	public function get_file()
	{
		return $this->path;
	}

	/**
	 * Throw away any changes made to the temporary file returned by begin()
	 */
	public function rollback()
	{
		if ($this->tempfile !== NULL) {
			unlink($this->tempfile);
			$this->tempfile = NULL;
		}
	}

	public function __destruct()
	{
		$this->rollback();
	}

	/**
	 * GZIPs the temp file
	 *
	 * From http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php
	 * Based on function by Kioob at:
	 * http://www.php.net/manual/en/function.gzwrite.php#34955
	 *
	 * @param string $source Path to file that should be compressed
	 * @param integer $level GZIP compression level (default: 6)
	 * @return boolean true if operation succeeds, false on error
	 */
	public function gzip_compress($level = 6){
		if ($this->tempfile === NULL) {
			return;
		}

		$source = $this->tempfile;
		$file = new storage($source);
		$dest = $file->begin();
		$mode = 'wb' . $level;
		$error = false;
		$chunk_size = 1024*512;

		if ($fp_out = gzopen($dest, $mode)) {
			if ($fp_in = fopen($source,'rb')) {
				while (!feof($fp_in)) {
					gzwrite($fp_out, fread($fp_in, $chunk_size));
				}
				fclose($fp_in);
			} else {
				$error = true;
			}
			gzclose($fp_out);
		} else {
			$error = true;
		}

		if ($error) {
			return false;
		} else {
			$file->commit();
			return true;
		}
	}

	/**
	 * Delete the file if it exists.
	 */
	public function unlink()
	{
		if ($this->exists()) {
			unlink($this->get_file());
		}
	}
}

# vim: set noet: