src/java.instrument/unix/native/libinstrument/FileSystemSupport_md.c
author pliden
Wed, 20 Nov 2019 10:37:46 +0100
changeset 59152 59272e9e0635
parent 54319 55025f677f68
permissions -rw-r--r--
8234383: Test TestBiasedLockRevocationEvents.java assumes -XX:UseBiasedLocking is enabled Reviewed-by: mgronlun, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54319
55025f677f68 8221532: Incorrect copyright header in FileSystemSupport_md.c
dtitov
parents: 53313
diff changeset
     2
 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    26
#include <stdio.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "FileSystemSupport_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Solaris/Linux implementation of the file system support functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#define slash           '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
char pathSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    return ':';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/* Filenames are case senstitive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
int filenameStrcmp(const char* s1, const char* s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  return strcmp(s1, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
char* basePath(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    char* last = strrchr(path, slash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if (last == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        return (char*)path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int len = last - path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        char* str = (char*)malloc(len+1);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    54
        if (str == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    55
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    56
            return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    57
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            memcpy(str, path, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        str[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
int isAbsolute(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    return (path[0] == slash) ? 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/* Ported from src/solaris/classes/java/io/UnixFileSystem.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/* A normal Unix pathname contains no duplicate slashes and does not end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
   with a slash.  It may be the empty string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/* Normalize the given pathname, whose length is len, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
   offset; everything before this offset is already normal. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static char* normalizePath(const char* pathname, int len, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    char* sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int sbLen, i, n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    char prevChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (len == 0) return (char*)pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    while ((n > 0) && (pathname[n - 1] == slash)) n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (n == 0) return strdup("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    sb = (char*)malloc(strlen(pathname)+1);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    88
    if (sb == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    89
        fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    90
        return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
    91
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    sbLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (off > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        memcpy(sb, pathname, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        sbLen = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    for (i = off; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        char c = pathname[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ((prevChar == slash) && (c == slash)) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        sb[sbLen++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    return sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
/* Check that the given pathname is normal.  If not, invoke the real
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
   normalizer on the part of the pathname that requires normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
   This way we iterate through the whole pathname string only once. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
char* normalize(const char* pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    int n = strlen(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    char prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    for (i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        char c = pathname[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if ((prevChar == slash) && (c == slash))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return normalizePath(pathname, n, i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        prevChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    if (prevChar == slash) return normalizePath(pathname, n, n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    return (char*)pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
char* resolve(const char* parent, const char* child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    char* theChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    int pn = strlen(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    int cn = strlen(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    int childStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    int parentEnd = pn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    if (pn > 0 && parent[pn-1] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        parentEnd--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    len = parentEnd + cn - childStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    if (child[0] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        theChars = (char*)malloc(len+1);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   140
        if (theChars == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   141
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   142
            return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   143
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (parentEnd > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            memcpy(theChars, parent, parentEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (cn > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            memcpy(theChars+parentEnd, child, cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        theChars[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        theChars = (char*)malloc(len+2);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   151
        if (theChars == NULL) {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   152
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   153
            return NULL;
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   154
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (parentEnd > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            memcpy(theChars, parent, parentEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        theChars[parentEnd] = slash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (cn > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            memcpy(theChars+parentEnd+1, child, cn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        theChars[len+1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    return theChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
char* fromURIPath(const char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    int len = strlen(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    if (len > 1 && path[len-1] == slash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // "/foo/" --> "/foo", but "/" --> "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        char* str = (char*)malloc(len);
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   170
        if (str == NULL)
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   171
        {
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   172
            fprintf(stderr, "OOM error in native tmp buffer allocation");
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   173
            return NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
53313
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   175
        memcpy(str, path, len-1);
c66b192fe3b4 8205709: Proper allocation handling
dtitov
parents: 47216
diff changeset
   176
        str[len-1] = '\0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return (char*)path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}