mirror of
https://github.com/gnustep/libs-base.git
synced 2025-06-03 01:50:55 +00:00
Path handling cleanup.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@12905 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
79f0881b0d
commit
e469947f56
2 changed files with 85 additions and 132 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
2002-02-28 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
* Source/NSUser.m: rewrite user and defaults root code to standardise
|
||||||
|
and to use .GNUsteprc configuration. Also permit forcing of fixed
|
||||||
|
roots at compile time.
|
||||||
|
|
||||||
Thu Feb 28 16:46:31 2002 Nicola Pero <n.pero@mi.flashnet.it>
|
Thu Feb 28 16:46:31 2002 Nicola Pero <n.pero@mi.flashnet.it>
|
||||||
|
|
||||||
* Source/NSUser.m (NSSearchPathForDirectoriesInDomains): Changed
|
* Source/NSUser.m (NSSearchPathForDirectoriesInDomains): Changed
|
||||||
|
|
211
Source/NSUser.m
211
Source/NSUser.m
|
@ -80,13 +80,9 @@ GSSetUserName(NSString* name)
|
||||||
if ([theUserName isEqualToString: name] == NO)
|
if ([theUserName isEqualToString: name] == NO)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* We must ensure that userDirectory() has been called to set
|
* We can destroy the cached user path so that next time
|
||||||
* up the template user paths from the environment variables.
|
* anything wants it, it will be regenerated.
|
||||||
* Then we can destroy the cached user path so that next time
|
|
||||||
* anything wants it, it will be regenerated from the template
|
|
||||||
* and the new user details.
|
|
||||||
*/
|
*/
|
||||||
userDirectory(theUserName, YES);
|
|
||||||
DESTROY(gnustep_user_root);
|
DESTROY(gnustep_user_root);
|
||||||
/*
|
/*
|
||||||
* Next we can set up the new user name, and reset the user defaults
|
* Next we can set up the new user name, and reset the user defaults
|
||||||
|
@ -107,6 +103,10 @@ GSSetUserName(NSString* name)
|
||||||
* are usually the same). If you have a setuid program and want to
|
* are usually the same). If you have a setuid program and want to
|
||||||
* change the user to reflect the uid, use GSSetUserName()
|
* change the user to reflect the uid, use GSSetUserName()
|
||||||
*/
|
*/
|
||||||
|
/* NOTE FOR DEVELOPERS.
|
||||||
|
* If you change the behavior of this method you must also change
|
||||||
|
* user_home.c in the makefiles package to match.
|
||||||
|
*/
|
||||||
NSString *
|
NSString *
|
||||||
NSUserName(void)
|
NSUserName(void)
|
||||||
{
|
{
|
||||||
|
@ -210,6 +210,10 @@ GSStringFromWin32EnvironmentVariable(const char * envVar)
|
||||||
/**
|
/**
|
||||||
* Returns loginName's home directory as an NSString object.
|
* Returns loginName's home directory as an NSString object.
|
||||||
*/
|
*/
|
||||||
|
/* NOTE FOR DEVELOPERS.
|
||||||
|
* If you change the behavior of this method you must also change
|
||||||
|
* user_home.c in the makefiles package to match.
|
||||||
|
*/
|
||||||
NSString *
|
NSString *
|
||||||
NSHomeDirectoryForUser(NSString *loginName)
|
NSHomeDirectoryForUser(NSString *loginName)
|
||||||
{
|
{
|
||||||
|
@ -424,11 +428,9 @@ GSSystemRootDirectory(void)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the path of the defaults directory for name.<br />
|
* Return the path of the defaults directory for name.<br />
|
||||||
* This uses the GNUSTEP_DEFAULTS_ROOT or the GNUSTEP_USER_ROOT
|
* This examines the .GNUsteprc file in the home directory of the
|
||||||
* environment variable to determine the directory. If the user
|
* user for the GNUSTEP_DEFAULTS_ROOT or the GNUSTEP_USER_ROOT
|
||||||
* has changed, the path for the new user will be based on a template
|
* directory definitions.
|
||||||
* derived from the path for the original user, substituting in
|
|
||||||
* the values returned by NSHomeDirectory() and NSUser()
|
|
||||||
*/
|
*/
|
||||||
NSString*
|
NSString*
|
||||||
GSDefaultsRootForUser(NSString *userName)
|
GSDefaultsRootForUser(NSString *userName)
|
||||||
|
@ -439,144 +441,89 @@ GSDefaultsRootForUser(NSString *userName)
|
||||||
static NSString *
|
static NSString *
|
||||||
userDirectory(NSString *name, BOOL defaults)
|
userDirectory(NSString *name, BOOL defaults)
|
||||||
{
|
{
|
||||||
/*
|
NSFileManager *manager;
|
||||||
* Marker objects should be something which will never
|
NSString *home;
|
||||||
* appear in a normal path
|
NSString *path = nil;
|
||||||
*/
|
NSString *file;
|
||||||
static NSString *uMarker = @"[{<USER>}]";
|
NSString *user = nil;
|
||||||
static NSString *hMarker = @"[{<HOME>}]";
|
NSString *defs = nil;
|
||||||
static NSString *fileTemplate = nil;
|
|
||||||
static NSString *defsTemplate = nil;
|
|
||||||
NSString *template;
|
|
||||||
NSString *home;
|
|
||||||
NSString *path = nil;
|
|
||||||
NSRange r;
|
|
||||||
|
|
||||||
NSCAssert([name length] > 0, NSInvalidArgumentException);
|
NSCAssert([name length] > 0, NSInvalidArgumentException);
|
||||||
|
|
||||||
/**
|
if (defaults == YES)
|
||||||
* If we don't have templates set up, ensure that it's set up for
|
|
||||||
* the original user by pre-calling ourself for that user.
|
|
||||||
*/
|
|
||||||
if ([name isEqual: NSUserName()] == NO)
|
|
||||||
{
|
{
|
||||||
if (defsTemplate == nil)
|
#ifdef FORCE_DEFAULTS_ROOT
|
||||||
|
return [NSString stringWithCString: stringify(FORCE_DEFAULTS_ROOT)];
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#ifdef FORCE_USER_ROOT
|
||||||
|
return [NSString stringWithCString: stringify(FORCE_USER_ROOT)];
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
home = NSHomeDirectoryForUser(name);
|
||||||
|
file = [home stringByAppendingPathComponent: @".GNUsteprc"];
|
||||||
|
manager = [NSFileManager defaultManager];
|
||||||
|
if ([manager isReadableFileAtPath: file] == YES)
|
||||||
|
{
|
||||||
|
NSArray *lines;
|
||||||
|
unsigned count;
|
||||||
|
|
||||||
|
file = [NSString stringWithContentsOfFile: file];
|
||||||
|
lines = [file componentsSeparatedByString: @"\n"];
|
||||||
|
count = [lines count];
|
||||||
|
while (count-- > 0)
|
||||||
{
|
{
|
||||||
userDirectory(NSUserName(), YES);
|
NSRange r;
|
||||||
}
|
NSString *line;
|
||||||
if (fileTemplate == nil)
|
|
||||||
{
|
line = [[lines objectAtIndex: count] stringByTrimmingSpaces];
|
||||||
userDirectory(NSUserName(), NO);
|
r = [line rangeOfString: @"="];
|
||||||
|
if (r.length == 1)
|
||||||
|
{
|
||||||
|
NSString *key = [line substringToIndex: r.location];
|
||||||
|
NSString *val = [line substringFromIndex: NSMaxRange(r)];
|
||||||
|
|
||||||
|
key = [key stringByTrimmingSpaces];
|
||||||
|
val = [val stringByTrimmingSpaces];
|
||||||
|
if ([key isEqualToString: @"GNUSTEP_USER_ROOT"] == YES)
|
||||||
|
{
|
||||||
|
user = val;
|
||||||
|
}
|
||||||
|
else if ([key isEqualToString: @"GNUSTEP_DEFAULTS_ROOT"] == YES)
|
||||||
|
{
|
||||||
|
defs = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defaults == YES)
|
if (defaults == YES)
|
||||||
{
|
{
|
||||||
template = defsTemplate;
|
path = defs;
|
||||||
|
/*
|
||||||
|
* defaults root may default to user root
|
||||||
|
*/
|
||||||
|
if (path == nil)
|
||||||
|
{
|
||||||
|
path = user;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
template = fileTemplate;
|
path = user;
|
||||||
}
|
}
|
||||||
home = NSHomeDirectoryForUser(name);
|
|
||||||
|
|
||||||
[gnustep_global_lock lock];
|
/*
|
||||||
NS_DURING
|
* If not specified in file, default to standard location.
|
||||||
|
*/
|
||||||
|
if (path == nil)
|
||||||
{
|
{
|
||||||
if (template == nil)
|
path = [home stringByAppendingPathComponent: @"GNUstep"];
|
||||||
{
|
|
||||||
NSString *old;
|
|
||||||
|
|
||||||
if (defaults == YES)
|
|
||||||
{
|
|
||||||
path = [[[NSProcessInfo processInfo] environment]
|
|
||||||
objectForKey: @"GNUSTEP_DEFAULTS_ROOT"];
|
|
||||||
}
|
|
||||||
if (path == nil)
|
|
||||||
{
|
|
||||||
path = [[[NSProcessInfo processInfo] environment]
|
|
||||||
objectForKey: @"GNUSTEP_USER_ROOT"];
|
|
||||||
}
|
|
||||||
if (path == nil)
|
|
||||||
{
|
|
||||||
path = [NSHomeDirectoryForUser(name)
|
|
||||||
stringByAppendingPathComponent: @"GNUstep"];
|
|
||||||
fprintf (stderr,
|
|
||||||
"Warning - GNUSTEP_USER_ROOT is not set "
|
|
||||||
"- using %s\n", [path lossyCString]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
path = [path stringByExpandingTildeInPath];
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* We build a template for the user root path by replacing
|
|
||||||
* the user name and home directory in the original string.
|
|
||||||
*/
|
|
||||||
old = path;
|
|
||||||
if ([old hasPrefix: home] == YES)
|
|
||||||
{
|
|
||||||
old = [old substringFromIndex: [home length]];
|
|
||||||
template = hMarker;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
template = @"";
|
|
||||||
}
|
|
||||||
r = [old rangeOfString: name];
|
|
||||||
while (r.length > 0)
|
|
||||||
{
|
|
||||||
template = [template stringByAppendingFormat:
|
|
||||||
@"%@%@", [old substringToIndex: r.location], uMarker];
|
|
||||||
old = [old substringFromIndex: NSMaxRange(r)];
|
|
||||||
r = [old rangeOfString: name];
|
|
||||||
}
|
|
||||||
template = [template stringByAppendingString: old];
|
|
||||||
RETAIN(template);
|
|
||||||
if (defaults == YES)
|
|
||||||
{
|
|
||||||
defsTemplate = template;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fileTemplate = template;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
NSMutableString *m;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Use an existing template to create the user root
|
|
||||||
* for the current user.
|
|
||||||
*/
|
|
||||||
m = [template mutableCopy];
|
|
||||||
r = [m rangeOfString: uMarker];
|
|
||||||
while (r.length > 0)
|
|
||||||
{
|
|
||||||
[m replaceCharactersInRange: r withString: name];
|
|
||||||
r.location += [name length];
|
|
||||||
r.length = [m length] - r.location;
|
|
||||||
r = [m rangeOfString: uMarker
|
|
||||||
options: NSLiteralSearch
|
|
||||||
range: r];
|
|
||||||
}
|
|
||||||
r = [m rangeOfString: hMarker];
|
|
||||||
if (r.location == 0)
|
|
||||||
{
|
|
||||||
[m replaceCharactersInRange: r withString: home];
|
|
||||||
}
|
|
||||||
path = m;
|
|
||||||
AUTORELEASE(path);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
NS_HANDLER
|
|
||||||
{
|
|
||||||
[gnustep_global_lock unlock];
|
|
||||||
[localException raise];
|
|
||||||
}
|
|
||||||
NS_ENDHANDLER
|
|
||||||
[gnustep_global_lock unlock];
|
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue