check_fs.c 8.33 KB
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*------------------------------------------------------------------------
 * Copyright (C) 2011 For the list of authors, see file AUTHORS.
 *
 * This file is part of SOFI2D.
 * 
 * SOFI2D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
 * SOFI2D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SOFI2D. See file COPYING and/or 
  * <http://www.gnu.org/licenses/gpl-2.0.html>.
--------------------------------------------------------------------------*/
/*-------------------------------------------------------------
 *  Check file system of SOFI2D (Part I)
 *
 *  Check
 *  - command line parameters
 *  - existence of parameter file(s) and if they are readable
 *  - existence of directories and if they are read-/writable
 *  
 *  Part II:
 *    depending on the content of the input file(s) further
 *    checks are done in read_par() resp. read_par_auto()
 *
 *  -----------------------------------------------------------*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "fd.h"


void check_fs(FILE *fp, int argc, char *fileinp)
{

	FILE *fpinp;
	char modestr[40], infostr[40], fdflt[40]="", errmsg[80];
	int runmd=0, finplen, fserr=0;


	/********************************************/
	/* Check number of commandline parameters   */
	/********************************************/
	
	if (argc < 2)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: Missing commandline parameter for SOFI2D!\n");
		fprintf(fp, "         Parameter: name of input file");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	else if (argc > 2)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: Too many commandline parameters for SOFI2D (%d)!\n", argc-1);
		fprintf(fp, "         SOFI2D only accepts one parameter (name of input file)!");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	
	
	/********************************************/
	/* Check parameter file(s)                  */
	/********************************************/
	
	/* >>> input file for standard usage of SOFI2D */
	if (access(fileinp,0) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The stated input file does not exist!\n");
		fprintf(fp, "         File name: <%s>", fileinp);
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	else if (access(fileinp,4) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The stated input file does not have read access!\n");
		fprintf(fp, "         File name: <%s>", fileinp);
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	
	
	/* >>> input file for auto mode of SOFI2D */
	/* Open parameter-file to check if auto mode or not*/
	fpinp = fopen(fileinp,"r");
	fscanf(fpinp, "%s %s = %i", infostr, modestr, &runmd);
	fclose(fpinp);
	if (runmd == 1)
	{
		/* construct file name for default input file, for example sofi2D_auto.inp -> sofi2D_auto.default */
		finplen = strlen(fileinp);
		strncpy(fdflt, fileinp, finplen-3);
		fdflt[finplen] = '\0';
		strcat(fdflt, "default");
		
		/* check access */
		if (access(fdflt,0) != 0)
		{
			fprintf(fp, "\n==================================================================\n");
			fprintf(fp, "  ERROR: The following parameter file does not exist!\n");
			fprintf(fp, "         File name: <%s>\n", fdflt);
			fprintf(fp, "         Auto mode of SOFI2D requires this file!");
			fprintf(fp, "\n==================================================================\n");
			fserr = 1;
		}
		else if (access(fdflt,4) != 0)
		{
			fprintf(fp, "\n==================================================================\n");
			fprintf(fp, "  ERROR: The following parameter file does not have read access!\n");
			fprintf(fp, "         File name: <%s>\n", fdflt);
			fprintf(fp, "         Auto mode of SOFI2D requires this file!");
			fprintf(fp, "\n==================================================================\n");
			fserr = 1;
		}
	}	
	
	
	/********************************************/
	/* Check subdirectories                     */
	/********************************************/
	if (access("su",0) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The subdirectory <../par/su/> for seismogram storage does\n");
		fprintf(fp, "         not exist!");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	else if (access("su",2) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The subdirectory <../par/su/> does not have write access!\n");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
		
	if (access("snap",0) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The subdirectory <../par/snap/> for snapshot storage does\n");
		fprintf(fp, "         not exist!");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	else if (access("snap",2) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The subdirectory <../par/snap/> does not have write access!\n");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	
	if (access("model",0) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The subdirectory <../par/model/> for model storage does\n");
		fprintf(fp, "         not exist!");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	else if (access("model",6) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The subdirectory <../par/model/> does not have read and/or\n");
		fprintf(fp, "         write access!");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	
	if (access("log",0) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The log directory <../par/log/> does not exist!\n");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	else if (access("log",2) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The subdirectory <../par/log/> does not have write access!\n");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}

	/********************************************/
	/* Check directory ../fsofi2D/par/       */
	/********************************************/
	if (access("../par",0) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The directory <../par/> does not exist\n");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}
	else if (access("../par",2) != 0)
	{
		fprintf(fp, "\n==================================================================\n");
		fprintf(fp, "  ERROR: The directory <../par/> does not have write access!\n");
		fprintf(fp, "\n==================================================================\n");
		fserr = 1;
	}



	/********************************************/
	/* ERROR                                    */
	/********************************************/
	if (fserr)
	{
		fprintf(fp, "\n");
		sprintf(errmsg, "\n  in: <check_fs.c> \n");
Tilman Steinweg's avatar
Tilman Steinweg committed
222
		declare_error(errmsg);
Tilman Steinweg's avatar
Tilman Steinweg committed
223
224
225
	}	

}