#include #include #include #include "mb/pg_wchar.h" #include "pg_config_paths.h"">

PostgreSQL Source Code: src/port/path.c File Reference (original) (raw)

#include "[postgres.h](postgres%5F8h%5Fsource.html)"
#include <ctype.h>
#include <sys/stat.h>
#include <[pwd.h](pwd%5F8h%5Fsource.html)>
#include <[unistd.h](unistd%5F8h%5Fsource.html)>
#include "[mb/pg_wchar.h](pg%5F%5Fwchar%5F8h%5Fsource.html)"
#include "pg_config_paths.h"

Go to the source code of this file.

Functions
static void make_relative_path (char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path)
static char * trim_directory (char *path)
static void trim_trailing_separator (char *path)
static char * append_subdir_to_path (char *path, char *subdir)
bool has_drive_prefix (const char *path)
char * first_dir_separator (const char *filename)
char * first_path_var_separator (const char *pathlist)
char * last_dir_separator (const char *filename)
void make_native_path (char *filename)
void cleanup_path (char *path)
void join_path_components (char *ret_path, const char *head, const char *tail)
void canonicalize_path (char *path)
void canonicalize_path_enc (char *path, int encoding)
bool path_contains_parent_reference (const char *path)
bool path_is_relative_and_below_cwd (const char *path)
bool path_is_prefix_of_path (const char *path1, const char *path2)
const char * get_progname (const char *argv0)
static int dir_strcmp (const char *s1, const char *s2)
char * make_absolute_path (const char *path)
void get_share_path (const char *my_exec_path, char *ret_path)
void get_etc_path (const char *my_exec_path, char *ret_path)
void get_include_path (const char *my_exec_path, char *ret_path)
void get_pkginclude_path (const char *my_exec_path, char *ret_path)
void get_includeserver_path (const char *my_exec_path, char *ret_path)
void get_lib_path (const char *my_exec_path, char *ret_path)
void get_pkglib_path (const char *my_exec_path, char *ret_path)
void get_locale_path (const char *my_exec_path, char *ret_path)
void get_doc_path (const char *my_exec_path, char *ret_path)
void get_html_path (const char *my_exec_path, char *ret_path)
void get_man_path (const char *my_exec_path, char *ret_path)
bool get_home_path (char *ret_path)
void get_parent_directory (char *path)

IS_PATH_VAR_SEP

| #define IS_PATH_VAR_SEP | ( | | ch | ) | ((ch) == ':') | | -------------------------- | - | | -- | - | ------------- |

Definition at line 44 of file path.c.

skip_drive

| #define skip_drive | ( | | path | ) | (path) | | ------------------- | - | | ---- | - | ------ |

Definition at line 85 of file path.c.

canonicalize_state

Enumerator
ABSOLUTE_PATH_INIT
ABSOLUTE_WITH_N_DEPTH
RELATIVE_PATH_INIT
RELATIVE_WITH_N_DEPTH
RELATIVE_WITH_PARENT_REF

Definition at line 309 of file path.c.

310{

312

314

317

@ RELATIVE_WITH_PARENT_REF

append_subdir_to_path()

static char * append_subdir_to_path ( char * path, char * subdir ) static

Definition at line 1139 of file path.c.

1140{

1141 size_t len = strlen(subdir);

1142

1143

1144 if (path != subdir)

1145 memmove(path, subdir, len);

1146

1147 return path + len;

1148}

References len.

Referenced by canonicalize_path_enc().

canonicalize_path()

void canonicalize_path ( char * path )

Definition at line 337 of file path.c.

338{

339

341}

void canonicalize_path_enc(char *path, int encoding)

References canonicalize_path_enc(), and PG_SQL_ASCII.

Referenced by AbsoluteConfigLocation(), add_tablespace_mapping(), adjust_data_dir(), check_canonical_path(), check_required_directory(), convert_and_check_filename(), create_script_for_old_cluster_deletion(), create_xlog_or_symlink(), CreateTableSpace(), find_in_path(), find_in_paths(), find_my_exec(), find_other_exec(), get_extension_control_directories(), get_tablespace_mapping(), GetConfFilesInDir(), main(), make_absolute_path(), make_relative_path(), member_verify_header(), normalize_exec_path(), parseCommandLine(), scan_for_existing_tablespaces(), setup(), setup_bin_paths(), setup_pgdata(), SplitDirectoriesString(), tablespace_list_append(), and test_canonicalize_path().

canonicalize_path_enc()

void canonicalize_path_enc ( char * path,
int encoding
)

Definition at line 344 of file path.c.

345{

346 char *p,

347 *to_p;

348 char *spath;

349 char *parsed;

350 char *unparse;

351 bool was_sep = false;

353 int pathdepth = 0;

354

355#ifdef WIN32

356

357

358

359

360

361

362 debackslash_path(path, encoding);

363

364

365

366

367

368 p = path + strlen(path);

369 if (p > path && *(p - 1) == '"')

370 *(p - 1) = '/';

371#endif

372

373

374

375

376

377

379

380

381

382

383 p = path;

384#ifdef WIN32

385

386 if (*p)

387 p++;

388#endif

389 to_p = p;

390 for (; *p; p++, to_p++)

391 {

392

393 while (*p == '/' && was_sep)

394 p++;

395 if (to_p != p)

396 *to_p = *p;

397 was_sep = (*p == '/');

398 }

399 *to_p = '\0';

400

401

402

403

404

405

406

407

408

409

410

411

412

414 if (*spath == '\0')

415 return;

416

417 if (*spath == '/')

418 {

420

421 parsed = unparse = (spath + 1);

422 }

423 else

424 {

426 parsed = unparse = spath;

427 }

428

429 while (*unparse != '\0')

430 {

431 char *unparse_next;

432 bool is_double_dot;

433

434

435 unparse_next = unparse;

436 while (*unparse_next && *unparse_next != '/')

437 unparse_next++;

438 if (*unparse_next != '\0')

439 *unparse_next++ = '\0';

440

441

442 if (strcmp(unparse, ".") == 0)

443 {

444

445 unparse = unparse_next;

446 continue;

447 }

448

449 if (strcmp(unparse, "..") == 0)

450 is_double_dot = true;

451 else

452 {

453

454 Assert(*unparse != '\0');

455 is_double_dot = false;

456 }

457

459 {

461

462 if (!is_double_dot)

463 {

464

467 pathdepth++;

468 }

469 break;

471 if (is_double_dot)

472 {

473

474

475 *parsed = '\0';

477 if (--pathdepth == 0)

479 }

480 else

481 {

482

483 *parsed++ = '/';

485 pathdepth++;

486 }

487 break;

489 if (is_double_dot)

490 {

491

494 }

495 else

496 {

497

500 pathdepth++;

501 }

502 break;

504 if (is_double_dot)

505 {

506

507 *parsed = '\0';

509 if (--pathdepth == 0)

510 {

511

512

513

514

515

516

517 if (parsed == spath)

519 else

521 }

522 }

523 else

524 {

525

526 *parsed++ = '/';

528 pathdepth++;

529 }

530 break;

532 if (is_double_dot)

533 {

534

535 *parsed++ = '/';

537 }

538 else

539 {

540

541 *parsed++ = '/';

543

544

545

546

547

548

550 pathdepth = 1;

551 }

552 break;

553 }

554

555 unparse = unparse_next;

556 }

557

558

559

560

561

562

563

564 if (parsed == spath)

565 *parsed++ = '.';

566

567

568 *parsed = '\0';

569}

Assert(PointerIsAligned(start, uint64))

static char * append_subdir_to_path(char *path, char *subdir)

static void trim_trailing_separator(char *path)

static char * trim_directory(char *path)

References ABSOLUTE_PATH_INIT, ABSOLUTE_WITH_N_DEPTH, append_subdir_to_path(), Assert(), encoding, RELATIVE_PATH_INIT, RELATIVE_WITH_N_DEPTH, RELATIVE_WITH_PARENT_REF, skip_drive, trim_directory(), and trim_trailing_separator().

Referenced by canonicalize_path(), do_copy(), exec_command_edit(), exec_command_write(), and process_file().

cleanup_path()

void cleanup_path ( char * path )

dir_strcmp()

static int dir_strcmp ( const char * s1, const char * s2 ) static

Definition at line 690 of file path.c.

691{

692 while (*s1 && *s2)

693 {

694 if (

695#ifndef WIN32

697#else

698

700#endif

702 return (int) *s1 - (int) *s2;

704 }

705 if (*s1)

706 return 1;

707 if (*s2)

708 return -1;

709 return 0;

710}

unsigned char pg_tolower(unsigned char ch)

References IS_DIR_SEP, pg_tolower(), s1, and s2.

Referenced by make_relative_path().

first_dir_separator()

char * first_dir_separator ( const char * filename )

Definition at line 110 of file path.c.

111{

112 const char *p;

113

117 return NULL;

118}

#define unconstify(underlying_type, expr)

References filename, IS_DIR_SEP, skip_drive, and unconstify.

Referenced by check_restricted_library_name(), check_valid_extension_name(), check_valid_version_name(), expand_dynamic_library_name(), find_in_path(), find_my_exec(), load_libraries(), and substitute_path_macro().

first_path_var_separator()

char * first_path_var_separator ( const char * pathlist )

get_doc_path()

void get_doc_path ( const char * my_exec_path,
char * ret_path
)

get_etc_path()

void get_etc_path ( const char * my_exec_path,
char * ret_path
)

get_home_path()

bool get_home_path ( char * ret_path )

Definition at line 1005 of file path.c.

1006{

1007#ifndef WIN32

1008

1009

1010

1011

1012 const char *home;

1013

1014 home = getenv("HOME");

1015 if (home && home[0])

1016 {

1018 return true;

1019 }

1020 else

1021 {

1022 struct passwd pwbuf;

1023 struct passwd *pw;

1024 char buf[1024];

1025 int rc;

1026

1027 rc = getpwuid_r(geteuid(), &pwbuf, buf, sizeof buf, &pw);

1028 if (rc != 0 || !pw)

1029 return false;

1031 return true;

1032 }

1033#else

1034 char *tmppath;

1035

1036

1037

1038

1039

1040

1041

1042

1043 tmppath = getenv("APPDATA");

1044 if (!tmppath)

1045 return false;

1047 return true;

1048#endif

1049}

size_t strlcpy(char *dst, const char *src, size_t siz)

References buf, MAXPGPATH, snprintf, and strlcpy().

Referenced by expand_tilde(), initializeInput(), and process_psqlrc().

get_html_path()

void get_html_path ( const char * my_exec_path,
char * ret_path
)

get_include_path()

void get_include_path ( const char * my_exec_path,
char * ret_path
)

get_includeserver_path()

void get_includeserver_path ( const char * my_exec_path,
char * ret_path
)

get_lib_path()

void get_lib_path ( const char * my_exec_path,
char * ret_path
)

get_locale_path()

void get_locale_path ( const char * my_exec_path,
char * ret_path
)

get_man_path()

void get_man_path ( const char * my_exec_path,
char * ret_path
)

get_parent_directory()

void get_parent_directory ( char * path )

get_pkginclude_path()

void get_pkginclude_path ( const char * my_exec_path,
char * ret_path
)

get_pkglib_path()

void get_pkglib_path ( const char * my_exec_path,
char * ret_path
)

get_progname()

const char * get_progname ( const char * argv0 )

Definition at line 652 of file path.c.

653{

654 const char *nodir_name;

656

658 if (nodir_name)

659 nodir_name++;

660 else

662

663

664

665

666

667 progname = strdup(nodir_name);

669 {

670 fprintf(stderr, "%s: out of memory\n", nodir_name);

671 abort();

672 }

673

674#if defined(__CYGWIN__) || defined(WIN32)

675

676 if (strlen(progname) > sizeof(EXE) - 1 &&

679#endif

680

682}

#define fprintf(file, fmt, msg)

char * last_dir_separator(const char *filename)

int pg_strcasecmp(const char *s1, const char *s2)

References argv0, EXE, fprintf, last_dir_separator(), pg_strcasecmp(), progname, and skip_drive.

Referenced by get_opts(), handle_help_version_opts(), main(), parseCommandLine(), pg_logging_init(), and regression_main().

get_share_path()

void get_share_path ( const char * my_exec_path,
char * ret_path
)

has_drive_prefix()

bool has_drive_prefix ( const char * path )

join_path_components()

void join_path_components ( char * ret_path,
const char * head,
const char * tail
)

Definition at line 286 of file path.c.

288{

289 if (ret_path != head)

291

292

293

294

295

296

297 if (*tail)

298 {

299

300 snprintf(ret_path + strlen(ret_path), MAXPGPATH - strlen(ret_path),

301 "%s%s",

302 (*(skip_drive(head)) != '\0') ? "/" : "",

303 tail);

304 }

305}

References MAXPGPATH, skip_drive, snprintf, and strlcpy().

Referenced by AbsoluteConfigLocation(), find_my_exec(), GetConfFilesInDir(), main(), make_relative_path(), and process_file().

last_dir_separator()

char * last_dir_separator ( const char * filename )

Definition at line 145 of file path.c.

146{

147 const char *p,

148 *ret = NULL;

149

152 ret = p;

154}

References filename, IS_DIR_SEP, skip_drive, and unconstify.

Referenced by check_file_excluded(), ECPGconnect(), find_other_exec(), get_progname(), main(), pg_get_loaded_modules(), sendDir(), setup(), setup_bin_paths(), and should_allow_existing_directory().

make_absolute_path()

char * make_absolute_path ( const char * path )

Definition at line 807 of file path.c.

808{

809 char *new;

810

811

812 if (path == NULL)

813 return NULL;

814

816 {

817 char *buf;

818 size_t buflen;

819

821 for (;;)

822 {

824 if (buf)

825 {

826#ifndef FRONTEND

828 (errcode(ERRCODE_OUT_OF_MEMORY),

829 errmsg("out of memory")));

830#else

831 fprintf(stderr, _("out of memory\n"));

832 return NULL;

833#endif

834 }

835

836 if (getcwd(buf, buflen))

837 break;

838 else if (errno == ERANGE)

839 {

841 buflen *= 2;

842 continue;

843 }

844 else

845 {

846 int save_errno = errno;

847

849 errno = save_errno;

850#ifndef FRONTEND

851 elog(ERROR, "could not get current working directory: %m");

852#else

853 fprintf(stderr, _("could not get current working directory: %m\n"));

854 return NULL;

855#endif

856 }

857 }

858

859 new = malloc(strlen(buf) + strlen(path) + 2);

860 if (!new)

861 {

863#ifndef FRONTEND

865 (errcode(ERRCODE_OUT_OF_MEMORY),

866 errmsg("out of memory")));

867#else

868 fprintf(stderr, _("out of memory\n"));

869 return NULL;

870#endif

871 }

874 }

875 else

876 {

877 new = strdup(path);

878 if (!new)

879 {

880#ifndef FRONTEND

882 (errcode(ERRCODE_OUT_OF_MEMORY),

883 errmsg("out of memory")));

884#else

885 fprintf(stderr, _("out of memory\n"));

886 return NULL;

887#endif

888 }

889 }

890

891

893

894 return new;

895}

int errcode(int sqlerrcode)

int errmsg(const char *fmt,...)

#define ereport(elevel,...)

void canonicalize_path(char *path)

#define is_absolute_path(filename)

References _, buf, canonicalize_path(), elog, ereport, errcode(), errmsg(), ERROR, fprintf, free, is_absolute_path, malloc, MAXPGPATH, and sprintf.

Referenced by regression_main(), SelectConfigFiles(), and SetDataDir().

make_native_path()

void make_native_path ( char * filename )

make_relative_path()

static void make_relative_path ( char * ret_path, const char * target_path, const char * bin_path, const char * my_exec_path ) static

Definition at line 738 of file path.c.

740{

741 int prefix_len;

742 int tail_start;

743 int tail_len;

744 int i;

745

746

747

748

749

750 prefix_len = 0;

751 for (i = 0; target_path[i] && bin_path[i]; i++)

752 {

754 prefix_len = i + 1;

755 else if (target_path[i] != bin_path[i])

756 break;

757 }

758 if (prefix_len == 0)

759 goto no_match;

760 tail_len = strlen(bin_path) - prefix_len;

761

762

763

764

765

767 trim_directory(ret_path);

769

770

771

772

773 tail_start = (int) strlen(ret_path) - tail_len;

774 if (tail_start > 0 &&

775 IS_DIR_SEP(ret_path[tail_start - 1]) &&

777 {

778 ret_path[tail_start] = '\0';

782 return;

783 }

784

785no_match:

788}

static char bin_path[MAXPGPATH]

void join_path_components(char *ret_path, const char *head, const char *tail)

static int dir_strcmp(const char *s1, const char *s2)

References bin_path, canonicalize_path(), dir_strcmp(), i, IS_DIR_SEP, join_path_components(), MAXPGPATH, my_exec_path, strlcpy(), trim_directory(), and trim_trailing_separator().

Referenced by get_doc_path(), get_etc_path(), get_html_path(), get_include_path(), get_includeserver_path(), get_lib_path(), get_locale_path(), get_man_path(), get_pkginclude_path(), get_pkglib_path(), and get_share_path().

path_contains_parent_reference()

bool path_contains_parent_reference ( const char * path )

path_is_prefix_of_path()

bool path_is_prefix_of_path ( const char * path1,
const char * path2
)

path_is_relative_and_below_cwd()

bool path_is_relative_and_below_cwd ( const char * path )

trim_directory()

static char * trim_directory ( char * path) static

trim_trailing_separator()

static void trim_trailing_separator ( char * path) static