Blame view

sources/lib/private/files/mount/mount.php 4 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
  <?php
  /**
   * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
31b7f2792   Kload   Upgrade to ownclo...
8
  namespace OC\Files\Mount;
03e52840d   Kload   Init
9

31b7f2792   Kload   Upgrade to ownclo...
10
11
12
  use \OC\Files\Filesystem;
  use OC\Files\Storage\Loader;
  use OC\Files\Storage\Storage;
03e52840d   Kload   Init
13

31b7f2792   Kload   Upgrade to ownclo...
14
  class Mount {
03e52840d   Kload   Init
15
16
17
  	/**
  	 * @var \OC\Files\Storage\Storage $storage
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
18
19
20
21
22
  	protected $storage = null;
  	protected $class;
  	protected $storageId;
  	protected $arguments = array();
  	protected $mountPoint;
03e52840d   Kload   Init
23
24
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
25
26
27
28
29
  	 * @var \OC\Files\Storage\Loader $loader
  	 */
  	private $loader;
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
30
  	 * @param string|\OC\Files\Storage\Storage $storage
03e52840d   Kload   Init
31
  	 * @param string $mountpoint
31b7f2792   Kload   Upgrade to ownclo...
32
33
  	 * @param array $arguments (optional)\
  	 * @param \OC\Files\Storage\Loader $loader
03e52840d   Kload   Init
34
  	 */
31b7f2792   Kload   Upgrade to ownclo...
35
  	public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
03e52840d   Kload   Init
36
37
38
  		if (is_null($arguments)) {
  			$arguments = array();
  		}
31b7f2792   Kload   Upgrade to ownclo...
39
40
41
42
43
  		if (is_null($loader)) {
  			$this->loader = new Loader();
  		} else {
  			$this->loader = $loader;
  		}
03e52840d   Kload   Init
44

31b7f2792   Kload   Upgrade to ownclo...
45
46
  		$mountpoint = $this->formatPath($mountpoint);
  		if ($storage instanceof Storage) {
03e52840d   Kload   Init
47
  			$this->class = get_class($storage);
31b7f2792   Kload   Upgrade to ownclo...
48
  			$this->storage = $this->loader->wrap($mountpoint, $storage);
03e52840d   Kload   Init
49
50
51
52
53
54
55
56
57
  		} else {
  			// Update old classes to new namespace
  			if (strpos($storage, 'OC_Filestorage_') !== false) {
  				$storage = '\OC\Files\Storage\\' . substr($storage, 15);
  			}
  			$this->class = $storage;
  			$this->arguments = $arguments;
  		}
  		$this->mountPoint = $mountpoint;
03e52840d   Kload   Init
58
59
60
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
61
62
  	 * get complete path to the mount point, relative to data/
  	 *
03e52840d   Kload   Init
63
64
65
66
67
68
69
  	 * @return string
  	 */
  	public function getMountPoint() {
  		return $this->mountPoint;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  	 * get name of the mount point
  	 *
  	 * @return string
  	 */
  	public function getMountPointName() {
  		return basename(rtrim($this->mountPoint, '/'));
  	}
  
  	/**
  	 * @param string $mountPoint new mount point
  	 */
  	public function setMountPoint($mountPoint) {
  		$this->mountPoint = $mountPoint;
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
86
87
  	 * create the storage that is mounted
  	 *
03e52840d   Kload   Init
88
89
90
91
92
  	 * @return \OC\Files\Storage\Storage
  	 */
  	private function createStorage() {
  		if (class_exists($this->class)) {
  			try {
31b7f2792   Kload   Upgrade to ownclo...
93
  				return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
03e52840d   Kload   Init
94
  			} catch (\Exception $exception) {
6d9380f96   Cédric Dupont   Update sources OC...
95
96
97
98
99
100
  				if ($this->mountPoint === '/') {
  					// the root storage could not be initialized, show the user!
  					throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception);
  				} else {
  					\OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR);
  				}
03e52840d   Kload   Init
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
  				return null;
  			}
  		} else {
  			\OC_Log::write('core', 'storage backend ' . $this->class . ' not found', \OC_Log::ERROR);
  			return null;
  		}
  	}
  
  	/**
  	 * @return \OC\Files\Storage\Storage
  	 */
  	public function getStorage() {
  		if (is_null($this->storage)) {
  			$this->storage = $this->createStorage();
  		}
  		return $this->storage;
  	}
  
  	/**
  	 * @return string
  	 */
  	public function getStorageId() {
  		if (!$this->storageId) {
  			if (is_null($this->storage)) {
  				$storage = $this->createStorage(); //FIXME: start using exceptions
  				if (is_null($storage)) {
  					return null;
  				}
  				$this->storage = $storage;
  			}
  			$this->storageId = $this->storage->getId();
  			if (strlen($this->storageId) > 64) {
  				$this->storageId = md5($this->storageId);
  			}
  		}
  		return $this->storageId;
  	}
  
  	/**
  	 * @param string $path
  	 * @return string
  	 */
  	public function getInternalPath($path) {
  		if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
  			$internalPath = '';
  		} else {
  			$internalPath = substr($path, strlen($this->mountPoint));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
149
150
  		// substr returns false instead of an empty string, we always want a string
  		return (string)$internalPath;
03e52840d   Kload   Init
151
152
153
154
155
156
  	}
  
  	/**
  	 * @param string $path
  	 * @return string
  	 */
31b7f2792   Kload   Upgrade to ownclo...
157
  	private function formatPath($path) {
03e52840d   Kload   Init
158
159
160
161
162
163
164
165
  		$path = Filesystem::normalizePath($path);
  		if (strlen($path) > 1) {
  			$path .= '/';
  		}
  		return $path;
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
166
  	 * @param callable $wrapper
03e52840d   Kload   Init
167
  	 */
31b7f2792   Kload   Upgrade to ownclo...
168
  	public function wrapStorage($wrapper) {
6d9380f96   Cédric Dupont   Update sources OC...
169
  		$this->storage = $wrapper($this->mountPoint, $this->getStorage());
03e52840d   Kload   Init
170
171
  	}
  }