##// END OF EJS Templates
avoid race condition in profile creation...
MinRK -
Show More
@@ -87,22 +87,55 b' class ProfileDir(LoggingConfigurable):'
87 87
88 88 def _log_dir_changed(self, name, old, new):
89 89 self.check_log_dir()
90
91 def _mkdir(self, path, mode=None):
92 """ensure a directory exists at a given path
93
94 This is a version of os.mkdir, with the following differences:
95
96 - returns True if it created the directory, False otherwise
97 - ignores EEXIST, protecting against race conditions where
98 the dir may have been created in between the check and
99 the creation
100 - sets permissions if requested and the dir already exists
101 """
102 if os.path.exists(path):
103 if mode and os.stat(path).st_mode != mode:
104 try:
105 os.chmod(path, mode)
106 except OSError:
107 self.log.warn(
108 "Could not set permissions on %s",
109 path
110 )
111 return False
112 try:
113 if mode:
114 os.mkdir(path, mode)
115 else:
116 os.mkdir(path)
117 except OSError as e:
118 if e.errno == errno.EEXIST:
119 return False
120 else:
121 raise
122
123 return True
90 124
91 125 def check_log_dir(self):
92 if not os.path.isdir(self.log_dir):
93 os.mkdir(self.log_dir)
126 self._mkdir(self.log_dir)
94 127
95 128 def _startup_dir_changed(self, name, old, new):
96 129 self.check_startup_dir()
97 130
98 131 def check_startup_dir(self):
99 if not os.path.isdir(self.startup_dir):
100 os.mkdir(self.startup_dir)
132 self._mkdir(self.startup_dir)
133
101 134 readme = os.path.join(self.startup_dir, 'README')
102 135 src = os.path.join(get_ipython_package_dir(), u'config', u'profile', u'README_STARTUP')
103 136
104 137 if not os.path.exists(src):
105 self.log.warn("Could not copy README_STARTUP to startup dir. Source file %s does not exist." % src)
138 self.log.warn("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src)
106 139
107 140 if os.path.exists(src) and not os.path.exists(readme):
108 141 shutil.copy(src, readme)
@@ -111,25 +144,13 b' class ProfileDir(LoggingConfigurable):'
111 144 self.check_security_dir()
112 145
113 146 def check_security_dir(self):
114 if not os.path.isdir(self.security_dir):
115 os.mkdir(self.security_dir, 0o700)
116 else:
117 try:
118 os.chmod(self.security_dir, 0o700)
119 except OSError:
120 self.log.warn("Could not set security dir permissions to private.")
147 self._mkdir(self.security_dir, 0o40700)
121 148
122 149 def _pid_dir_changed(self, name, old, new):
123 150 self.check_pid_dir()
124 151
125 152 def check_pid_dir(self):
126 if not os.path.isdir(self.pid_dir):
127 os.mkdir(self.pid_dir, 0o700)
128 else:
129 try:
130 os.chmod(self.pid_dir, 0o700)
131 except OSError:
132 self.log.warn("Could not set pid dir permissions to private.")
153 self._mkdir(self.pid_dir, 0o40700)
133 154
134 155 def check_dirs(self):
135 156 self.check_security_dir()
General Comments 0
You need to be logged in to leave comments. Login now