Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream_get_meta_data returns an empty uri for tmpfile() streams #2336

Closed
stof opened this issue Apr 5, 2014 · 4 comments
Closed

stream_get_meta_data returns an empty uri for tmpfile() streams #2336

stof opened this issue Apr 5, 2014 · 4 comments

Comments

@stof
Copy link
Contributor

stof commented Apr 5, 2014

The assetic testsuite is currently failing 1 test on HHVM on Travis: https://travis-ci.org/kriswallsmith/assetic/jobs/22333581
When looking at the failure, I see that the command being executed has an empty argument for the --flagfile option. In the test, this option is set by getting the uri of a tmp file with stream_get_meta_data. It looks like it returns an empty string in the uri key instead of returning the path to the file created by tmpfile.

the code of the test is https://github.com/kriswallsmith/assetic/blob/master/tests/Assetic/Test/Filter/GoogleClosure/CompilerJarFilterTest.php#L105-112

@JoelMarcey JoelMarcey added this to the Lockdown milestone May 12, 2014
@fredemmott fredemmott self-assigned this May 12, 2014
@fredemmott
Copy link
Contributor

Simpler test case:

<?php
$f = tmpfile();
var_dump(stream_get_meta_data($f)['uri']);

@fredemmott fredemmott removed their assignment May 12, 2014
@fredemmott fredemmott removed this from the Lockdown milestone May 12, 2014
@fredemmott
Copy link
Contributor

This isn't quite as trivial as it sounds - we use the c tmpfile() call which gives us an fd, not a filename. This is better ins ome ways (it removes a race condition), but makes it hard to implement this.

@LiraNuna
Copy link
Contributor

It's possible but that will only work on Linux, using fcntl and F_GETPATH.

@sgolemon
Copy link
Contributor

sgolemon commented Nov 6, 2014

Yeah, tmpfile() resources don't necessarily have paths. On linux this seems to be commonly implemented by creating a file and immediately unlinking it (meaning the open file handle is the only reference to it), it literally doesn't have an entry on the file system. If you need a file path you really should use tempnam() and open/close/unlink the file explicitly.

You could do something like the following (untested):

function tmpfilewithname(&$name, $prefix = '') {
  $name = tempnam(sys_get_tmp_dir(), $prefix);
  $fp = fopen($name, 'w+');
  register_shutdown_function(function() use ($fp, $name) {
    fclose($fp);
    unlink($name);
  });
  return $fp;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants