shfp_fname.c 2.72 KB
Newer Older
1
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2
3
4
5
6
7
8
9
/* 
 *
 *   Copyright (C) 1997 University of Chicago. 
 *   See COPYRIGHT notice in top-level directory.
 */

#include "adio.h"

10
#ifdef HAVE_UNISTD_H
11
#include <unistd.h>
12
13
#endif
#ifdef HAVE_SYS_TYPES_H
14
#include <sys/types.h>
15
#endif
16

17
18
19
#ifdef HAVE_TIME_H
#include <time.h>
#endif
20
21
22
23
24
25
26
27
28
/* The following function selects the name of the file to be used to 
   store the shared file pointer. The shared-file-pointer file is a 
   hidden file in the same directory as the real file being accessed.
   If the real file is /tmp/thakur/testfile, the shared-file-pointer
   file will be /tmp/thakur/.testfile.shfp.xxxx, where xxxx is
   a random number. This file is created only if the shared
   file pointer functions are used and is deleted when the real
   file is closed. */

29
void ADIOI_Shfp_fname(ADIO_File fd, int rank, int *error_code)
30
{
31
32
    int i;
    int len;
33
    char *slash, *ptr, tmp[128];
34
    int pid = 0;
35
36
37
38

    fd->shared_fp_fname = (char *) ADIOI_Malloc(256);

    if (!rank) {
39
40
        srand(time(NULL));
        i = rand();
41
	pid = (int)getpid();
42
	
43
44
45
46
47
	if (ADIOI_Strncpy(fd->shared_fp_fname, fd->filename, 256)) {
	    *error_code = ADIOI_Err_create_code("ADIOI_Shfp_fname",
		    fd->filename, ENAMETOOLONG);
	    return;
	}
48
49
50
51
52
53
54
	
#ifdef ROMIO_NTFS
	slash = strrchr(fd->filename, '\\');
#else
	slash = strrchr(fd->filename, '/');
#endif
	if (!slash) {
55
56
57
58
59
60
61
62
63
64
	    if (ADIOI_Strncpy(fd->shared_fp_fname, ".", 2)) {
		*error_code = ADIOI_Err_create_code("ADIOI_Shfp_fname",
			fd->filename, ENAMETOOLONG);
		return;
	    }
	    if (ADIOI_Strncpy(fd->shared_fp_fname + 1, fd->filename, 255)) {
		*error_code = ADIOI_Err_create_code("ADIOI_Shfp_fname",
			fd->filename, ENAMETOOLONG);
		return;
	    }
65
66
67
68
69
70
71
72
	}
	else {
	    ptr = slash;
#ifdef ROMIO_NTFS
		slash = strrchr(fd->shared_fp_fname, '\\');
#else
	    slash = strrchr(fd->shared_fp_fname, '/');
#endif
73
74
75
76
77
	    if (ADIOI_Strncpy(slash + 1, ".", 2))  {
		*error_code = ADIOI_Err_create_code("ADIOI_Shfp_fname",
			fd->filename, ENAMETOOLONG);
		return;
	    }
78
79
	    /* ok to cast: file names bounded by PATH_MAX and NAME_MAX */
	    len = (int) (256 - (slash+2 - fd->shared_fp_fname));
80
81
82
83
84
	    if (ADIOI_Strncpy(slash + 2, ptr + 1, len)) {
		*error_code = ADIOI_Err_create_code("ADIOI_Shfp_fname",
			ptr + 1, ENAMETOOLONG);
		return;
	    }
85
86
	}
	    
87
	ADIOI_Snprintf(tmp, 128, ".shfp.%d.%d", pid, i);
88
	/* ADIOI_Strnapp will return non-zero if truncated.  That's ok */
89
90
91
92
93
94
95
96
97
98
99
	ADIOI_Strnapp(fd->shared_fp_fname, tmp, 256);
	
	len = (int)strlen(fd->shared_fp_fname);
	MPI_Bcast(&len, 1, MPI_INT, 0, fd->comm);
	MPI_Bcast(fd->shared_fp_fname, len+1, MPI_CHAR, 0, fd->comm);
    }
    else {
	MPI_Bcast(&len, 1, MPI_INT, 0, fd->comm);
	MPI_Bcast(fd->shared_fp_fname, len+1, MPI_CHAR, 0, fd->comm);
    }
}