ia64/xen-unstable

view tools/python/xen/util/mkdir.py @ 12725:36fe7ca48e54

Tidy up the creation of directories that Xend needs. This avoids potential
races in this creation.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Fri Dec 01 11:32:32 2006 +0000 (2006-12-01)
parents
children
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU Lesser General Public License as published by
4 # the Free Software Foundation; either version 2.1 of the License, or
5 # (at your option) any later version.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public License
13 # along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #============================================================================
16 # Copyright (c) 2006 XenSource Inc.
17 #============================================================================
19 import errno
20 import os
21 import os.path
22 import stat
25 def parents(dir, perms, enforcePermissions = False):
26 """
27 Ensure that the given directory exists, creating it if necessary, but not
28 complaining if it's already there.
30 @param dir The directory name.
31 @param perms One of the stat.S_ constants.
32 @param enforcePermissions Enforce our ownership and the given permissions,
33 even if the directory pre-existed with different ones.
34 """
35 # Catch the exception here, rather than checking for the directory's
36 # existence first, to avoid races.
37 try:
38 os.makedirs(dir, perms)
39 except OSError, exn:
40 if exn.args[0] != errno.EEXIST or not os.path.isdir(dir):
41 raise
42 if enforcePermissions:
43 os.chown(dir, os.geteuid(), os.getegid())
44 os.chmod(dir, stat.S_IRWXU)