Getting a unique filename?

M

Michael Schuerig

I need to build a Zip-Archive with a unique name that is served by
Apache using its X-SendFile mechanism.

Tempfile doesn't appear to be suitable in this case as it unlinks its
managed file when the Tempfile object is finalized. In my current, wrong
code, I'm using Tempfile and the file is not deleted prematurely, but I
take this to be no more than luck.

There are two processes involved, the ruby process (Rails, actually)
writing the file and apache delivering it. Ruby only passes the file's
path to apache. The file's life has to be independent of the ruby
process from then on (and I have to delete it externally).

I could (ab)use Tempfile to write code like this, but that doesn't
strike me as very elegant

tf = Tempfile.new(['archive-', '.zip'])
tempname = tf.path
tf.close!

Is there a nicer way?

Michael
 
D

Daniel Berger

I need to build a Zip-Archive with a unique name that is served by
Apache using its X-SendFile mechanism.

Tempfile doesn't appear to be suitable in this case as it unlinks its
managed file when the Tempfile object is finalized. In my current, wrong
code, I'm using Tempfile and the file is not deleted prematurely, but I
take this to be no more than luck.

There are two processes involved, the ruby process (Rails, actually)
writing the file and apache delivering it. Ruby only passes the file's
path to apache. The file's life has to be independent of the ruby
process from then on (and I have to delete it externally).

I could (ab)use Tempfile to write code like this, but that doesn't
strike me as very elegant

=A0 tf =3D Tempfile.new(['archive-', '.zip'])
=A0 tempname =3D tf.path
=A0 tf.close!

Is there a nicer way?

require 'file/temp'

fh =3D File::Temp.new(false) # Keep the file around

Regards,

Dan
 
M

Michael Schuerig

Daniel said:
tf = Tempfile.new(['archive-', '.zip'])
tempname = tf.path
tf.close!

Is there a nicer way?

require 'file/temp'

fh = File::Temp.new(false) # Keep the file around

Thanks, that looks good, but unfortunately doesn't work for me. See
below for the trace. Apparently it stumbles because the content of
/usr/libc.so is not as expected. I'm running Debian/unstable amd64.

Michael


# /usr/lib/libc.so
/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily. */
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a AS_NEEDED ( /lib/ld-
linux-x86-64.so.2 ) )

$ rake test
(in /var/lib/gems/1.8/gems/file-temp-1.1.2)
/usr/bin/ruby1.8 -w -I"lib" "/usr/lib/ruby/1.8/rake/rake_test_loader.rb"
"test/test_file_temp.rb"
/var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:61:in `ffi_lib':
Could not open library 'libc': libc: cannot open shared object file: No
such file or directory. Could not open library 'libc.so':
/usr/lib/libc.so: invalid ELF header (LoadError)
from /var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:43:in
`map'
from /var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:43:in
`ffi_lib'
from ./lib/file/temp.rb:47
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in
`require'
from ./test/test_file_temp.rb:11
from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5:in `load'
from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5
from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5:in `each'
from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -w -I"lib"
"/usr/lib/ruby...]
 
D

Daniel Berger

Daniel said:
tf =3D Tempfile.new(['archive-', '.zip'])
tempname =3D tf.path
tf.close!
Is there a nicer way?
require 'file/temp'
fh =3D File::Temp.new(false) # Keep the file around

Thanks, that looks good, but unfortunately doesn't work for me. See
below for the trace. Apparently it stumbles because the content of
/usr/libc.so is not as expected. I'm running Debian/unstable amd64.

Michael

# /usr/lib/libc.so
/* GNU ld script
=A0 =A0Use the shared library, but some functions are only in
=A0 =A0the static library, so try that secondarily. =A0*/
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a =A0AS_NEEDED ( /lib/ld-
linux-x86-64.so.2 ) )

$ rake test
(in /var/lib/gems/1.8/gems/file-temp-1.1.2)
/usr/bin/ruby1.8 -w -I"lib" "/usr/lib/ruby/1.8/rake/rake_test_loader.rb"
"test/test_file_temp.rb"
/var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:61:in `ffi_lib':
Could not open library 'libc': libc: cannot open shared object file: No
such file or directory. Could not open library 'libc.so':
/usr/lib/libc.so: invalid ELF header (LoadError)
=A0 =A0 =A0 =A0 from /var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:= 43:in
`map'
=A0 =A0 =A0 =A0 from /var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:= 43:in
`ffi_lib'
=A0 =A0 =A0 =A0 from ./lib/file/temp.rb:47
=A0 =A0 =A0 =A0 from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
=A0 =A0 =A0 =A0 from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in
`require'
=A0 =A0 =A0 =A0 from ./test/test_file_temp.rb:11
=A0 =A0 =A0 =A0 from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5:in `loa= d'
=A0 =A0 =A0 =A0 from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5
=A0 =A0 =A0 =A0 from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5:in `eac= h'
=A0 =A0 =A0 =A0 from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -w -I"lib"
"/usr/lib/ruby...]

Oops. I've had the fix in git for a while now, just forgot to push it
out.

I've just pushed out 1.1.3. Try that.

Regards,

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top