3 * FusionForge Generic Storage Class
5 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
6 * Copyright 2012, Franck Villaume - TrivialDev
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Standard Alcatel-Lucent disclaimer for contributing to open source
27 * "The Artifact ("Contribution") has not been tested and/or
28 * validated for release as or in products, combinations with products or
29 * other commercial use. Any use of the Contribution is entirely made at
30 * the user's own responsibility and the user can not rely on any features,
31 * functionalities or performances Alcatel-Lucent has attributed to the
34 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
35 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
36 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
37 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
38 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
39 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
40 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
41 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
42 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
46 class Storage extends Error {
47 protected static $_instance;
48 var $pending_store = array();
49 var $pending_delete = array();
51 function store($key, $file) {
52 $storage = $this->get_storage($key);
53 $dir = dirname($storage);
55 if (!mkdir( $dir, 0755, true)) {
56 $this->setError(_('Cannot create directory:').' '.$dir);
61 $this->pending_store[] = $storage;
63 if (is_file($file) && is_dir($dir)) {
64 $ret = rename($file, $storage);
66 $this->setError(sprintf(_('File %1$s cannot be moved to the permanent location: %2$s.'), $file, $storage));
70 $this->setError(sprintf(_('Not a File %1$s or not a directory %2$s.'), $file, $dir));
77 return $this->get_storage($key);
80 function delete($key) {
81 $this->pending_delete[] = $this->get_storage($key);
82 return self::$_instance;
85 function deleteFromQuery($query, $params) {
86 $res = db_query_params($query, $params);
87 while($row = db_fetch_array($res)) {
88 $this->delete($row['id']);
93 foreach ($this->pending_delete as $f) {
94 rename($f, "$f.removed");
97 $this->pending_store = array();
98 $this->pending_delete = array();
101 function rollback() {
102 foreach ($this->pending_store as $f) {
105 $this->pending_store = array();
106 $this->pending_delete = array();
109 function get_storage($key) {
111 $pre = substr($key, strlen($key)-2);
112 $last = substr($key, 0, strlen($key)-2);
113 if (!$last) $last = '0';
114 return $this->get_storage_path().'/'.$pre.'/'.$last;