Calum Lind : Fix #1963 : Unhandled OSError if permission issue creating default config dir

5 views
Skip to first unread message

g...@deluge-torrent.org

unread,
Dec 17, 2012, 9:13:32 PM12/17/12
to deluge...@googlegroups.com
Module: deluge
Branch: master
Commit: 7c2725acdc0867c1297c89233c834f0726b35b39

Author: Calum Lind <calumlin...@gmail.com>
Date: Mon Dec 10 19:01:05 2012 +0000

Fix #1963 : Unhandled OSError if permission issue creating default config dir

The fix now logs the error and exits. To log the error also required moving
the logger setup code to before any default_config_dir call.

---

deluge/common.py | 48 ++++++++++++++++++++++++------------------------
deluge/main.py | 47 +++++++++++++++++++++--------------------------
2 files changed, 45 insertions(+), 50 deletions(-)

diff --git a/deluge/common.py b/deluge/common.py
index eaaa3b1..b68c1c7 100644
--- a/deluge/common.py
+++ b/deluge/common.py
@@ -37,17 +37,23 @@
"""Common functions for various parts of Deluge to use."""

import os
+import sys
import time
import subprocess
import platform
import chardet
import logging
+import pkg_resources
+import gettext
+import locale

try:
import json
except ImportError:
import simplejson as json

+from deluge.error import *
+
log = logging.getLogger(__name__)

# Do a little hack here just in case the user has json-py installed since it
@@ -65,13 +71,6 @@ if not hasattr(json, "dumps"):
json.dump = dump
json.load = load

-import pkg_resources
-import gettext
-import locale
-import sys
-
-from deluge.error import *
-
LT_TORRENT_STATE = {
"Queued": 0,
"Checking": 1,
@@ -91,7 +90,6 @@ LT_TORRENT_STATE = {
7: "Checking Resume Data"
}

-
TORRENT_STATE = [
"Allocating",
"Checking",
@@ -135,24 +133,26 @@ def get_default_config_dir(filename=None):
:rtype: string

"""
+
if windows_check():
- appDataPath = os.environ.get("APPDATA")
- if not appDataPath:
- import _winreg
- hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders")
- appDataReg = _winreg.QueryValueEx(hkey, "AppData")
- appDataPath = appDataReg[0]
- _winreg.CloseKey(hkey)
- if filename:
- return os.path.join(appDataPath, "deluge", filename)
- else:
- return os.path.join(appDataPath, "deluge")
+ def save_config_path(resource):
+ appDataPath = os.environ.get("APPDATA")
+ if not appDataPath:
+ import _winreg
+ hkey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders")
+ appDataReg = _winreg.QueryValueEx(hkey, "AppData")
+ appDataPath = appDataReg[0]
+ _winreg.CloseKey(hkey)
+ return os.path.join(appDataPath, resource)
else:
- import xdg.BaseDirectory
- if filename:
- return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename)
- else:
- return xdg.BaseDirectory.save_config_path("deluge")
+ from xdg.BaseDirectory import save_config_path
+ if not filename:
+ filename = ''
+ try:
+ return os.path.join(save_config_path("deluge"), filename)
+ except OSError, e:
+ log.error("Unable to use default config directory, exiting... (%s)", e)
+ sys.exit(1)

def get_default_download_dir():
"""
diff --git a/deluge/main.py b/deluge/main.py
index 422b661..a1525df 100644
--- a/deluge/main.py
+++ b/deluge/main.py
@@ -43,8 +43,10 @@
import os
import sys
from optparse import OptionParser
+from logging import FileHandler, getLogger
+from errno import EEXIST

-import deluge.log
+from deluge.log import setupLogger
import deluge.error

def version_callback(option, opt_str, value, parser):
@@ -88,19 +90,16 @@ def start_ui():
# Get the options and args from the OptionParser
(options, args) = parser.parse_args(deluge.common.unicode_argv()[1:])

+ # Setup the logger
if options.quiet:
options.loglevel = "none"
-
if options.loglevel:
options.loglevel = options.loglevel.lower()
-
logfile_mode = 'w'
if options.rotate_logs:
logfile_mode = 'a'
-
- # Setup the logger
- deluge.log.setupLogger(level=options.loglevel, filename=options.logfile,
- filemode=logfile_mode)
+ setupLogger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode)
+ log = getLogger(__name__)

if options.config:
if not os.path.exists(options.config):
@@ -110,7 +109,7 @@ def start_ui():
except Exception, e:
pass
elif not os.path.isdir(options.config):
- print "Config option needs to be a directory!"
+ log.error("Config option needs to be a directory!")
sys.exit(1)
else:
if not os.path.exists(deluge.common.get_default_config_dir()):
@@ -129,8 +128,6 @@ def start_ui():

version = deluge.common.get_version()

- import logging
- log = logging.getLogger(__name__)
log.info("Deluge ui %s", version)
log.debug("options: %s", options)
log.debug("args: %s", args)
@@ -188,27 +185,35 @@ this should be an IP address", metavar="IFACE",
# Get the options and args from the OptionParser
(options, args) = parser.parse_args()

+ # Setup the logger
if options.quiet:
options.loglevel = "none"
-
+ if options.logfile:
+ # Try to create the logfile's directory if it doesn't exist
+ try:
+ os.makedirs(os.path.abspath(os.path.dirname(options.logfile)))
+ except OSError, e:
+ if e.errno != EEXIST:
+ print "There was an error creating the log directory, exiting... (%s)" % e
+ sys.exit(1)
logfile_mode = 'w'
if options.rotate_logs:
logfile_mode = 'a'
-
- # Setup the logger
- deluge.log.setupLogger(level=options.loglevel, filename=options.logfile,
- filemode=logfile_mode)
+ setupLogger(level=options.loglevel, filename=options.logfile, filemode=logfile_mode)
+ log = getLogger(__name__)

import deluge.configmanager
if options.config:
if not deluge.configmanager.set_config_dir(options.config):
- print("There was an error setting the config dir! Exiting..")
+ log.error("There was an error setting the config directory! Exiting...")
sys.exit(1)

# Sets the options.logfile to point to the default location
def open_logfile():
if not options.logfile:
options.logfile = deluge.configmanager.get_config_dir("deluged.log")
+ file_handler = FileHandler(options.logfile)
+ log.addHandler(file_handler)

# Writes out a pidfile if necessary
def write_pidfile():
@@ -242,16 +247,6 @@ this should be an IP address", metavar="IFACE",

open_logfile()

- # Setup the logger
- try:
- # Try to make the logfile's directory if it doesn't exist
- os.makedirs(os.path.abspath(os.path.dirname(options.logfile)))
- except:
- pass
-
- import logging
- log = logging.getLogger(__name__)
-
if options.profile:
import hotshot
hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile"))

g...@deluge-torrent.org

unread,
Dec 17, 2012, 9:13:32 PM12/17/12
to deluge...@googlegroups.com
Module: deluge
Branch: 1.3-stable
Commit: 1dd078f4f1d3a35b293c305e53a35d6b1e82d739

Author: Calum Lind <calumlin...@gmail.com>
Date: Mon Dec 10 19:01:05 2012 +0000

Fix #1963 : Unhandled OSError if permission issue creating default config dir

The fix now logs the error and exits. To log the error also required moving
the logger setup code to before any default_config_dir call.

---

deluge/common.py | 28 +++++++++++++++-------------
deluge/main.py | 46 +++++++++++++++++++++++++---------------------
2 files changed, 40 insertions(+), 34 deletions(-)

diff --git a/deluge/common.py b/deluge/common.py
index 63c364d..033bbd2 100644
--- a/deluge/common.py
+++ b/deluge/common.py
@@ -42,12 +42,18 @@ import subprocess
import platform
import sys
import chardet
+import pkg_resources
+import gettext
+import locale

try:
import json
except ImportError:
import simplejson as json

+from deluge.error import *
+from deluge.log import LOG as log
+
# Do a little hack here just in case the user has json-py installed since it
# has a different api
if not hasattr(json, "dumps"):
@@ -63,10 +69,6 @@ if not hasattr(json, "dumps"):
json.dump = dump
json.load = load

-import pkg_resources
-import gettext
-import locale
-
# Initialize gettext
try:
if hasattr(locale, "bindtextdomain"):
@@ -75,14 +77,11 @@ try:
locale.textdomain("deluge")
gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n"), unicode=True)
except Exception, e:
- from deluge.log import LOG as log
log.error("Unable to initialize gettext/locale!")
log.exception(e)
import __builtin__
__builtin__.__dict__["_"] = lambda x: x

-from deluge.error import *
-
LT_TORRENT_STATE = {
"Queued": 0,
"Checking": 1,
@@ -102,7 +101,6 @@ LT_TORRENT_STATE = {
7: "Checking Resume Data"
}

-
TORRENT_STATE = [
"Allocating",
"Checking",
@@ -159,11 +157,15 @@ def get_default_config_dir(filename=None):
else:
return os.path.join(appDataPath, "deluge")
else:
- import xdg.BaseDirectory
- if filename:
- return os.path.join(xdg.BaseDirectory.save_config_path("deluge"), filename)
- else:
- return xdg.BaseDirectory.save_config_path("deluge")
+ from xdg.BaseDirectory import save_config_path
+ try:
+ if filename:
+ return os.path.join(save_config_path("deluge"), filename)
+ else:
+ return save_config_path("deluge")
+ except OSError, e:
+ log.error("Unable to use default config directory, exiting... (%s)", e)
+ sys.exit(1)

def get_default_download_dir():
"""
diff --git a/deluge/main.py b/deluge/main.py
index d7f3df2..f8953d2 100644
--- a/deluge/main.py
+++ b/deluge/main.py
@@ -42,6 +42,8 @@
import os
import sys
from optparse import OptionParser
+from logging import FileHandler
+from errno import EEXIST

import deluge.log
import deluge.common
@@ -86,6 +88,14 @@ def start_ui():
# Get the options and args from the OptionParser
(options, args) = parser.parse_args()

+ # Setup the logger
+ if options.quiet:
+ options.loglevel = "none"
+ if options.loglevel:
+ options.loglevel = options.loglevel.lower()
+ deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
+ from deluge.log import LOG as log
+
if options.config:
if not os.path.exists(options.config):
# Try to create the config folder if it doesn't exist
@@ -94,7 +104,7 @@ def start_ui():
except Exception, e:
pass
elif not os.path.isdir(options.config):
- print "Config option needs to be a directory!"
+ log.error("Config option needs to be a directory!")
sys.exit(1)
else:
if not os.path.exists(deluge.common.get_default_config_dir()):
@@ -110,18 +120,8 @@ def start_ui():
print "The default UI has been changed to", options.default_ui
sys.exit(0)

- if options.quiet:
- options.loglevel = "none"
-
- if options.loglevel:
- options.loglevel = options.loglevel.lower()
-
- # Setup the logger
- deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
-
version = deluge.common.get_version()

- from deluge.log import LOG as log
log.info("Deluge ui %s", version)
log.debug("options: %s", options)
log.debug("args: %s", args)
@@ -170,18 +170,31 @@ this should be an IP address", metavar="IFACE",
# Get the options and args from the OptionParser
(options, args) = parser.parse_args()

+ # Setup the logger
if options.quiet:
options.loglevel = "none"
+ if options.logfile:
+ # Try to create the logfile's directory if it doesn't exist
+ try:
+ os.makedirs(os.path.abspath(os.path.dirname(options.logfile)))
+ except OSError, e:
+ if e.errno != EEXIST:
+ print "There was an error creating the log directory, exiting... (%s)" % e
+ sys.exit(1)
+ deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
+ from deluge.log import LOG as log

if options.config:
if not deluge.configmanager.set_config_dir(options.config):
- print "There was an error setting the config dir! Exiting.."
+ log.error("There was an error setting the config directory! Exiting...")
sys.exit(1)

# Sets the options.logfile to point to the default location
def open_logfile():
if not options.logfile:
options.logfile = deluge.configmanager.get_config_dir("deluged.log")
+ file_handler = FileHandler(options.logfile)
+ log.addHandler(file_handler)

# Writes out a pidfile if necessary
def write_pidfile():
@@ -208,15 +221,6 @@ this should be an IP address", metavar="IFACE",
# Do not daemonize
write_pidfile()

- # Setup the logger
- try:
- # Try to make the logfile's directory if it doesn't exist
- os.makedirs(os.path.abspath(os.path.dirname(options.logfile)))
- except:
- pass
- deluge.log.setupLogger(level=options.loglevel, filename=options.logfile)
- from deluge.log import LOG as log
Reply all
Reply to author
Forward
0 new messages