check for and if not there, create direcory and file

C

charlie bowman

I'm making a little app that requires this folder .timeclock/filename

The first time the app runs I would like it to check to see if the
folder and file are there and if not, create them. Everything I've tried
crashes if the folder isn't there. Thanks for any help!
 
M

Matthew Smillie

I'm making a little app that requires this folder .timeclock/filename

The first time the app runs I would like it to check to see if the
folder and file are there and if not, create them. Everything I've
tried
crashes if the folder isn't there. Thanks for any help!

These methods might help you out with creating the directory:

irb(main):001:0> File.exist?(".timeclock")
=> false
irb(main):002:0> Dir.mkdir(".timeclock")
=> 0
irb(main):003:0> File.exist?(".timeclock")
=> true
irb(main):004:0> File.directory?(".timeclock")
=> true

From there you should be able to just open .timeclock/new_file as
one normally would.

matthew smillie.
 
C

charlie bowman

irb(main):002:0> Dir.mkdir(".timeclock")
won't the above faile once the directory is created. I need to run a
check each time the application is ran.
 
E

Eric Schwartz

charlie bowman said:
irb(main):002:0> Dir.mkdir(".timeclock")
won't the above faile once the directory is created. I need to run a
check each time the application is ran.

That is why you use the File.exist?(".timeclock") method first, and
only run the mkdir if the exist? method returns false.

-=Eric
 
L

Logan Capaldo

irb(main):002:0> Dir.mkdir(".timeclock")
won't the above faile once the directory is created. I need to run a
check each time the application is ran.

Yes, that's why you wrap it in an if (or unless) statement.

unless File.exist?(".timeclock")
Dir.mkdir(".timeclock")
end
# Optionally make sure .timeclock is a directory
unless File.directory?(".timeclock")
puts ".timeclock is a not a directory."
exit( 1 )
end
# do whatever.
 
K

konsu

hello,

may be it would fail. but you have the other method(s) that you can use to
check if the directory exists.

konstantin
 
K

konsu

hello,

a related question: how to make this atomic to avoid race conditions:

Dir.mkdir(".timeclock") unless File.exist?(".timeclock")

konstantin
 
C

charlie bowman

Thank you, that worked perfect.


Logan said:
Yes, that's why you wrap it in an if (or unless) statement.

unless File.exist?(".timeclock")
Dir.mkdir(".timeclock")
end
# Optionally make sure .timeclock is a directory
unless File.directory?(".timeclock")
puts ".timeclock is a not a directory."
exit( 1 )
end
# do whatever.
 
A

ara.t.howard

Yes, that's why you wrap it in an if (or unless) statement.

unless File.exist?(".timeclock")
Dir.mkdir(".timeclock")
end
# Optionally make sure .timeclock is a directory
unless File.directory?(".timeclock")
puts ".timeclock is a not a directory."
exit( 1 )
end
# do whatever.

one cannot both check and/or create a dir in an atomic fashion. in fact, one
cannot even create a dir in an atomic fashion on some filesystems. about the
best you can do is

class Dir
def self::create d
begin
mkdir d # always try to create it
rescue Errno::EEXIST
nil # ignore failure to do so
end
new d # this will throw an error if it does not exist!
end
end

regards.

-a
 
R

Ryan Leavengood

one cannot both check and/or create a dir in an atomic fashion. in fact,= one
cannot even create a dir in an atomic fashion on some filesystems. about= the
best you can do is

While this is true, I think it only matters when one is dealing with
multi-process and/or multi-threaded code, which I doubt the original
poster is. But I know that multi-processing is about all you do Ara,
so I guess it is hard to forget lessons like this! ;)

Ryan
 
A

ara.t.howard

While this is true, I think it only matters when one is dealing with
multi-process and/or multi-threaded code, which I doubt the original poster
is. But I know that multi-processing is about all you do Ara, so I guess it
is hard to forget lessons like this! ;)

heh - i probably do get a little pedantic with that stuff - but it's so hard
to debug i just try to avoid it all together! ;-)

-a
 
C

charlie bowman

Thanks for the tip! This is just a personal timekeeper for me. But I
will keep that in mind if I need to do any threaded apps.
 
K

konsu

hello,

heh - i probably do get a little pedantic with that stuff - but it's so
hard
to debug i just try to avoid it all together! ;-)

i think it is not possuble to avoid this problem in a web application which
caches its output in the server's filesystem. is it?

your code returns an error if it cannot create the file, how do you handle
this error? when trying to create a cache in a web application, would sleep
and retry strategy be reasonable here?

thanks
konstantin
 
J

Justin Collins

Ryan said:
While this is true, I think it only matters when one is dealing with
multi-process and/or multi-threaded code, which I doubt the original
poster is. But I know that multi-processing is about all you do Ara,
so I guess it is hard to forget lessons like this! ;)

Ryan
Not necessarily - the program could check for the existence of the
directory, then something else (another program, the user, etc.) could
create or remove the directory. Not very likely, but it could happen.
Probably not worth worrying about though, huh?

-Justin
 
J

Joel VanderWerf

konsu said:
hello,

a related question: how to make this atomic to avoid race conditions:

Dir.mkdir(".timeclock") unless File.exist?(".timeclock")

There is always:

require 'fileutils'
FileUtils.mkdir_p(".timeclock")
 
M

Matthew Smillie

hello,



i think it is not possuble to avoid this problem in a web
application which
caches its output in the server's filesystem. is it?

your code returns an error if it cannot create the file, how do you
handle
this error? when trying to create a cache in a web application,
would sleep
and retry strategy be reasonable here?

If it has to be a file, I would use unique names keyed to the session
id or user name or whatever you happen to be tracking.

Imagine trying to sleep & retry for even 10 concurrent requests. Ick.

matthew smillie.
 
S

Stefan Lang

hello,

a related question: how to make this atomic to avoid race
conditions:

Dir.mkdir(".timeclock") unless File.exist?(".timeclock")

####################################################################
class Dir
def ensure_dir(name)
Dir.mkdir name
rescue Errno::EEXIST
raise Errno::ENOTDIR, name, caller unless File.directory?(name)
end
end

Dir.ensure_dir ".timeclock"
####################################################################

Although I'm not sure how portable that is because
of Errno codes (I have tested on Linux).

Regards,
Stefan
 

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
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top