Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/java/io/canonicalize_md.c
32287 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#endif383940/* Note: The comments in this file use the terminology41defined in the java.io.File class */424344/* Check the given name sequence to see if it can be further collapsed.45Return zero if not, otherwise return the number of names in the sequence. */4647static int48collapsible(char *names)49{50char *p = names;51int dots = 0, n = 0;5253while (*p) {54if ((p[0] == '.') && ((p[1] == '\0')55|| (p[1] == '/')56|| ((p[1] == '.') && ((p[2] == '\0')57|| (p[2] == '/'))))) {58dots = 1;59}60n++;61while (*p) {62if (*p == '/') {63p++;64break;65}66p++;67}68}69return (dots ? n : 0);70}717273/* Split the names in the given name sequence,74replacing slashes with nulls and filling in the given index array */7576static void77splitNames(char *names, char **ix)78{79char *p = names;80int i = 0;8182while (*p) {83ix[i++] = p++;84while (*p) {85if (*p == '/') {86*p++ = '\0';87break;88}89p++;90}91}92}939495/* Join the names in the given name sequence, ignoring names whose index96entries have been cleared and replacing nulls with slashes as needed */9798static void99joinNames(char *names, int nc, char **ix)100{101int i;102char *p;103104for (i = 0, p = names; i < nc; i++) {105if (!ix[i]) continue;106if (i > 0) {107p[-1] = '/';108}109if (p == ix[i]) {110p += strlen(p) + 1;111} else {112char *q = ix[i];113while ((*p++ = *q++));114}115}116*p = '\0';117}118119120/* Collapse "." and ".." names in the given path wherever possible.121A "." name may always be eliminated; a ".." name may be eliminated if it122follows a name that is neither "." nor "..". This is a syntactic operation123that performs no filesystem queries, so it should only be used to cleanup124after invoking the realpath() procedure. */125126static void127collapse(char *path)128{129char *names = (path[0] == '/') ? path + 1 : path; /* Preserve first '/' */130int nc;131char **ix;132int i, j;133char *p, *q;134135nc = collapsible(names);136if (nc < 2) return; /* Nothing to do */137ix = (char **)alloca(nc * sizeof(char *));138splitNames(names, ix);139140for (i = 0; i < nc; i++) {141int dots = 0;142143/* Find next occurrence of "." or ".." */144do {145char *p = ix[i];146if (p[0] == '.') {147if (p[1] == '\0') {148dots = 1;149break;150}151if ((p[1] == '.') && (p[2] == '\0')) {152dots = 2;153break;154}155}156i++;157} while (i < nc);158if (i >= nc) break;159160/* At this point i is the index of either a "." or a "..", so take the161appropriate action and then continue the outer loop */162if (dots == 1) {163/* Remove this instance of "." */164ix[i] = 0;165}166else {167/* If there is a preceding name, remove both that name and this168instance of ".."; otherwise, leave the ".." as is */169for (j = i - 1; j >= 0; j--) {170if (ix[j]) break;171}172if (j < 0) continue;173ix[j] = 0;174ix[i] = 0;175}176/* i will be incremented at the top of the loop */177}178179joinNames(names, nc, ix);180}181182183/* Convert a pathname to canonical form. The input path is assumed to contain184no duplicate slashes. On Solaris we can use realpath() to do most of the185work, though once that's done we still must collapse any remaining "." and186".." names by hand. */187188int189canonicalize(char *original, char *resolved, int len)190{191if (len < PATH_MAX) {192errno = EINVAL;193return -1;194}195196if (strlen(original) > PATH_MAX) {197errno = ENAMETOOLONG;198return -1;199}200201/* First try realpath() on the entire path */202if (realpath(original, resolved)) {203/* That worked, so return it */204collapse(resolved);205return 0;206}207else {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(original) <= PATH_MAX, see above214strncpy(path, original, 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, resolved);228*p = (p == end) ? '\0' : '/';229230if (r != NULL) {231/* The subpath has a canonical path */232break;233}234else if (errno == ENOENT || errno == ENOTDIR || errno == EACCES) {235/* If the lookup of a particular subpath fails because the file236does not exist, because it is of the wrong type, or because237access is denied, then remove its last name and try again.238Other I/O problems cause an error return. */239continue;240}241else {242return -1;243}244}245246if (r != NULL) {247/* Append unresolved subpath to resolved subpath */248int rn = strlen(r);249if (rn + (int)strlen(p) >= len) {250/* Buffer overflow */251errno = ENAMETOOLONG;252return -1;253}254if ((rn > 0) && (r[rn - 1] == '/') && (*p == '/')) {255/* Avoid duplicate slashes */256p++;257}258strcpy(r + rn, p);259collapse(r);260return 0;261}262else {263/* Nothing resolved, so just return the original path */264strcpy(resolved, path);265collapse(resolved);266return 0;267}268}269270}271272273