Should have seen this coming - Where to write temp files?
-
I stand by my answer.
The method I point to is more elegant, and makes sure that the dir is writeable by the current user.
-
BUT because the 'TEMP' directory IS the current user's temporary directory it should be 'writable' by that user, unless of course some permissions are serious foobar - but then nothing else needing a temporary directory would work either ?!
I prefer to use a variant of thomthom's code [I just dislike the neat but non-obvious '? :' syntax...]
tempdir=ENV["TMPDIR"] if not tempdir=ENV["TEMP"]
So 'tempdir' is the path to the 'temp' directory for that user - on PC or MAC - the right one is always selected to suit...
To make the 'full path' to a temporary file use...
tempfile=File.join(tempdir, tempfilename)
-
@dan rathbun said:
I stand by my answer.
The method I point to is more elegant, and makes sure that the dir is writeable by the current user.
But it requires webtextures to be enabled... which is not a guaranty.
@tig said:
[I just dislike the neat but non-obvious '? :' syntax...]
How about this? (probably better than sniffing OS first.)
<span class="syntaxdefault"><br />def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">temp_path<br /> ENV</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'TMPDIR'</span><span class="syntaxkeyword">] || </span><span class="syntaxdefault">ENV</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'TEMP'</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">end<br /></span>
-
@tig said:
[I just dislike the neat but non-obvious '? :' syntax...]
The ternary is a pretty common operator...
-
I know about the 'alternative' ternary syntax tests.
But your '||' is the much same as my 'if' test... but not so 'obvious' to a casual reader.
Call me old fashioned... it's just that I prefer to be able to 'read' it in 'English' - just a bit - therefore I prefer 'if'... probably goes back to Applescripting where you write the whole thing in 'English'... 'if the file named "xxx" exists then open...' etc... -
@thomthom said:
@dan rathbun said:
The method I point to is more elegant, and makes sure that the dir is writeable by the current user.
But it requires webtextures to be enabled... which is not a guaranty.
So copy it into "sketchup.rb" or into your own module.
ENV
vars can be changed... by another plugin.. by a user who doesn't know better.. whatever.Making an assumption that the path will be writable (without testing to be sure,) just sets you up for a future fall. And then you'll have to issue a update for your plugin, and waste a bunch of time placating angry customers.
-
So here's what I've come up with. I did like Dan just suggested in the above post - I just copied their method and put it into my module I'm making. This is what I am using:
tmp = "" for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], ENV['USERPROFILE'], '/tmp'] if dir and File.directory?(dir) and File.writable?(dir) tmp = File.join( dir, 'temp_model') break end end @@temp_dir = File.expand_path(tmp) Dir.mkdir( @@temp_dir, 777 )
It is working great on y PC, but the guy testing it on a Mac is getting permission denied errors. Any good reason he should be getting an error?
Also, is there any way that the above script will ever not find a writable directory?
Chris
-
@chris fullmer said:
Also, is there any way that the above script will ever not find a writable directory?
On Mac, if somebody wipes out the
ENV['TEMPDIR']
value, AND the directory'/tmp'
does not exist, then after thefor
loop,tmp
would be empty.
Make sure to add:
` if tmp.empty?make a tempTemp dir below HOME dir
ie: delete the dir when done.
end`
ENV['USERPROFILE']
is Windows only.
On Mac, it'sENV['HOME']
.Myself.. I "hate" when programs write crap into my home dir.
I'd prefer a "/temp" subdir if that was the only way to solve the issue. -
There's also a
File.writable_real?(dir
) method.
Returns true if the named file is writable by the real user id of this process. -
What does the "real user" mean. I saw that in the docs but didn't understand the difference between the "effective user" and the "real user".
Also, another thing I am doing in my script. I find their ENV[temp] directory as I showed in my example script. And then I write my own subdir called temp_model and then I try to write files to that subdir. When I make my subdir, I am giving it 777 permissions
mkdir( mydir_string, 777 )
. That should in theory be alloweable right? My subdir should have all the right permissions for me to write to it? -
@unknownuser said:
(http://en.wikipedia.org/wiki/Chmod)":1q6nz2xj]The chmod command accepts up to four digits to represent an octal number. The octets refer to bits applied to the file owner, group and other users, respectively. Use of three digits is discouraged because it leaves the fourth as the default and this value is not fixed.
You can try:
"777".oct
see: String.octalso see: cacls for NT
-
777
is a decimal number.0o777
is an octal number.0xDEADBEEF
is a hexidecimal number. and0b111111111
is a binary.The file permission is a bit field - 3 sets of 3 bits.
If you want to set all 9 bits to 1, use 0o777 because:
0b111_111_111.to_s(8) 777
(You could just use 0b111111111 instead of 0o777, too)
Want to set just the owner to full rights, and deny eveyone else? Use 0700:
0b111000000.to_s(8) 700
-
@chris fullmer said:
What does the "real user" mean. I saw that in the docs but didn't understand the difference between the "effective user" and the "real user".
see the setuid link (below)
@unknownuser said:
(http://en.wikipedia.org/wiki/Chmod#Special_modes)":2b9d9a8z]Special modes
The chmod command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use s to represent the setuid and setgid modes,See also:
Ruby methods:
File.setuid? File.setgid?
At the console:
"%b" % "777".oct
111111111
-> rwxrwxrwx -
@jim said:
777
is a decimal number.0777
is an octal number.0xDEADBEEF
is a hexidecimal number. and0b111111111
is a binary. Enter these in the Ruby Console to see.I could not remember how to specify octals (which is why I went with the
String
method,) until you posted, reminding me of the "0x
" and "0b
" prefixes.0o777
511
so:
"%b" % 0o777
-
i tend to do write temp content next to the Sketchup file and in the end just delete it again.
that location should be writable for sure. -
Yeah, I'm writing a LOT of files and they are persistant while the model is open. So I could have a few thousand files xluttering up someone's desktop hehhe. Might not go over well.
I still can't figure out why my Mac tester's computer won't let me write to his temp folder. It lets me create my folder, but then won't let me write to it.....grr. Oh yes, though come to think of it, he has not tested my new version using the different permission settings the Jim and Dan suggested. I better get him to try that soon.
-
And you may have set the "special" bits, by accident, when you sent int(777) instead of int(511)...
.. I suggest having the tester, totally delete the folder manually, before trying the new version.
-
Ahh, good point.
Thanks so much everyone for all the help always. Its nice to be able to help people from time to and time, and even nicer when there are amazing people out there who are willing and able to help when needed. Very grateful, thanks Dan (et al)
Chris
-
Just noticed ANOTHER standard Ruby extension that bears on this subject.
With a path to the local standard Ruby lib dir pushed into the
$LOAD_PATH
array, you add the following to your module:
require('tmpdir.rb')
This standard extension adds the class methods:
Dir::tmpdir
andDir::mktmpdir
If you read the file, you'll recognize the
Dir::tmpdir
method as the 'progenitor' of the edition in the Google 'webtextures_loader.rb' file. (Google "lifted" it, and stripped out the Win32 specific part.)So (above) when we talked about whether to rely on the WebTextures plugin, my advice is, to instead rely on the Standard Ruby library.
Advertisement