Check for and if not there, create direcory and file

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!

On Jan 30, 2006, at 16:39, charlie bowman wrote:

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.

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.

Matthew S. wrote:

On Jan 30, 2006, at 16:39, charlie bowman wrote:

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.

On Jan 30, 2006, at 12:09 PM, charlie bowman wrote:

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.

Thank you, that worked perfect.

Logan C. wrote:

On Jan 30, 2006, at 12:09 PM, charlie bowman wrote:

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.

On Tue, 31 Jan 2006, Logan C. wrote:

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

On 1/30/06, [email protected] [email protected] wrote:

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! :wink:

Ryan

On Tue, 31 Jan 2006, Ryan L. wrote:

On 1/30/06, [email protected] [email protected] wrote:

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! :wink:

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! :wink:

-a

Ryan L. wrote:

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! :wink:

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

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.

unknown wrote:

On Tue, 31 Jan 2006, Ryan L. wrote:

On 1/30/06, [email protected] [email protected] wrote:

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! :wink:

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! :wink:

-a

hello,

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

konstantin

“charlie bowman” [email protected] wrote in message
news:[email protected]

Quoting Justin C. [email protected]:

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?

It’s probably worth working out what a sane way to fail in that case
would be; stuff like that can have security implications.

-mental

hello,

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

Dir.mkdir(“.timeclock”) unless File.exist?(“.timeclock”)

konstantin

“Logan C.” [email protected] wrote in message
news:[email protected]

charlie bowman [email protected] writes:

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

hello,

[email protected] wrote in message
news:[email protected]

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! :wink:

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

konsu wrote:

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")

On Feb 1, 2006, at 21:43, konsu wrote:

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.

On Wednesday 01 February 2006 22:42, konsu wrote:

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