📄 ad_fstype.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* * Copyright (C) 1997 University of Chicago. * See COPYRIGHT notice in top-level directory. *//* This file is quickly becoming the single one, outside the ADIO * implementations, which has "what ADIO components are built in" code in it. */#include "adio.h"#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_SYS_PARAM_H#include <sys/param.h>#endif#ifdef HAVE_PVFS_H#include "pvfs.h"#endif#ifdef HAVE_PVFS2_H#include "pvfs2.h"#endif/* Notes on detection process: * * There are three more "general" mechanisms that we use for detecting * file system type: * - struct statfs's f_type field * - struct statvfs's f_basetype field * - struct stat's st_fstype field * * Otherwise we'll fall back on some OS-specific approach. */#ifdef HAVE_STRUCT_STATFS# ifdef HAVE_SYS_VFS_H# include <sys/vfs.h># endif# ifdef HAVE_SYS_STATVFS_H# include <sys/statvfs.h># endif# ifdef HAVE_SYS_PARAM_H# include <sys/param.h># endif# ifdef HAVE_SYS_MOUNT_H# include <sys/mount.h># endif /* On Linux platforms, linux/nfs_fs.h is all messed up and cannot be * reliably included. */# if defined(ROMIO_NFS) && !defined(NFS_SUPER_MAGIC)# define NFS_SUPER_MAGIC 0x6969# endif# if defined(ROMIO_PANFS) && !defined(PAN_KERNEL_FS_CLIENT_SUPER_MAGIC)# define PAN_KERNEL_FS_CLIENT_SUPER_MAGIC 0xAAD7AAEA# endif#endif# if defined(ROMIO_XFS) && !defined(XFS_SUPER_MAGIC)# define XFS_SUPER_MAGIC 0x58465342# endif#ifdef ROMIO_HAVE_STRUCT_STATVFS_WITH_F_BASETYPE# ifdef HAVE_SYS_STATVFS_H# include <sys/statvfs.h># endif# ifdef HAVE_SYS_VFS_H# include <sys/vfs.h># endif# ifdef HAVE_SYS_PARAM_H# include <sys/param.h># endif# ifdef HAVE_SYS_MOUNT_H# include <sys/mount.h># endif#endif#ifdef ROMIO_HAVE_STRUCT_STAT_WITH_ST_FSTYPE# ifdef HAVE_SYS_TYPES_H# include <sys/types.h># endif# ifdef HAVE_SYS_STAT_H# include <sys/stat.h># endif#endif#ifndef ROMIO_NTFSstatic void ADIO_FileSysType_parentdir(char *filename, char **dirnamep);#endifstatic void ADIO_FileSysType_prefix(char *filename, int *fstype, int *error_code);static void ADIO_FileSysType_fncall(char *filename, int *fstype, int *error_code);/* ADIO_FileSysType_parentdir - determines a string pathname for the parent directory of a given filename.Input Parameters:. filename - pointer to file name character arrayOutput Parameters:. dirnamep - pointer to location in which to store a pointer to a string Note that the caller should free the memory located at the pointer returned after the string is no longer needed.*/#ifndef ROMIO_NTFS#ifndef PATH_MAX#define PATH_MAX 65535#endif/* In a strict ANSI environment, S_ISLNK may not be defined. Fix that here. We assume that S_ISLNK is *always* defined as a macro. If that is not universally true, then add a test to the romio configure that trys to link a program that references S_ISLNK */#if !defined(S_ISLNK) # if defined(S_IFLNK) /* Check for the link bit */# define S_ISLNK(mode) ((mode) & S_IFLNK)# else /* no way to check if it is a link, so say false */# define S_ISLNK(mode) 0 # endif#endif/* ADIO_FileSysType_parentdir * * Returns pointer to string in dirnamep; that string is allocated with * strdup and must be free()'d. */static void ADIO_FileSysType_parentdir(char *filename, char **dirnamep){ int err; char *dir = NULL, *slash; struct stat statbuf; err = lstat(filename, &statbuf); if (err || (!S_ISLNK(statbuf.st_mode))) { /* no such file, or file is not a link; these are the "normal" * cases where we can just return the parent directory. */ dir = ADIOI_Strdup(filename); } else { /* filename is a symlink. we've presumably already tried * to stat it and found it to be missing (dangling link), * but this code doesn't care if the target is really there * or not. */ int namelen; char *linkbuf; linkbuf = ADIOI_Malloc(PATH_MAX+1); namelen = readlink(filename, linkbuf, PATH_MAX+1); if (namelen == -1) { /* something strange has happened between the time that * we determined that this was a link and the time that * we attempted to read it; punt and use the old name. */ dir = ADIOI_Strdup(filename); } else { /* successfully read the link */ linkbuf[namelen] = '\0'; /* readlink doesn't null terminate */ dir = ADIOI_Strdup(linkbuf); ADIOI_Free(linkbuf); } } slash = strrchr(dir, '/'); if (!slash) ADIOI_Strncpy(dir, ".", 2); else { if (slash == dir) *(dir + 1) = '\0'; else *slash = '\0'; } *dirnamep = dir; return;}#endif /* ROMIO_NTFS *//* ADIO_FileSysType_fncall - determines the file system type for a given file using a system-dependent function callInput Parameters:. filename - pointer to file name character arrayOutput Parameters:. fstype - location in which to store file system type (ADIO_XXX). error_code - location in which to store error code MPI_SUCCESS is stored in the location pointed to by error_code on success. This function is used by MPI_File_open() and MPI_File_delete() to determine file system type. Most other functions use the type which is stored when the file is opened. */static void ADIO_FileSysType_fncall(char *filename, int *fstype, int *error_code){#ifndef ROMIO_NTFS char *dir; int err;#endif#ifdef ROMIO_HAVE_STRUCT_STATVFS_WITH_F_BASETYPE struct statvfs vfsbuf;#endif#ifdef HAVE_STRUCT_STATFS struct statfs fsbuf;#endif#ifdef ROMIO_HAVE_STRUCT_STAT_WITH_ST_FSTYPE struct stat sbuf;#endif static char myname[] = "ADIO_RESOLVEFILETYPE_FNCALL"; *error_code = MPI_SUCCESS;#ifdef ROMIO_HAVE_STRUCT_STATVFS_WITH_F_BASETYPE do { err = statvfs(filename, &vfsbuf); } while (err && (errno == ESTALE)); if (err && (errno == ENOENT)) { /* ENOENT may be returned in two cases: * 1) no directory entry for "filename" * 2) "filename" is a dangling symbolic link * * ADIO_FileSysType_parentdir tries to deal with both cases. */ ADIO_FileSysType_parentdir(filename, &dir); err = statvfs(dir, &vfsbuf); ADIOI_Free(dir); } /* --BEGIN ERROR HANDLING-- */ if (err) { *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, myname, __LINE__, MPI_ERR_NO_SUCH_FILE, "**filename", "**filename %s", filename); return; } /* --END ERROR HANDLING-- */ /* FPRINTF(stderr, "%s\n", vfsbuf.f_basetype); */ if (!strncmp(vfsbuf.f_basetype, "nfs", 3)) { *fstype = ADIO_NFS; return; } if (!strncmp(vfsbuf.f_basetype, "xfs", 3)) { *fstype = ADIO_XFS; return; }# ifdef ROMIO_UFS /* if UFS support is enabled, default to that */ *fstype = ADIO_UFS; return;# endif /* --BEGIN ERROR HANDLING-- */ *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, myname, __LINE__, MPI_ERR_NO_SUCH_FILE, "**filename", "**filename %s", filename); /* --END ERROR HANDLING-- */#endif /* STATVFS APPROACH */#ifdef HAVE_STRUCT_STATFS do { err = statfs(filename, &fsbuf); } while (err && (errno == ESTALE)); if (err && (errno == ENOENT)) { ADIO_FileSysType_parentdir(filename, &dir); err = statfs(dir, &fsbuf); ADIOI_Free(dir); } /* --BEGIN ERROR HANDLING-- */ if (err) { *error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE, myname, __LINE__, MPI_ERR_NO_SUCH_FILE, "**filename", "**filename %s", filename); return; } /* --END ERROR HANDLING-- */# ifdef ROMIO_HAVE_STRUCT_STATFS_WITH_F_FSTYPENAME if ( !strncmp("nfs",fsbuf.f_fstypename,3) ) { *fstype = ADIO_NFS; return; }# endif /* FPRINTF(stderr, "%d\n", fsbuf.f_type);*/# ifdef NFS_SUPER_MAGIC if (fsbuf.f_type == NFS_SUPER_MAGIC) { *fstype = ADIO_NFS; return; }# endif# ifdef PAN_KERNEL_FS_CLIENT_SUPER_MAGIC if (fsbuf.f_type == PAN_KERNEL_FS_CLIENT_SUPER_MAGIC) { *fstype = ADIO_PANFS; return; }# endif# ifdef MOUNT_NFS if (fsbuf.f_type == MOUNT_NFS) { *fstype = ADIO_NFS; return; }# endif# ifdef MOUNT_PFS if (fsbuf.f_type == MOUNT_PFS) { *fstype = ADIO_PFS; return; }# endif# ifdef PVFS_SUPER_MAGIC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -