4

I'm completely stumped by this one. I'm trying to debug some code that creates a FirstRun file as a marker that the program has already done its first run setup. It tries to create this in the specified working directory, given by the absworkingdir variable. However, when it tries to create the file with File.new I get the following error

`initialize': No such file or directory '

Here's the relevant code:

#First run setup
puts workingdir#debug
absworkingdir = File.expand_path(workingdir)
puts absworkingdir#debug
if File.exist?("#{absworkingdir}/FirstRun.lock") == false
    puts "This appears to be the first run of FigShare Sync. We'll setup a few things."
    print "Where would you like to store settings and files? [~./figsharesync]: "
    @input = gets.chomp
    puts @input#debug
        if @input.empty? == false
            workingdir = @input
            absworkingdir = File.expand_path(workingdir)
        end
    print "Please enter OAuth consumer key: "
    consumerkey = gets.chomp
    print "Please enter OAuth consumer key secret: "
    consumersecret = gets.chomp
    print "Please enter OAuth access token: "
    accesstoken = gets.chomp
    print "Please enter OAuth access token secret: "
    accesstokensecret = gets.chomp
    puts "Great! we'll get running now..."
puts absworkingdir#debug
File.new("#{absworkingdir}/FirstRun.lock", "r")
end

Here's the output from the debugging lines:

~/.figsharesync/                                                                                                                                                                                                                        
/var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync                                                                                                                                                                
This appears to be the first run of FigShare Sync. We'll setup a few things.                                                                                                                                                            
Where would you like to store settings and files? [~./figsharesync]:                                                                                                                                                                    

Please enter OAuth consumer key:                                                                                                                                                                                                        
Please enter OAuth consumer key secret:                                                                                                                                                                                                 
Please enter OAuth access token:                                                                                                                                                                                                        
Please enter OAuth access token secret:                                                                                                                                                                                                 
Great! we'll get running now...                                                                                                                                                                                                         
/var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync                                                                                                                                                                
source/figsharesync2.rb:38:in `initialize': No such file or directory - /var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync/FirstRun.lock(Errno::ENOENT)                                                          
    from source/figsharesync2.rb:38:in `new'                                                                                                                                                                                        
    from source/figsharesync2.rb:38:in `<main>'     
user2276204
  • 257
  • 1
  • 3
  • 9
  • Tried suggestiongs from @JulienLanglois and iamnotmaynard to no avail. I still get the same "does not exist" error. – user2276204 Apr 24 '13 at 20:05
  • For anyone else having this issue, it is indeed a Cloud 9 issue. The terminal defaults to a folder within the `~/` usually a six digit number. E.g. `~/111111/` – user2276204 Apr 26 '13 at 22:15

4 Answers4

7

You want to use write mode with File.new, otherwise you're saying you want to open a non-existent file in read mode:

f = File.new("#{absworkingdir}/FirstRun.lock", "w")
f.close
Julien Langlois
  • 482
  • 3
  • 11
  • Ah, thats exactly what I was look for. Thanks! I assume I can also pass it `{}` to immediately close the file. Any reason I should use `File.open` over this method? – user2276204 Apr 24 '13 at 19:50
  • You are correct that you can just use File.open("#{absworkingdir}/FirstRun.lock", "w") {}, just not with File.new :) The former nets you one less variable, the latter is more explicit. – Julien Langlois Apr 24 '13 at 20:06
  • @user2276204, can you give a bit more detail about the issue (as I understand it is ongoing)? Are you sure everything is identical with the error? – Julien Langlois Apr 24 '13 at 20:22
  • The error message is identical to the one in the question. Could it have something to do with running it in a VM inside Cloud 9 IDE? – user2276204 Apr 24 '13 at 21:37
  • It might be, although I just tried a minimal example (File.new("#{File.expand_path(Dir.pwd)}/test.txt", 'w').close) which seemed to work just fine. (Cool site by the way, I had never heard of it). – Julien Langlois Apr 24 '13 at 22:04
  • It's possible their doing something weird with `/.*` directories since they use one to store version information. Yes, it is a cool site (created out of the ACE editor, and Mozilla Skywriter (then Bespin) project). Not my default IDE, but very useful when switching machines frequently. – user2276204 Apr 24 '13 at 22:39
  • File.new('~/1.txt',"w") returns cited error in the 3.0 irb. is behavior different in the interactive runtime environment or am i missing something? – Andrew Feb 03 '23 at 16:10
  • at least in the irb, you have to use single quotes for your options flags ('w' instead of "w") – Andrew Feb 03 '23 at 16:26
0

You're in an if-block whose condition is "if this file does not exist" and then you try to open the nonexistent file for reading. That obviously can't work. You probably want to open for writing instead of reading.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • Thanks for that. I was under the impression that `File.new` was for creating a new file. Is there something else I should be using? The problem with doing it outside the block is that in that case the file already exists, so I wouldn't need to create it. – user2276204 Apr 24 '13 at 19:44
0

You have no file named /var/lib/stickshift/5165dc1e4382ec92040001a8/app-root/data/.figsharesync/FirstRun.lock to open for reading. If you're trying to create a new file to write to, you need to use the mode "w" (write), which creates the file if it does not exist, instead of "r" (read), which requires the file to already exist.

See the docs for more info on the modes. Also http://www.ruby-doc.org/core-1.9.3/File.html.

0

Try using w+ as the write mode instead of just w:

File.open("out.txt", "w+") { |file| file.write("boo!") }

see: How to create a file in Ruby

Community
  • 1
  • 1
Siwei
  • 19,858
  • 7
  • 75
  • 95
  • 1
    `w` and `w+` both create a new writable file. The difference is, `w` opens write-only mode while `w+` opens read-write mode. http://stackoverflow.com/a/7915881 – jkdev Nov 19 '15 at 06:13