Platform  3.1
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  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * @author Anakeen
9  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
10  * @package FDL
11  */
12 // ---------------------------------------------------------------
13 // $Id: Class.VaultDiskFs.php,v 1.17 2008/11/21 09:57:23 jerome Exp $
14 // $Source: /home/cvsroot/anakeen/freedom/vault/Class/Class.VaultDiskFs.php,v $
15 // ---------------------------------------------------------------
16 //
17 //
18 //
19 // ---------------------------------------------------------------
20 include_once ("Class.QueryDb.php");
21 include_once ("Class.DbObj.php");
22 include_once ("VAULT/Class.VaultDiskDir.php");
23 
24 class VaultDiskFs extends DbObj
25 {
26 
27  var $fields = array(
28  "id_fs",
29  "fsname",
30  "max_size",
31  "free_size",
32  "subdir_cnt_bydir",
33  "subdir_deep",
34  "max_entries_by_dir",
35  "r_path"
36  );
37  var $id_fields = array(
38  "id_fs"
39  );
40  var $dbtable_tmpl = "vaultdiskfs%s";
41  var $order_by = "";
42  var $seq_tmpl = "seq_id_vaultdiskfs%s";
44  create table vaultdiskfs%s ( id_fs int not null,
45  fsname text,
46  primary key (id_fs),
47  max_size int8,
48  free_size int8,
49  subdir_cnt_bydir int,
50  subdir_deep int,
51  max_entries_by_dir int,
52  r_path varchar(2048)
53  );
54  create sequence seq_id_vaultdiskfs%s start 10;";
55  // --------------------------------------------------------------------
56  function __construct($dbaccess, $id_fs = '')
57  {
58  // --------------------------------------------------------------------
59  $this->dbtable = sprintf($this->dbtable_tmpl, $this->specific);
60  $this->sqlcreate = sprintf($this->sqlcreate_tmpl, $this->specific, $this->specific);
61  $this->seq = sprintf($this->seq_tmpl, $this->specific);
62  parent::__construct($dbaccess, $id_fs);
63  }
64 
65  function createArch($maxsize, $path, $fsname = "-")
66  {
67  if (!is_dir($path)) $err = sprintf(_("%s directory not found") , $path);
68  elseif (!is_writable($path)) $err = sprintf(_("%s directory not writable") , $path);
69  if ($err == "") {
70  $this->fsname = $fsname;
71  $this->max_size = $maxsize;
72  $this->free_size = $maxsize;
73  $this->subdir_cnt_bydir = VAULT_MAXDIRBYDIR;
74  $this->subdir_deep = 1;
75  $this->max_entries_by_dir = VAULT_MAXENTRIESBYDIR;
76  $this->r_path = $path;
77  $err = $this->Add();
78  }
79  return $err;
80  }
81  // --------------------------------------------------------------------
82  function PreInsert()
83  {
84  // --------------------------------------------------------------------
85  if ($this->Exists($this->r_path)) return (_("File System already exists"));
86  $res = $this->exec_query("select nextval ('" . $this->seq . "')");
87  $arr = $this->fetch_array(0);
88  $this->id_fs = $arr["nextval"];
89  return '';
90  }
91  // --------------------------------------------------------------------
92  function Exists($path)
93  {
94  // --------------------------------------------------------------------
95  $query = new QueryDb($this->dbaccess, $this->dbtable);
96  $query->basic_elem->sup_where = array(
97  "r_path='" . $path . "'"
98  );
99  $t = $query->Query(0, 0, "TABLE");
100  return ($query->nb > 0);
101  }
102  // --------------------------------------------------------------------
103  function SetFreeFs($f_size, &$id_fs, &$id_dir, &$f_path, $fsname)
104  {
105  // --------------------------------------------------------------------
106  $id_fs = $id_dir = - 1;
107  $f_path = "";
108  $query = new QueryDb($this->dbaccess, $this->dbtable);
109  $qs = array();
110  $qs[0] = "free_size>" . $f_size;
111  if ($fsname != "") {
112  $qs[1] = "fsname='" . pg_escape_string($fsname) . "'";
113  }
114  $query->basic_elem->sup_where = $qs;
115  $t = $query->Query(0, 1, "TABLE");
116 
117  if ($query->nb > 0) {
118  $ifs = 0;
119  $dirfound = FALSE;
120  while (!$dirfound && ($ifs < $query->nb)) {
121  $sd = new VaultDiskDir($this->dbaccess, '', $this->specific);
122  $msg = $sd->SetFreeDir($t[$ifs]);
123  if ($msg == '') $dirfound = TRUE;
124  else $ifs++;
125  }
126  if ($dirfound) {
127  $this->Select($t[0]["id_fs"]);
128  $id_fs = $this->id_fs;
129  $id_dir = $sd->id_dir;
130  $f_path = $this->r_path . "/" . $sd->l_path;
131  } else {
132  return ($msg);
133  }
134  unset($t);
135  } else {
136  return (_("no empty vault file system found"));
137  }
138  return "";
139  }
140  // --------------------------------------------------------------------
141  function Show($id_fs, $id_dir, &$f_path)
142  {
143  // --------------------------------------------------------------------
144  $query = new QueryDb($this->dbaccess, $this->dbtable);
145  $query->basic_elem->sup_where = array(
146  sprintf("id_fs=%d", $id_fs)
147  );
148  $t = $query->Query(0, 0, "TABLE");
149  if ($query->nb > 0) {
150  $sd = new VaultDiskDir($this->dbaccess, $id_dir, $this->specific);
151  if ($sd->IsAffected()) {
152  $f_path = $t[0]["r_path"] . "/" . $sd->l_path;
153  } else {
154  return (_("no vault directory found"));
155  }
156  } else {
157  return (_("no vault file system found"));
158  }
159  return '';
160  }
161  // --------------------------------------------------------------------
162  function AddEntry($fs)
163  {
164  // --------------------------------------------------------------------
165  $this->free_size = $this->free_size - $fs;
166  $err = $this->Modify();
167  }
168  // --------------------------------------------------------------------
169  function DelEntry($id_fs, $id_dir, $fs)
170  {
171  // --------------------------------------------------------------------
172  DbObj::Select($id_fs);
173  if ($this->IsAffected()) {
174  $this->free_size = $this->free_size + $fs;
175  $this->Modify();
176  $sd = new VaultDiskDir($this->dbaccess, $id_dir, $this->specific);
177  if ($sd->IsAffected()) {
178  $sd->DelEntry();
179  } else {
180  return (_("no vault directory found"));
181  }
182  } else {
183  return (_("no vault file system found"));
184  }
185  return '';
186  }
187  // --------------------------------------------------------------------
188  function Stats(&$s)
189  {
190  // --------------------------------------------------------------------
191  $query = new QueryDb($this->dbaccess, $this->dbtable);
192  $t = $query->Query(0, 0, "TABLE");
193  while ($query->nb > 0 && (list($k, $v) = each($t))) {
194  $s["fs$k"]["root_dir"] = $v["r_path"];
195  $s["fs$k"]["allowed_size"] = $v["max_size"];
196  $s["fs$k"]["free_size"] = $v["free_size"];
197  $sd = new VaultDiskDir($this->dbacces, '', $this->specific);
198  $s["fs$k"]["free_entries"] = $sd->FreeEntries($v["id_fs"]);
199  unset($sd);
200  }
201  return '';
202  }
203 }
204 ?>
← centre documentaire © anakeen - published under CC License - Dynacase