Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.VaultDiskDir.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  * Directories for vault files
9  * @author Anakeen
10  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
11  * @package FDL
12  */
13 // ---------------------------------------------------------------
14 // $Id: Class.VaultDiskDir.php,v 1.10 2006/12/06 11:12:13 eric Exp $
15 // $Source: /home/cvsroot/anakeen/freedom/vault/Class/Class.VaultDiskDir.php,v $
16 // ---------------------------------------------------------------
17 //
18 // ---------------------------------------------------------------
19 include_once ("Class.QueryDb.php");
20 
21 define("VAULT_MAXENTRIESBYDIR", 1000);
22 define("VAULT_MAXDIRBYDIR", 100);
23 class VaultDiskDir extends DbObj
24 {
25 
26  public $fields = array(
27  "id_dir",
28  "id_fs",
29  "free_entries",
30  "l_path"
31  );
32  public $id_fields = array(
33  "id_dir"
34  );
35  public $dbtable_tmpl = "vaultdiskdir%s";
36  public $order_by = "";
37  public $seq_tmpl = "seq_id_vaultdiskdir%s";
38  public $sqlcreate_tmpl = "
39  create table vaultdiskdir%s ( id_dir int not null,
40  primary key (id_dir),
41  id_fs int,
42  free_entries int,
43  l_path varchar(2048)
44  );
45  create sequence seq_id_vaultdiskdir%s start 10";
46  // --------------------------------------------------------------------
47  function __construct($dbaccess, $id_dir = '', $def = '')
48  {
49  // --------------------------------------------------------------------
50  $this->specific = $def;
51  $this->dbtable = sprintf($this->dbtable_tmpl, $this->specific);
52  $this->sqlcreate = sprintf($this->sqlcreate_tmpl, $this->specific, $this->specific);
53  $this->seq = sprintf($this->seq_tmpl, $this->specific);
54 
55  parent::__construct($dbaccess, $id_dir);
56  }
57  /**
58  * return name of next directory
59  * 1/1 => 1/2
60  * 1/10 => 2/1
61  * 1/2 = 1/3
62  */
63  function nextdir($d, $max = VAULT_MAXDIRBYDIR)
64  {
65  $td = explode('/', $d);
66  $dend = intval(end($td));
67 
68  $lastkey = end(array_keys($td));
69  if ($dend < $max) {
70  $td[$lastkey]++;
71  } else {
72  $good = false;;
73  $key = $lastkey;
74  while (($key >= 0) && (!$good)) {
75  $prev = intval(prev($td));
76  $td[$key] = 1;
77  $key--;
78  if ($prev) {
79  if ($prev < $max) {
80  $td[$key]++;
81  $good = true;
82  }
83  }
84  }
85  if (!$good) $td = array_fill(0, count($td) + 1, 1);
86  }
87  return implode('/', $td);
88  }
89  // --------------------------------------------------------------------
90  function SetFreeDir($fs)
91  {
92  // --------------------------------------------------------------------
93  $query = new QueryDb($this->dbaccess, $this->dbtable);
94  $id_fs = $fs["id_fs"];
95  $query->basic_elem->sup_where = array(
96  "id_fs=" . $id_fs,
97  "free_entries>0"
98  );
99  $t = $query->Query(0, 0, "TABLE");
100  if ($query->nb > 0) {
101  $this->Select($t[0]["id_dir"]);
102  unset($t);
103  $this->free_entries--;
104  $this->Modify();
105  } else {
106  $t = $query->Query(0, 0, "TABLE", "SELECT * from vaultdiskdirstorage where id_fs=" . intval($id_fs) . " order by id_dir desc limit 1");
107  $lpath = $t[0]["l_path"];
108  $npath = $this->nextdir($lpath);
109  $rpath = $fs["r_path"];
110 
111  $this->id_dir = "";
112  $this->id_fs = $id_fs;
113  $this->l_path = $npath;
114  $this->free_entries = VAULT_MAXENTRIESBYDIR;
115  $this->free_entries--;
116  $err = $this->Add();
117  if ($err == "") {
118  mkdir($rpath . "/" . $npath, VAULT_DMODE);
119  chown($rpath . "/" . $npath, HTTP_USER);
120  chgrp($rpath . "/" . $npath, HTTP_USER);
121  } else {
122  error_log("Vault dirs full");
123  return (_("no empty vault dir found") . $err);
124  }
125  }
126  return "";
127  }
128  // --------------------------------------------------------------------
129  function PreInsert()
130  {
131  // --------------------------------------------------------------------
132  if ($this->Exists($this->l_path, $this->id_fs)) return (_("Directory already exists"));
133  $res = $this->exec_query("select nextval ('" . $this->seq . "')");
134  $arr = $this->fetch_array(0);
135  $this->id_dir = $arr["nextval"];
136  return '';
137  }
138  // --------------------------------------------------------------------
139  function Exists($path, $id_fs)
140  {
141  // --------------------------------------------------------------------
142  $query = new QueryDb($this->dbaccess, $this->dbtable);
143  $query->basic_elem->sup_where = array(
144  "l_path='" . $path . "'",
145  "id_fs=" . $id_fs
146  );
147  $t = $query->Query(0, 0, "TABLE");
148  return ($query->nb > 0);
149  }
150  // --------------------------------------------------------------------
151  function DelEntry()
152  {
153  // --------------------------------------------------------------------
154  $this->free_entries+= 1;
155  $this->Modify();
156  }
157  // --------------------------------------------------------------------
158  function FreeEntries($id_fs)
159  {
160  // --------------------------------------------------------------------
161  $free_entries = 0;
162  $query = new QueryDb($this->dbaccess, $this->dbtable);
163  $query->basic_elem->sup_where = array(
164  "free_entries>0",
165  "id_fs=" . $id_fs
166  );
167  $t = $query->Query(0, 0, "TABLE");
168  while ($query->nb > 0 && (list($k, $v) = each($t))) $free_entries+= $v["free_entries"];
169  unset($t);
170  return ($free_entries);
171  }
172 }
173 ?>
← centre documentaire © anakeen - published under CC License - Dynacase