Getting a unique filename?

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

On Sep 14, 4:00 am, Michael S. [email protected] wrote:

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?

require ‘file/temp’

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

Regards,

Dan

Daniel B. wrote:

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:inmap’
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:ingem_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:inload’
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…]

On Sep 14, 6:15 am, Michael S. [email protected] wrote:

OUTPUT_FORMAT(elf64-x86-64)
/usr/lib/libc.so: invalid ELF header (LoadError)
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:ineach’
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs