Skip to content
mikey179 edited this page Aug 25, 2012 · 3 revisions

Introduction

Since version 0.4.0 vfsStream supports file modes as well as users and groups for both files and directories.

Purpose

While not being complete file mode support allows to create tests which verify that classes using the file system behave correctly when they check files or directories with is_readable(), is_writable() or is_executable().

File modes

File modes for new directories and files

Newly created files receive the file mode 0666, while newly created directories receive the file mode 0777. An exception to this are directories created with mkdir(), they will receive the file mode set with mkdir().

Since version 0.8.0 the default file mode for new directories and files can be changed using vfsStream::umask(), see below.

Setting other file modes than the default ones

On creation of files and directories

Each method which creates a file or directory does not only take the name of the file or directory to create but additionally as optional parameter the file mode the new file or directory should receive:

new vfsStreamFile('foo.txt', 0644);

new vfsStreamDirectory('bar', 0755);

vfsStream::newFile('foo.txt', 0644);

vfsStream::newDirectory('bar', 0755);

If a chain of directories is created with vfsStream::newDirectory('bar/baz', 0755); all directories in the chain receive the same file mode.

On existing instances

On existing file and directory instances simply call the vfsStreamContent::chmod() method:

$vfsStreamFile->chmod(0664);

$vfsStreamDirectory->chmod(0700);

vfsStream and umask()

The umask setting is ignored by vfsStream. However, starting with version 0.8.0 vfsStream provides its own umask mechanism. To enable this the method vfsStream::umask() can be used. It works the same way as PHP's native umask() function, but of course only applies to vfsStream URLs.

Users and groups

By default new files and directories will be owned by the current user and group of the process the test is running in. On Windows both will be 0 (meaning root) by default.

Change user or group

To change the user or group of a file or directory use the instance methods vfsStreamContent::chown() or vfsStreamContent::chgrp():

$vfsStreamFile->chown(vfsStream::OWNER_USER_2);

$vfsStreamDirectory->chown(vfsStream::OWNER_ROOT);

$vfsStreamFile->chgrp(vfsStream::GROUP_USER_2);

$vfsStreamDirectory->chgrp(vfsStream::GROUP_ROOT);

The vfsStream class defines several constants which can be used for more clarity:

vfsStream::OWNER_ROOT, vfsStream::OWNER_USER_1, vfsStream::OWNER_USER_2, vfsStream::GROUP_ROOT, vfsStream::GROUP_USER_1, vfsStream::GROUP_USER_2

vfsStream and chmod()

Only applies to versions < 1.1.0 or PHP < 5.4

Because chmod() only works with real files and PHP stream wrappers provide no way to set the file mode chmod() can not be used with vfsStream URLs. Doing so will return false from the function and yield a warning chmod(): No such file or directory with PHP < 5.3, and chmod(): Invalid argument with PHP >= 5.3.

vfsStream and chown() and chgrp()

Only applies to versions < 1.1.0 or PHP < 5.4

Both chown() and chgrp() can not be applied to a vfsStream URL because PHP stream wrappers provide no way to set the file owner with chown() or group with chgrp(). Both functions will return false if applied to a vfsStream URL, on Linux system they additionally will yield a warning.