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!

···

--
Posted via http://www.ruby-forum.com/.

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

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 Smillie 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.

--
Posted via http://www.ruby-forum.com/.

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 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.

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" <cbowmanschool@yahoo.com> wrote in message
news:5d2bdb4af4e0aab57e66d7fe58f508ac@ruby-forum.com...

···

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 Smillie 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.

--
Posted via http://www.ruby-forum.com/.

charlie bowman <cbowmanschool@yahoo.com> 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

Thank you, that worked perfect.

Logan Capaldo 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.

--
Posted via http://www.ruby-forum.com/.

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 Tue, 31 Jan 2006, Logan Capaldo 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.

--
sleep is the best meditation. -- h.h. the 14th dali lama

hello,

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

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

konstantin

"Logan Capaldo" <logancapaldo@gmail.com> wrote in message
news:DFC45DE6-0CCF-40CE-A21B-8BCCE7674C71@gmail.com...

···

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.

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 1/30/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> 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

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

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

hello,

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

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

···

On Wednesday 01 February 2006 22:42, konsu wrote:
####################################################################
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

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

···

On Tue, 31 Jan 2006, Ryan Leavengood wrote:

On 1/30/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> 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:

--
sleep is the best meditation. -- h.h. the 14th dali lama

Ryan Leavengood wrote:

···

On 1/30/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> 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

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 Leavengood wrote:

On 1/30/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> 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

--
Posted via http://www.ruby-forum.com/.

Quoting Justin Collins <collinsj@seattleu.edu>:

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,

<ara.t.howard@noaa.gov> wrote in message
news:Pine.LNX.4.62.0601301323530.4780@harp.ngdc.noaa.gov...

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

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 Feb 1, 2006, at 21:43, konsu wrote:

hello,

<ara.t.howard@noaa.gov> wrote in message
news:Pine.LNX.4.62.0601301323530.4780@harp.ngdc.noaa.gov...

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?