Forum: Ruby check for and if not there, create direcory and file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Charlie B. (Guest)
on 2006-01-30 18:39
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!
Matthew S. (Guest)
on 2006-01-30 19:04
(Received via mailing list)
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.
Charlie B. (Guest)
on 2006-01-30 19:08
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.
Logan C. (Guest)
on 2006-01-30 19:19
(Received via mailing list)
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.
Charlie B. (Guest)
on 2006-01-30 19:27
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.
unknown (Guest)
on 2006-01-30 22:46
(Received via mailing list)
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
Ryan L. (Guest)
on 2006-01-30 22:46
(Received via mailing list)
On 1/30/06, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> 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! ;)

Ryan
unknown (Guest)
on 2006-01-30 22:46
(Received via mailing list)
On Tue, 31 Jan 2006, Ryan L. wrote:

> On 1/30/06, removed_email_address@domain.invalid <removed_email_address@domain.invalid> 
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! ;)

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
Charlie B. (Guest)
on 2006-01-30 23:11
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, removed_email_address@domain.invalid <removed_email_address@domain.invalid> 
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! ;)
>
> 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
Justin C. (Guest)
on 2006-02-01 07:38
(Received via mailing list)
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! ;)
>
>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
unknown (Guest)
on 2006-02-01 18:11
(Received via mailing list)
Quoting Justin C. <removed_email_address@domain.invalid>:

> 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
konsu (Guest)
on 2006-02-02 00:12
(Received via mailing list)
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" <removed_email_address@domain.invalid> wrote in message
news:removed_email_address@domain.invalid...
Eric S. (Guest)
on 2006-02-02 00:15
(Received via mailing list)
charlie bowman <removed_email_address@domain.invalid> 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
konsu (Guest)
on 2006-02-02 00:15
(Received via mailing list)
hello,

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

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

konstantin


"Logan C." <removed_email_address@domain.invalid> wrote in message
news:removed_email_address@domain.invalid...
konsu (Guest)
on 2006-02-02 00:22
(Received via mailing list)
hello,

<removed_email_address@domain.invalid> wrote in message
news:removed_email_address@domain.invalid...
>
> 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
Joel VanderWerf (Guest)
on 2006-02-02 02:18
(Received via mailing list)
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")
Matthew S. (Guest)
on 2006-02-02 02:36
(Received via mailing list)
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.
Stefan L. (Guest)
on 2006-02-04 14:00
(Received via mailing list)
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
This topic is locked and can not be replied to.