Mkdir fails if path has non-English characters sometimes


#1

I have this code:

def ProfileBuilder2.get_user_data()
temp_dir = ON_MAC ? (File.join(ENV[‘HOME’],“Library”,“Application Support”)) : ((ENV[‘APPDATA’]))
temp_dir = temp_dir.dup.force_encoding(“UTF-8”)
user_data = File.join(temp_dir,“ProfileBuilder”)
return user_data
end

Later on, this gets called:

USER_DATA = ProfileBuilder2.get_user_data()
Dir.mkdir(USER_DATA) unless FileTest.exist?(USER_DATA)

The mkdir line fails and I think it is because of the non-English characters in the username. This is the partial output.
Error: c:/users/oscar grönlund/appdata/roaming/sketchup/sketchup 2017/sketchup/plugins/dm_profilebuilder2/profilebuilder.rbe:897:in `mkdir’

I am confused because I thought my ‘force_encoding’ is supposed to take care of non-english characters. Is this a system-dependent error? Because I have only seen two reports of this issue and I am sure I have lots of other users that have non-English characters in their usernames. Is it related to Windows version perhaps?

Thanks


#2

Since you seem to be using the ‘encoding’ methods you can assume that the temp directory is got in an available method via:
temp_dir = Sketchup.temp_dir

You can force its encoding if you wish [but it should already be properly encoded ?]…
temp_dir = temp_dir.dup.force_encoding("UTF-8")

Then:
user_data = File.join(temp_dir, "ProfileBuilder")

Then after setting up USER_DATA - later on…
Dir.mkdir(USER_DATA) unless FileTest.exist?(USER_DATA)

Having non-ASCII characters in user-names was always a no-no… with the newer force_encoding methods available it is somewhat alleviated…

Can you get the problem user to check what their system text encoding is ?

e.g. temp_dir.encoding

It might be that there is no single step re-encoding… but perhaps two would do once you establish the real issue…
Some user’s PCs have unusual text encoding settings…


#3

Dale, on Discourse forums use triple backtick delimiter lines for code
(with optional language on opening delimiter. For plain text use “text” as a code language.) :
ie:

```ruby
# code here
```

see http://ruby-doc.org/core-2.2.4/Encoding.html class’ introduction for nifty class methods, like:

fse = Encoding::find("filesystem")

or

loce = Encoding::find("locale")

Also are you using a “magic comment” on the 1st line of your code files ?
You should, see the class introduction I link to above. It’s simply be:

# encoding: UTF-8

… but you need to actually save the file in that encoding via your favorite code editor. (The comment is a signal to the Ruby interpreter of how to treat all literal strings within the file.)


#4

If I had to guess, I’d wonder if the user installed SketchUp via right-click “Runs as administrator”.
Which might result in a permissions error on the %AppData% path.

Try this test:

File.writable? ENV["APPDATA"]

There are also a few other class methods for testing in the File class.

It has been reported that sometimes Windows caches updates from filesystem duties, and doesn’t report them immediately. (Waiting for idle time we suspect.) Manually I update file explorer windows by pressing the F5 [Refresh] key. But you may be able to force refreshes by asking the OS to provide a directory listing after calling the mkdir command. You can do so easily with %x strings (same as backtick strings. See Ruby literals doc.)

Ie:

%x(dir)

or

dir_listing = %x(dir).split("\n")

#5

Thanks guys. I’ll dig into this and report back later


#6

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.