Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.VaultDiskFs.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * @author Anakeen
8  * @package FDL
9  */
10 // ---------------------------------------------------------------
11 include_once ("Class.QueryDb.php");
12 include_once ("Class.DbObj.php");
13 include_once ("VAULT/Class.VaultDiskDir.php");
14 
15 class VaultDiskFs extends DbObj
16 {
17  var $fields = array(
18  "id_fs",
19  "fsname",
20  "max_size",
21  "r_path"
22  );
23  var $id_fields = array(
24  "id_fs"
25  );
26  var $dbtable_tmpl = "vaultdiskfs%s";
27  var $order_by = "";
28  var $seq_tmpl = "seq_id_vaultdiskfs%s";
30  create table vaultdiskfs%s (
31  id_fs int not null,
32  fsname text,
33  primary key (id_fs),
34  max_size int8,
35  r_path text
36  );
37  create sequence seq_id_vaultdiskfs%s start 10;
38 
40 
41 
42  /**
43  * @var int file system id (10 is the first)
44  */
45  public $id_fs;
46  public $fsname;
47  public $max_size;
48  /**
49  * @var string path to vault root
50  */
51  public $r_path;
52  public $specific;
53  protected $seq;
54  /**
55  * @var VaultDiskDirStorage
56  */
57  protected $sd;
58  private $htaccess = <<<EOF
59 Order Allow,Deny
60 Deny from all
61 
62 EOF;
63  // --------------------------------------------------------------------
64  function __construct($dbaccess, $id_fs = '')
65  {
66  // --------------------------------------------------------------------
67  $this->dbtable = sprintf($this->dbtable_tmpl, $this->specific);
68  $this->sqlcreate = sprintf($this->sqlcreate_tmpl, $this->specific, $this->specific);
69  $this->seq = sprintf($this->seq_tmpl, $this->specific);
70  parent::__construct($dbaccess, $id_fs);
71  }
72 
73  function createArch($maxsize, $path, $fsname = "-")
74  {
75  if (!is_dir($path)) {
76  return sprintf(_("%s directory not found") , $path);
77  }
78  if (!is_writable($path)) {
79  return sprintf(_("%s directory not writable") , $path);
80  }
81  if (($err = $this->setHtaccess($path)) != "") {
82  return $err;
83  }
84  $this->fsname = $fsname;
85  $this->max_size = $maxsize;
86  $this->r_path = $path;
87  return $this->Add();
88  }
89  /**
90  * verify if fs is availlable (file system is mounted)
91  * @return bool
92  */
93  function isAvailable()
94  {
95  if ($this->isAffected()) {
96  if ($this->r_path) {
97  if (is_dir($this->r_path)) return true;
98  }
99  }
100  return false;
101  }
102  // --------------------------------------------------------------------
103  function PreInsert()
104  {
105  // --------------------------------------------------------------------
106  if ($this->Exists($this->r_path)) return (_("File System already exists"));
107  $this->exec_query(sprintf("select nextval ('%s')", pg_escape_string($this->seq)));
108  $arr = $this->fetch_array(0);
109  $this->id_fs = $arr["nextval"];
110  return '';
111  }
112  // --------------------------------------------------------------------
113  function Exists($path)
114  {
115  // --------------------------------------------------------------------
116  $query = new QueryDb($this->dbaccess, $this->dbtable);
117  $query->basic_elem->sup_where = array(
118  "r_path=E'" . pg_escape_string($path) . "'"
119  );
120  $query->Query(0, 0, "TABLE");
121  return ($query->nb > 0);
122  }
123  // --------------------------------------------------------------------
124  function SetFreeFs($f_size, &$id_fs, &$id_dir, &$f_path, $fsname)
125  {
126  // --------------------------------------------------------------------
127  $id_fs = $id_dir = - 1;
128  $f_path = "";
129 
130  $freeFs = $this->findFreeFS($f_size, $fsname);
131  if ($freeFs) {
132  $ifs = $this->getValues();
133 
134  $this->sd = new VaultDiskDir($this->dbaccess, '', $this->specific);
135  $err = $this->sd->SetFreeDir($ifs);
136 
137  if (!$err) {
139  $id_dir = $this->sd->id_dir;
140  $f_path = $this->r_path . "/" . $this->sd->l_path;
141  if (!is_dir($f_path)) {
142  if (!mkdir($f_path, VaultFile::VAULT_DMODE, true)) {
143  return (sprintf(_("Failed to create directory \"%s\" in vault") , $f_path));
144  }
145  }
146  } else {
147  return ($err);
148  }
149  unset($t);
150  } else {
151  return (_("no empty vault file system found"));
152  }
153  return "";
154  }
155 
156  public function closeCurrentDir()
157  {
158  return $this->sd->closeDir();
159  }
160 
161  public function findFreeFS($size, $specificFs = "")
162  {
163  $sql = <<<SQL
164 
165 select vaultdiskfsstorage.*, y.size
166 from vaultdiskfsstorage, (
167  select sum(c) as size, id_fs from (
168  (
169  SELECT CASE WHEN sum(vaultdiskstorage.size) IS NULL THEN 0 ELSE sum(vaultdiskstorage.size) END AS c, vaultdiskdirstorage.id_fs
170  FROM vaultdiskstorage RIGHT JOIN vaultdiskdirstorage ON (vaultdiskdirstorage.id_fs = vaultdiskstorage.id_fs AND vaultdiskdirstorage.id_dir = vaultdiskstorage.id_dir)
171  WHERE NOT vaultdiskdirstorage.isfull
172  GROUP BY vaultdiskdirstorage.id_fs
173  )
174  union
175  (select sum(size) as c, vaultdiskdirstorage.id_fs
176  from vaultdiskdirstorage
177  where isfull
178  group by vaultdiskdirstorage.id_fs)) as z group by id_fs) as y
179  where y.id_fs =vaultdiskfsstorage.id_fs :SQLFSNAME:
180  and vaultdiskfsstorage.max_size > (y.size + %d)
181  order by vaultdiskfsstorage.id_fs
182  ;
183 SQL;
184 
185  if ($specificFs) {
186  $sqlName = sprintf("and vaultdiskfsstorage.fsname='%s'", pg_escape_string($specificFs));
187  } else {
188  $sqlName = '';
189  }
190 
191  $sql = sprintf(str_replace(":SQLFSNAME:", $sqlName, $sql) , $size);
192  $this->exec_query($sql);
193  if ($this->numrows() > 0) {
194  $result = $this->fetch_array(0);
195  if ($result) {
196  $this->affect($result);
197 
198  return $result["id_fs"];
199  }
200  } else {
201  // May be a new vault
202  $sql = "select * from vaultdiskfsstorage where id_fs not in (select id_fs from vaultdiskdirstorage ) order by id_fs;";
203  $this->exec_query($sql);
204  if ($this->numrows() > 0) {
205  $result = $this->fetch_array(0);
206  if ($result) {
207  $this->affect($result);
208 
209  return $result["id_fs"];
210  }
211  }
212  }
213  return false;
214  }
215 
216  public function getSize()
217  {
218  $sql = <<<SQL
219 
220 select vaultdiskfsstorage.*, y.size
221 from vaultdiskfsstorage, (
222  select sum(c) as size, id_fs from (
223  (select sum(vaultdiskstorage.size) as c, vaultdiskdirstorage.id_fs
224  from vaultdiskstorage, vaultdiskdirstorage
225  where vaultdiskdirstorage.id_dir = vaultdiskstorage.id_dir and not isfull
226  group by vaultdiskdirstorage.id_fs )
227  union
228  (select sum(size) as c, vaultdiskdirstorage.id_fs
229  from vaultdiskdirstorage
230  where isfull
231  group by vaultdiskdirstorage.id_fs)) as z group by id_fs) as y
232  where y.id_fs =vaultdiskfsstorage.id_fs
233  and vaultdiskfsstorage.id_fs = %d
234  ;
235 SQL;
236 
237  $sql = sprintf($sql, $this->id_fs);
238  $this->exec_query($sql);
239  if ($this->numrows() > 0) {
240  $result = $this->fetch_array(0);
241  if ($result) {
242  $this->affect($result);
243 
244  return intval($result["size"]);
245  }
246  }
247  return -1;
248  }
249 
250  public function recomputeDirectorySize()
251  {
252  $sql = "update vaultdiskdirstorage set size=(select sum(size) from vaultdiskstorage where id_dir=vaultdiskdirstorage.id_dir) where isfull;";
253  $this->exec_query($sql);
254  $sql = "update vaultdiskdirstorage set size=0 where isfull and size is null;";
255  $this->exec_query($sql);
256  }
257  // --------------------------------------------------------------------
258  function Show($id_fs, $id_dir, &$f_path)
259  {
260  // --------------------------------------------------------------------
261  $query = new QueryDb($this->dbaccess, $this->dbtable);
262  $query->basic_elem->sup_where = array(
263  sprintf("id_fs=%d", $id_fs)
264  );
265  $t = $query->Query(0, 0, "TABLE");
266  if ($query->nb > 0) {
267  $sd = new VaultDiskDir($this->dbaccess, $id_dir, $this->specific);
268  if ($sd->IsAffected()) {
269  $f_path = $t[0]["r_path"] . "/" . $sd->l_path;
270  } else {
271  return (_("no vault directory found"));
272  }
273  } else {
274  return (_("no vault file system found"));
275  }
276  return '';
277  }
278  // --------------------------------------------------------------------
279  function DelEntry($id_fs, $id_dir, $fs)
280  {
281  // --------------------------------------------------------------------
283  if ($this->IsAffected()) {
284  $sd = new VaultDiskDir($this->dbaccess, $id_dir, $this->specific);
285  if ($sd->IsAffected()) {
286  $sd->DelEntry();
287  } else {
288  return (_("no vault directory found"));
289  }
290  } else {
291  return (_("no vault file system found"));
292  }
293  return '';
294  }
295  // --------------------------------------------------------------------
296  private function setHtaccess($path)
297  {
298  $htaccess = sprintf("%s/.htaccess", $path);
299  if (file_exists($htaccess)) {
300  return "";
301  }
302  if (file_put_contents($htaccess, $this->htaccess) === false) {
303  return sprintf(_("Error writing content to '%s'.") , $htaccess);
304  }
305  return "";
306  }
307 }
exec_query($sql, $lvl=0, $prepare=false)
const VAULT_DMODE
Add($nopost=false, $nopre=false)
$size
Definition: resizeimg.php:110
Select($id)
create sequence seq_id_vaultdiskfs s start
isAffected()
fetch_array($c, $type=PGSQL_ASSOC)
Show($id_fs, $id_dir, &$f_path)
getValues()
__construct($dbaccess, $id_fs= '')
$path
Definition: dav.php:39
SetFreeFs($f_size, &$id_fs, &$id_dir, &$f_path, $fsname)
createArch($maxsize, $path, $fsname="-")
DelEntry($id_fs, $id_dir, $fs)
if(($docid!==0)&&(!is_numeric($docid))) $query
if($file) if($subject==""&&$file) if($subject=="") $err
affect($array, $more=false, $reset=true)
findFreeFS($size, $specificFs="")
← centre documentaire © anakeen