Path: blob/master/src/java.base/unix/native/libjava/canonicalize_md.c
41119 views
/*1* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* Pathname canonicalization for Unix file systems27*/2829#include <stdio.h>30#include <stdlib.h>31#include <string.h>32#include <sys/stat.h>33#include <errno.h>34#include <limits.h>35#if !defined(_ALLBSD_SOURCE)36#include <alloca.h>37#endif3839#include "jdk_util.h"4041/* Note: The comments in this file use the terminology42defined in the java.io.File class */434445/* Check the given name sequence to see if it can be further collapsed.46Return zero if not, otherwise return the number of names in the sequence. */4748static int49collapsible(char *names)50{51char *p = names;52int dots = 0, n = 0;5354while (*p) {55if ((p[0] == '.') && ((p[1] == '\0')56|| (p[1] == '/')57|| ((p[1] == '.') && ((p[2] == '\0')58|| (p[2] == '/'))))) {59dots = 1;60}61n++;62while (*p) {63if (*p == '/') {64p++;65break;66}67p++;68}69}70return (dots ? n : 0);71}727374/* Split the names in the given name sequence,75replacing slashes with nulls and filling in the given index array */7677static void78splitNames(char *names, char **ix)79{80char *p = names;81int i = 0;8283while (*p) {84ix[i++] = p++;85while (*p) {86if (*p == '/') {87*p++ = '\0';88break;89}90p++;91}92}93}949596/* Join the names in the given name sequence, ignoring names whose index97entries have been cleared and replacing nulls with slashes as needed */9899static void100joinNames(char *names, int nc, char **ix)101{102int i;103char *p;104105for (i = 0, p = names; i < nc; i++) {106if (!ix[i]) continue;107if (i > 0) {108p[-1] = '/';109}110if (p == ix[i]) {111p += strlen(p) + 1;112} else {113char *q = ix[i];114while ((*p++ = *q++));115}116}117*p = '\0';118}119120121/* Collapse "." and ".." names in the given path wherever possible.122A "." name may always be eliminated; a ".." name may be eliminated if it123follows a name that is neither "." nor "..". This is a syntactic operation124that performs no filesystem queries, so it should only be used to cleanup125after invoking the realpath() procedure. */126127static void128collapse(char *path)129{130char *names = (path[0] == '/') ? path + 1 : path; /* Preserve first '/' */131int nc;132char **ix;133int i, j;134char *p, *q;135136nc = collapsible(names);137if (nc < 2) return; /* Nothing to do */138ix = (char **)alloca(nc * sizeof(char *));139splitNames(names, ix);140141for (i = 0; i < nc; i++) {142int dots = 0;143144/* Find next occurrence of "." or ".." */145do {146char *p = ix[i];147if (p[0] == '.') {148if (p[1] == '\0') {149dots = 1;150break;151}152if ((p[1] == '.') && (p[2] == '\0')) {153dots = 2;154break;155}156}157i++;158} while (i < nc);159if (i >= nc) break;160161/* At this point i is the index of either a "." or a "..", so take the162appropriate action and then continue the outer loop */163if (dots == 1) {164/* Remove this instance of "." */165ix[i] = 0;166}167else {168/* If there is a preceding name, remove both that name and this169instance of ".."; otherwise, leave the ".." as is */170for (j = i - 1; j >= 0; j--) {171if (ix[j]) break;172}173if (j < 0) continue;174ix[j] = 0;175ix[i] = 0;176}177/* i will be incremented at the top of the loop */178}179180joinNames(names, nc, ix);181}182183184/* Convert a pathname to canonical form. The input path is assumed to contain185no duplicate slashes. On Solaris we can use realpath() to do most of the186work, though once that's done we still must collapse any remaining "." and187".." names by hand. */188189JNIEXPORT int190JDK_Canonicalize(const char *orig, char *out, int len)191{192if (len < PATH_MAX) {193errno = EINVAL;194return -1;195}196197if (strlen(orig) > PATH_MAX) {198errno = ENAMETOOLONG;199return -1;200}201202/* First try realpath() on the entire path */203if (realpath(orig, out)) {204/* That worked, so return it */205collapse(out);206return 0;207} else {208/* Something's bogus in the original path, so remove names from the end209until either some subpath works or we run out of names */210char *p, *end, *r = NULL;211char path[PATH_MAX + 1];212213// strlen(orig) <= PATH_MAX, see above214strncpy(path, orig, PATH_MAX);215// append null for == case216path[PATH_MAX] = '\0';217end = path + strlen(path);218219for (p = end; p > path;) {220221/* Skip last element */222while ((--p > path) && (*p != '/'));223if (p == path) break;224225/* Try realpath() on this subpath */226*p = '\0';227r = realpath(path, out);228*p = (p == end) ? '\0' : '/';229230if (r != NULL) {231/* The subpath has a canonical path */232break;233} else if (errno == ENOENT || errno == ENOTDIR || errno == EACCES) {234/* If the lookup of a particular subpath fails because the file235does not exist, because it is of the wrong type, or because236access is denied, then remove its last name and try again.237Other I/O problems cause an error return. */238continue;239} else {240return -1;241}242}243244if (r != NULL) {245/* Append unresolved subpath to resolved subpath */246int rn = strlen(r);247if (rn + (int)strlen(p) >= len) {248/* Buffer overflow */249errno = ENAMETOOLONG;250return -1;251}252if ((rn > 0) && (r[rn - 1] == '/') && (*p == '/')) {253/* Avoid duplicate slashes */254p++;255}256strcpy(r + rn, p);257collapse(r);258return 0;259} else {260/* Nothing resolved, so just return the original path */261strcpy(out, path);262collapse(out);263return 0;264}265}266}267268269