Instantly share code, notes, and snippets.

Embed
What would you like to do?
[SOLVED] Drush building from /dev/stdin doesn't work on some systems

Building from /dev/stdin work fine on Macbook (useful to use sed to edit stream).

➜  2ndleveldeep git:(develop) ✗ cat << EOH >> /tmp/test.make
heredoc> core = 7.x
heredoc> api = 2
heredoc> 
heredoc> projects[] = drupal
heredoc> EOH
➜  2ndleveldeep git:(develop) ✗ cat /tmp/test.make | drush make /dev/stdin /tmp/test
drupal-7.14 downloaded.
➜  2ndleveldeep git:(develop) ✗ php -v
PHP 5.3.8 (cli) (built: Dec  5 2011 21:24:09) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
➜  2ndleveldeep git:(develop) ✗ drush --version
drush version 5.3
➜  2ndleveldeep git:(develop) ✗ uname -a
Darwin Patrick-Connollys-MacBook-Pro.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64

But running on a Lucid64 vagrant box doesn't work (same for 5.0):

vagrant@lucid64:~$ cat << EOH >> /tmp/test.make
> core = 7.x
> api = 2
> 
> projects[] = drupal
> EOH
vagrant@lucid64:~$ cat /tmp/test.make | drush make /dev/stdin /tmp/test
file_get_contents(/dev/stdin): failed to open stream: No such file or directory
make.utilities.inc:449                                              [warning]
Invalid or empty make file: /dev/stdin                              [error]
vagrant@lucid64:~$ php -v
PHP 5.3.2-1ubuntu4.17 with Suhosin-Patch (cli) (built: Jun 19 2012 03:21:35) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
vagrant@lucid64:~$ drush --version
drush version 5.4
vagrant@lucid64:~$ uname -a
Linux lucid64 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
@patcon

This comment has been minimized.

Owner

patcon commented Jun 29, 2012

OK, so seems it's a change in how PHP works :(

Success

➜  php-src git:(master) php -v
PHP 5.3.8 (cli) (built: Dec  5 2011 21:24:09) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
➜  php-src git:(master) echo "dog" | php -r "echo file_get_contents('/dev/stdin');"
dog
➜  php-src git:(master) 

Fail

vagrant@lucid64:~$ php -v
PHP 5.3.2-1ubuntu4.17 with Suhosin-Patch (cli) (built: Jun 19 2012 03:21:35) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
vagrant@lucid64:~$ echo "dog" | php -r "echo file_get_contents('/dev/stdin');"
PHP Warning:  file_get_contents(/dev/stdin): failed to open stream: No such file or directory in Command line code on line 1
vagrant@lucid64:~$ 

I'm sure there could be other places where it's getting borked, but here are the places for obvious changes:

➜  php-src git:(master) git diff php-5.3.2 php-5.3.8 ext/standard/file.c
[...]
@@ -538,6 +543,10 @@ PHP_FUNCTION(file_get_contents)
                return;
        }

+       if (strlen(filename) != filename_len) {
+               RETURN_FALSE;
+       }
+
        if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "length must be greater than or equal to zero");
                RETURN_FALSE;
@@ -558,7 +567,6 @@ PHP_FUNCTION(file_get_contents)
                RETURN_FALSE;
        }

-       /* uses mmap if possible */
        if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) {

                if (PG(magic_quotes_runtime)) {
[ ... ]
➜  php-src git:(master) 

For the record, strlen('/dev/null') returns 10 in both 5.3.2 and 5.3.8.

@patcon

This comment has been minimized.

Owner

patcon commented Jun 29, 2012

Ended up having NOTHING to do with PHP versions. Still not sure why it worked on 5.3.8 on Mac, but not the same version on lucid, but found an alternative:

cat /tmp/test.make | drush make php://stdin /tmp/test

This works since file_get_contents('php://stdin') doesn't fail like it does with /dev/stdin.

Reference: http://stackoverflow.com/questions/3249427/how-to-process-stdin-to-stdout-in-php

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