Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit376ce3e

Browse files
committed
Prefer $HOME when looking up the current user's home directory.
When we need to identify the home directory on non-Windows, firstconsult getenv("HOME"). If that's empty or unset, fall backon our previous method of checking the <pwd.h> database.Preferring $HOME allows the user to intentionally point at someother directory, and it seems to be in line with the behavior ofmost other utilities. However, we shouldn't rely on it completely,as $HOME is likely to be unset when running as a daemon.Anders KaseorgDiscussion:https://postgr.es/m/1634252654444.90107@mit.edu
1 parent6867f96 commit376ce3e

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

‎src/bin/psql/command.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -558,19 +558,25 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
558558
else
559559
{
560560
#ifndefWIN32
561-
structpasswd*pw;
562-
uid_tuser_id=geteuid();
563-
564-
errno=0;/* clear errno before call */
565-
pw=getpwuid(user_id);
566-
if (!pw)
561+
/* This should match get_home_path() */
562+
dir=getenv("HOME");
563+
if (dir==NULL||dir[0]=='\0')
567564
{
568-
pg_log_error("could not get home directory for user ID %ld: %s",
569-
(long)user_id,
570-
errno ?strerror(errno) :_("user does not exist"));
571-
exit(EXIT_FAILURE);
565+
uid_tuser_id=geteuid();
566+
structpasswd*pw;
567+
568+
errno=0;/* clear errno before call */
569+
pw=getpwuid(user_id);
570+
if (pw)
571+
dir=pw->pw_dir;
572+
else
573+
{
574+
pg_log_error("could not get home directory for user ID %ld: %s",
575+
(long)user_id,
576+
errno ?strerror(errno) :_("user does not exist"));
577+
success= false;
578+
}
572579
}
573-
dir=pw->pw_dir;
574580
#else/* WIN32 */
575581

576582
/*
@@ -581,7 +587,8 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
581587
#endif/* WIN32 */
582588
}
583589

584-
if (chdir(dir)==-1)
590+
if (success&&
591+
chdir(dir)<0)
585592
{
586593
pg_log_error("\\%s: could not change directory to \"%s\": %m",
587594
cmd,dir);

‎src/interfaces/libpq/fe-connect.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7267,14 +7267,21 @@ bool
72677267
pqGetHomeDirectory(char*buf,intbufsize)
72687268
{
72697269
#ifndefWIN32
7270-
charpwdbuf[BUFSIZ];
7271-
structpasswdpwdstr;
7272-
structpasswd*pwd=NULL;
7270+
constchar*home;
72737271

7274-
(void)pqGetpwuid(geteuid(),&pwdstr,pwdbuf,sizeof(pwdbuf),&pwd);
7275-
if (pwd==NULL)
7276-
return false;
7277-
strlcpy(buf,pwd->pw_dir,bufsize);
7272+
home=getenv("HOME");
7273+
if (home==NULL||home[0]=='\0')
7274+
{
7275+
charpwdbuf[BUFSIZ];
7276+
structpasswdpwdstr;
7277+
structpasswd*pwd=NULL;
7278+
7279+
(void)pqGetpwuid(geteuid(),&pwdstr,pwdbuf,sizeof(pwdbuf),&pwd);
7280+
if (pwd==NULL)
7281+
return false;
7282+
home=pwd->pw_dir;
7283+
}
7284+
strlcpy(buf,home,bufsize);
72787285
return true;
72797286
#else
72807287
chartmppath[MAX_PATH];

‎src/port/path.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,25 @@ bool
807807
get_home_path(char*ret_path)
808808
{
809809
#ifndefWIN32
810-
charpwdbuf[BUFSIZ];
811-
structpasswdpwdstr;
812-
structpasswd*pwd=NULL;
810+
/*
811+
* We first consult $HOME. If that's unset, try to get the info from
812+
* <pwd.h>.
813+
*/
814+
constchar*home;
813815

814-
(void)pqGetpwuid(geteuid(),&pwdstr,pwdbuf,sizeof(pwdbuf),&pwd);
815-
if (pwd==NULL)
816-
return false;
817-
strlcpy(ret_path,pwd->pw_dir,MAXPGPATH);
816+
home=getenv("HOME");
817+
if (home==NULL||home[0]=='\0')
818+
{
819+
charpwdbuf[BUFSIZ];
820+
structpasswdpwdstr;
821+
structpasswd*pwd=NULL;
822+
823+
(void)pqGetpwuid(geteuid(),&pwdstr,pwdbuf,sizeof(pwdbuf),&pwd);
824+
if (pwd==NULL)
825+
return false;
826+
home=pwd->pw_dir;
827+
}
828+
strlcpy(ret_path,home,MAXPGPATH);
818829
return true;
819830
#else
820831
char*tmppath;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp