Should have seen this coming - Where to write temp files?
-
OK, I have my plugins folder to be very write-friendly. I'm making a folder called "temp_model" in my plugin's installation root folder. But on my friend's Mac, he is getting a "permission denied" error when it tried to create the folder.
So how do I know where I can safely write temp folders and files on the different OS's?
-
All of those issues will disappear as soon as you install boot camp of the iMac
Windows 7 sp1 and OSX Snow Leopard works well together, I just installed it in my wife's iMac,
Otherwise try http://forums.macrumors.com/ its an invaluable source for technical issues. -
The correct answer (because your writing a plugin for the whole world,) is to use the same method that Google uses.
See "Tools/WebTextures/webtextures_loader.rb" : line 858
If the user has the extension loaded:
temp = defined?($wt_instance) ? $wt_instance.temp_directory() : nil unless temp ... find it yourself end
Actually, this method's code should be in "sketchup.rb" ...
and should set a global$TEMPDIR
-
Ok, I'll look into that. Thanks Dan!,
Chris
-
This is what I use:
TT::System.temp_path
http://www.thomthom.net/software/sketchup/tt_lib2/doc/TT/System.html#temp_path-class_method<span class="syntaxdefault"><br />def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">temp_path<br /> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">PLATFORM_IS_OSX</span><span class="syntaxkeyword">) ? </span><span class="syntaxdefault">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>
-
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.
Advertisement