Permalink
Browse files

implement quota, fixes #35

  • Loading branch information...
1 parent 989ee25 commit 3ef08a28b5db63fae0746e800d62f0f406133dd2 @mikey179 committed Aug 9, 2012
View
@@ -4,6 +4,7 @@
vfsStream now supports touch(), chown(), chgrp() and chmod()
- implemented issue #33: add support for stream_truncate
(provided by https://github.com/nikcorg)
+- implemented issue #35: size limit (quota) for VFS
1.0.0, 2012-05-15
@@ -0,0 +1,87 @@
+<?php
+/**
+ * This file is part of vfsStream.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @package org\bovigo\vfs
+ */
+namespace org\bovigo\vfs;
+/**
+ * Represents a quota for disk space.
+ *
+ * @since 1.1.0
+ * @internal
+ */
+class Quota
+{
+ /**
+ * unlimited quota
+ */
+ const UNLIMITED = -1;
+ /**
+ * quota in bytes
+ *
+ * A value of -1 is treated as unlimited.
+ *
+ * @type int
+ */
+ private $amount;
+
+ /**
+ * constructor
+ *
+ * @param int $amount quota in bytes
+ */
+ public function __construct($amount)
+ {
+ $this->amount = $amount;
+ }
+
+ /**
+ * create with unlimited space
+ *
+ * @return Quota
+ */
+ public static function unlimited()
+ {
+ return new self(self::UNLIMITED);
+ }
+
+ /**
+ * checks if a quota is set
+ *
+ * @return bool
+ */
+ public function isLimited()
+ {
+ return self::UNLIMITED < $this->amount;
+ }
+
+ /**
+ * checks if given used space exceeda quota limit
+ *
+ *
+ * @param int $usedSpace
+ * @return int
+ */
+ public function spaceLeft($usedSpace)
+ {
+ if (self::UNLIMITED === $this->amount) {
+ return $usedSpace;
+ }
+
+ if ($usedSpace >= $this->amount) {
+ return 0;
+ }
+
+ $spaceLeft = $this->amount - $usedSpace;
+ if (0 >= $spaceLeft) {
+ return 0;
+ }
+
+ return $spaceLeft;
+ }
+}
+?>
@@ -19,31 +19,31 @@ class vfsStream
/**
* url scheme
*/
- const SCHEME = 'vfs';
+ const SCHEME = 'vfs';
/**
* owner: root
*/
- const OWNER_ROOT = 0;
+ const OWNER_ROOT = 0;
/**
* owner: user 1
*/
- const OWNER_USER_1 = 1;
+ const OWNER_USER_1 = 1;
/**
* owner: user 2
*/
- const OWNER_USER_2 = 2;
+ const OWNER_USER_2 = 2;
/**
* group: root
*/
- const GROUP_ROOT = 0;
+ const GROUP_ROOT = 0;
/**
* group: user 1
*/
- const GROUP_USER_1 = 1;
+ const GROUP_USER_1 = 1;
/**
* group: user 2
*/
- const GROUP_USER_2 = 2;
+ const GROUP_USER_2 = 2;
/**
* initial umask setting
*
@@ -374,5 +374,16 @@ public static function inspect(vfsStreamVisitor $visitor, vfsStreamContent $cont
return $visitor->visitDirectory($root);
}
+
+ /**
+ * sets quota to given amount of bytes
+ *
+ * @param int $bytes
+ * @since 1.1.0
+ */
+ public static function setQuota($bytes)
+ {
+ vfsStreamWrapper::setQuota(new Quota($bytes));
+ }
}
?>
@@ -59,6 +59,12 @@ class vfsStreamWrapper
*/
protected static $root;
/**
+ * disk space quota
+ *
+ * @type Quota
+ */
+ private static $quota;
+ /**
* file mode: read only, write only, all
*
* @type int
@@ -96,7 +102,8 @@ class vfsStreamWrapper
*/
public static function register()
{
- self::$root = null;
+ self::$root = null;
+ self::$quota = Quota::unlimited();
if (true === self::$registered) {
return;
}
@@ -131,6 +138,17 @@ public static function getRoot()
}
/**
+ * sets quota for disk space
+ *
+ * @param Quota $quota
+ * @since 1.1.0
+ */
+ public static function setQuota(Quota $quota)
+ {
+ self::$quota = $quota;
+ }
+
+ /**
* returns content for given path
*
* @param string $path
@@ -390,6 +408,10 @@ public function stream_write($data)
return 0;
}
+ if (self::$quota->isLimited()) {
+ $data = substr($data, 0, self::$quota->spaceLeft(self::$root->sizeSummarized()));
+ }
+
return $this->content->write($data);
}
@@ -414,6 +436,17 @@ public function stream_truncate($size)
return false;
}
+ if (self::$quota->isLimited() && $this->content->size() < $size) {
+ $maxSize = self::$quota->spaceLeft(self::$root->sizeSummarized());
+ if (0 === $maxSize) {
+ return false;
+ }
+
+ if ($size > $maxSize) {
+ $size = $maxSize;
+ }
+ }
+
return $this->content->truncate($size);
}
@@ -0,0 +1,81 @@
+<?php
+/**
+ * This file is part of vfsStream.
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @package org\bovigo\vfs
+ */
+namespace org\bovigo\vfs;
+/**
+ * Test for org\bovigo\vfs\Quota.
+ *
+ * @group issue_35
+ */
+class QuotaTestCase extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * instance to test
+ *
+ * @type Quota
+ */
+ private $quota;
+
+ /**
+ * set up test environment
+ */
+ public function setUp()
+ {
+ $this->quota = new Quota(10);
+ }
+
+ /**
+ * @test
+ */
+ public function unlimitedQuotaIsNotLimited()
+ {
+ $this->assertFalse(Quota::unlimited()->isLimited());
+ }
+
+ /**
+ * @test
+ */
+ public function limitedQuotaIsLimited()
+ {
+ $this->assertTrue($this->quota->isLimited());
+ }
+
+ /**
+ * @test
+ */
+ public function unlimitedQuotaHasAlwaysSpaceLeft()
+ {
+ $this->assertEquals(303, Quota::unlimited()->spaceLeft(303));
+ }
+
+ /**
+ * @test
+ */
+ public function hasNoSpaceLeftWhenUsedSpaceIsLargerThanQuota()
+ {
+ $this->assertEquals(0, $this->quota->spaceLeft(11));
+ }
+
+ /**
+ * @test
+ */
+ public function hasNoSpaceLeftWhenUsedSpaceIsEqualToQuota()
+ {
+ $this->assertEquals(0, $this->quota->spaceLeft(10));
+ }
+
+ /**
+ * @test
+ */
+ public function hasSpaceLeftWhenUsedSpaceIsLowerThanQuota()
+ {
+ $this->assertEquals(1, $this->quota->spaceLeft(9));
+ }
+}
+?>
Oops, something went wrong.

0 comments on commit 3ef08a2

Please sign in to comment.