Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Nextcloud
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Nextcloud
Commits
48fe85d9
Commit
48fe85d9
authored
13 years ago
by
Robin Appelman
Browse files
Options
Downloads
Patches
Plain Diff
add streamwrapper that provides a callback on stream close
parent
e8afe4f1
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/base.php
+1
-0
1 addition, 0 deletions
lib/base.php
lib/streamwrappers.php
+86
-0
86 additions, 0 deletions
lib/streamwrappers.php
tests/lib/streamwrappers.php
+31
-0
31 additions, 0 deletions
tests/lib/streamwrappers.php
with
118 additions
and
0 deletions
lib/base.php
+
1
−
0
View file @
48fe85d9
...
...
@@ -148,6 +148,7 @@ class OC{
require_once
(
'streamwrappers.php'
);
stream_wrapper_register
(
"fakedir"
,
"OC_FakeDirStream"
);
stream_wrapper_register
(
'static'
,
'OC_StaticStreamWrapper'
);
stream_wrapper_register
(
'close'
,
'OC_CloseStreamWrapper'
);
// calculate the documentroot
OC
::
$DOCUMENTROOT
=
realpath
(
$_SERVER
[
'DOCUMENT_ROOT'
]);
...
...
This diff is collapsed.
Click to expand it.
lib/streamwrappers.php
+
86
−
0
View file @
48fe85d9
...
...
@@ -218,3 +218,89 @@ class OC_StaticStreamWrapper {
return
false
;
}
}
/**
* stream wrapper that provides a callback on stream close
*/
class
OC_CloseStreamWrapper
{
public
static
$callBacks
=
array
();
private
$path
=
''
;
private
$source
;
private
static
$open
=
array
();
public
function
stream_open
(
$path
,
$mode
,
$options
,
&
$opened_path
){
$path
=
substr
(
$path
,
strlen
(
'close://'
));
$this
->
path
=
$path
;
$this
->
source
=
fopen
(
$path
,
$mode
);
if
(
is_resource
(
$this
->
source
)){
$this
->
meta
=
stream_get_meta_data
(
$this
->
source
);
}
self
::
$open
[]
=
$path
;
return
is_resource
(
$this
->
source
);
}
public
function
stream_seek
(
$offset
,
$whence
=
SEEK_SET
){
fseek
(
$this
->
source
,
$offset
,
$whence
);
}
public
function
stream_tell
(){
return
ftell
(
$this
->
source
);
}
public
function
stream_read
(
$count
){
return
fread
(
$this
->
source
,
$count
);
}
public
function
stream_write
(
$data
){
return
fwrite
(
$this
->
source
,
$data
);
}
public
function
stream_set_option
(
$option
,
$arg1
,
$arg2
){
switch
(
$option
){
case
STREAM_OPTION_BLOCKING
:
stream_set_blocking
(
$this
->
source
,
$arg1
);
break
;
case
STREAM_OPTION_READ_TIMEOUT
:
stream_set_timeout
(
$this
->
source
,
$arg1
,
$arg2
);
break
;
case
STREAM_OPTION_WRITE_BUFFER
:
stream_set_write_buffer
(
$this
->
source
,
$arg1
,
$arg2
);
}
}
public
function
stream_stat
(){
return
fstat
(
$this
->
source
);
}
public
function
stream_lock
(
$mode
){
flock
(
$this
->
source
,
$mode
);
}
public
function
stream_flush
(){
return
fflush
(
$this
->
source
);
}
public
function
stream_eof
(){
return
feof
(
$this
->
source
);
}
public
function
url_stat
(
$path
)
{
$path
=
substr
(
$path
,
strlen
(
'close://'
));
if
(
file_exists
(
$path
)){
return
stat
(
$path
);
}
else
{
return
false
;
}
}
public
function
stream_close
(){
fclose
(
$this
->
source
);
if
(
isset
(
self
::
$callBacks
[
$this
->
path
])){
call_user_func
(
self
::
$callBacks
[
$this
->
path
],
$this
->
path
);
}
}
public
function
unlink
(
$path
){
$path
=
substr
(
$path
,
strlen
(
'close://'
));
return
unlink
(
$path
);
}
}
This diff is collapsed.
Click to expand it.
tests/lib/streamwrappers.php
+
31
−
0
View file @
48fe85d9
...
...
@@ -44,4 +44,35 @@ class Test_StreamWrappers extends UnitTestCase {
clearstatcache
();
$this
->
assertFalse
(
file_exists
(
$staticFile
));
}
public
function
testCloseStream
(){
//ensure all basic stream stuff works
$sourceFile
=
OC
::
$SERVERROOT
.
'/tests/data/lorem.txt'
;
$tmpFile
=
OC_Helper
::
TmpFile
(
'.txt'
);
$file
=
'close://'
.
$tmpFile
;
$this
->
assertTrue
(
file_exists
(
$file
));
file_put_contents
(
$file
,
file_get_contents
(
$sourceFile
));
$this
->
assertEqual
(
file_get_contents
(
$sourceFile
),
file_get_contents
(
$file
));
unlink
(
$file
);
clearstatcache
();
$this
->
assertFalse
(
file_exists
(
$file
));
//test callback
$tmpFile
=
OC_Helper
::
TmpFile
(
'.txt'
);
$file
=
'close://'
.
$tmpFile
;
OC_CloseStreamWrapper
::
$callBacks
[
$tmpFile
]
=
array
(
'Test_StreamWrappers'
,
'closeCallBack'
);
$fh
=
fopen
(
$file
,
'w'
);
fwrite
(
$fh
,
'asd'
);
try
{
fclose
(
$fh
);
$this
->
fail
(
'Expected exception'
);
}
catch
(
Exception
$e
){
$path
=
$e
->
getMessage
();
$this
->
assertEqual
(
$path
,
$tmpFile
);
}
}
public
static
function
closeCallBack
(
$path
){
throw
new
Exception
(
$path
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment