Path: blob/master/src/java.instrument/unix/native/libinstrument/FileSystemSupport_md.c
41149 views
/*1* Copyright (c) 2004, 2020, 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#include <stdio.h>26#include <stdlib.h>27#include <string.h>2829#include "FileSystemSupport_md.h"3031/*32* Solaris/Linux implementation of the file system support functions.33*/3435#define slash '/'3637char* basePath(const char* path) {38char* last = strrchr(path, slash);39if (last == NULL) {40return (char*)path;41} else {42int len = last - path;43char* str = (char*)malloc(len+1);44if (str == NULL) {45fprintf(stderr, "OOM error in native tmp buffer allocation");46return NULL;47}48if (len > 0) {49memcpy(str, path, len);50}51str[len] = '\0';52return str;53}54}5556int isAbsolute(const char* path) {57return (path[0] == slash) ? 1 : 0;58}5960/* Ported from src/solaris/classes/java/io/UnixFileSystem.java */6162/* A normal Unix pathname contains no duplicate slashes and does not end63with a slash. It may be the empty string. */6465/* Normalize the given pathname, whose length is len, starting at the given66offset; everything before this offset is already normal. */67static char* normalizePath(const char* pathname, int len, int off) {68char* sb;69int sbLen, i, n;70char prevChar;7172if (len == 0) return (char*)pathname;73n = len;74while ((n > 0) && (pathname[n - 1] == slash)) n--;75if (n == 0) return strdup("/");7677sb = (char*)malloc(strlen(pathname)+1);78if (sb == NULL) {79fprintf(stderr, "OOM error in native tmp buffer allocation");80return NULL;81}82sbLen = 0;8384if (off > 0) {85memcpy(sb, pathname, off);86sbLen = off;87}8889prevChar = 0;90for (i = off; i < n; i++) {91char c = pathname[i];92if ((prevChar == slash) && (c == slash)) continue;93sb[sbLen++] = c;94prevChar = c;95}96return sb;97}9899/* Check that the given pathname is normal. If not, invoke the real100normalizer on the part of the pathname that requires normalization.101This way we iterate through the whole pathname string only once. */102char* normalize(const char* pathname) {103int i;104int n = strlen(pathname);105char prevChar = 0;106for (i = 0; i < n; i++) {107char c = pathname[i];108if ((prevChar == slash) && (c == slash))109return normalizePath(pathname, n, i - 1);110prevChar = c;111}112if (prevChar == slash) return normalizePath(pathname, n, n - 1);113return (char*)pathname;114}115116char* resolve(const char* parent, const char* child) {117int len;118char* theChars;119int pn = strlen(parent);120int cn = strlen(child);121int childStart = 0;122int parentEnd = pn;123124if (pn > 0 && parent[pn-1] == slash) {125parentEnd--;126}127len = parentEnd + cn - childStart;128if (child[0] == slash) {129theChars = (char*)malloc(len+1);130if (theChars == NULL) {131fprintf(stderr, "OOM error in native tmp buffer allocation");132return NULL;133}134if (parentEnd > 0)135memcpy(theChars, parent, parentEnd);136if (cn > 0)137memcpy(theChars+parentEnd, child, cn);138theChars[len] = '\0';139} else {140theChars = (char*)malloc(len+2);141if (theChars == NULL) {142fprintf(stderr, "OOM error in native tmp buffer allocation");143return NULL;144}145if (parentEnd > 0)146memcpy(theChars, parent, parentEnd);147theChars[parentEnd] = slash;148if (cn > 0)149memcpy(theChars+parentEnd+1, child, cn);150theChars[len+1] = '\0';151}152return theChars;153}154155char* fromURIPath(const char* path) {156int len = strlen(path);157if (len > 1 && path[len-1] == slash) {158// "/foo/" --> "/foo", but "/" --> "/"159char* str = (char*)malloc(len);160if (str == NULL)161{162fprintf(stderr, "OOM error in native tmp buffer allocation");163return NULL;164}165memcpy(str, path, len-1);166str[len-1] = '\0';167return str;168} else {169return (char*)path;170}171}172173174