~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/cull/cull_file.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <fcntl.h>
 
35
#include <string.h>
 
36
#include <errno.h>
 
37
 
 
38
/* do not compile in monitoring code */
 
39
#ifndef NO_SGE_COMPILE_DEBUG
 
40
#define NO_SGE_COMPILE_DEBUG
 
41
#endif
 
42
 
 
43
#include "cull_list.h"
 
44
#include "cull_lerrnoP.h"
 
45
#include "cull_listP.h"
 
46
#include "cull_multitypeP.h"
 
47
#include "cull_whatP.h"
 
48
#include "cull_whereP.h"
 
49
#include "cull_pack.h"
 
50
#include "cull_parse.h"
 
51
#include "cull_file.h"
 
52
#include "sgermon.h"
 
53
#include "sge_log.h"
 
54
#include "sge_io.h"
 
55
#include "sge_unistd.h"
 
56
 
 
57
#include "msg_cull.h"
 
58
 
 
59
/****** cull/file/lWriteElemToDisk() ******************************************
 
60
*  NAME
 
61
*     lWriteElemToDisk() -- Writes a element to file 
 
62
*
 
63
*  SYNOPSIS
 
64
*     int lWriteElemToDisk(const lListElem *ep, const char *prefix, 
 
65
*                          const char *name, const char *obj_name) 
 
66
*
 
67
*  FUNCTION
 
68
*     Writes the Element 'ep' to the file named 'prefix'/'name'.
 
69
*     Either 'prefix' or 'name can be null. 
 
70
*
 
71
*  INPUTS
 
72
*     const lListElem *ep  - CULL element 
 
73
*     const char *prefix   - Path 
 
74
*     const char *name     - Filename 
 
75
*     const char *obj_name - 
 
76
*
 
77
*  RESULT
 
78
*     int - error state 
 
79
*         0 - OK
 
80
*         1 - Error
 
81
******************************************************************************/
 
82
int lWriteElemToDisk(const lListElem *ep, const char *prefix, const char *name,
 
83
                     const char *obj_name) 
 
84
{
 
85
   stringT filename;
 
86
   sge_pack_buffer pb;
 
87
   int ret, fd;
 
88
 
 
89
   DENTER(TOP_LAYER, "lWriteElemToDisk");
 
90
 
 
91
   if (!prefix && !name) {
 
92
      ERROR((SGE_EVENT, MSG_CULL_NOPREFIXANDNOFILENAMEINWRITEELMTODISK ));
 
93
      DEXIT;
 
94
      return 1;
 
95
   }
 
96
 
 
97
   /* init packing buffer */
 
98
   ret = init_packbuffer(&pb, 8192, 0);
 
99
 
 
100
   /* pack ListElement */
 
101
   if (ret == PACK_SUCCESS) {
 
102
      ret = cull_pack_elem(&pb, ep);
 
103
   }
 
104
 
 
105
   switch (ret) {
 
106
   case PACK_SUCCESS:
 
107
      break;
 
108
 
 
109
   case PACK_ENOMEM:
 
110
      ERROR((SGE_EVENT, MSG_CULL_NOTENOUGHMEMORYFORPACKINGXY_SS ,
 
111
             obj_name, name ? name : "null"));
 
112
      clear_packbuffer(&pb);
 
113
      DEXIT;
 
114
      return 1;
 
115
 
 
116
   case PACK_FORMAT:
 
117
      ERROR((SGE_EVENT, MSG_CULL_FORMATERRORWHILEPACKINGXY_SS ,
 
118
             obj_name, name ? name : "null"));
 
119
      clear_packbuffer(&pb);
 
120
      DEXIT;
 
121
      return 1;
 
122
 
 
123
   default:
 
124
      ERROR((SGE_EVENT, MSG_CULL_UNEXPECTEDERRORWHILEPACKINGXY_SS ,
 
125
             obj_name, name ? name : "null"));
 
126
      clear_packbuffer(&pb);
 
127
      DEXIT;
 
128
      return 1;
 
129
   }
 
130
 
 
131
   /* create full file name */
 
132
   if (prefix && name) {
 
133
      sprintf(filename, "%s/%s", prefix, name);
 
134
   } else if (prefix) {
 
135
      sprintf(filename, "%s", prefix);
 
136
   } else {
 
137
      sprintf(filename, "%s", name);
 
138
   }
 
139
 
 
140
   /* open file */
 
141
   if ((fd = SGE_OPEN3(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
 
142
      CRITICAL((SGE_EVENT, MSG_CULL_CANTOPENXFORWRITINGOFYZ_SSS ,
 
143
                filename, obj_name, strerror(errno)));
 
144
      clear_packbuffer(&pb);
 
145
      DEXIT;
 
146
      return 1;
 
147
   }
 
148
 
 
149
   /* write packing buffer */
 
150
   if (sge_writenbytes(fd, pb.head_ptr, pb_used(&pb)) != pb_used(&pb)) {
 
151
      CRITICAL((SGE_EVENT, MSG_CULL_CANTWRITEXTOFILEY_SS , obj_name, 
 
152
               filename));
 
153
      clear_packbuffer(&pb);
 
154
      close(fd);
 
155
      DEXIT;
 
156
      return 1;
 
157
   }
 
158
 
 
159
   /* close file and exit */
 
160
   close(fd);
 
161
   clear_packbuffer(&pb);
 
162
 
 
163
   DEXIT;
 
164
   return 0;
 
165
}
 
166
 
 
167
/****** cull/file/lReadElemFromDisk() ****************************************
 
168
*  NAME
 
169
*     lReadElemFromDisk() -- Reads a cull element from file 
 
170
*
 
171
*  SYNOPSIS
 
172
*     lListElem* lReadElemFromDisk(const char *prefix, 
 
173
*                                  const char *name, 
 
174
*                                  const lDescr *type, 
 
175
*                                  const char *obj_name) 
 
176
*
 
177
*  FUNCTION
 
178
*     Reads a lListElem of the specified 'type' from the file
 
179
*     'prefix'/'name'. Either 'prefix' or 'name' can be null.
 
180
*     Returns a pointer to the read element or NULL in case
 
181
*     of an error 
 
182
*
 
183
*  INPUTS
 
184
*     const char *prefix   - Path 
 
185
*     const char *name     - Filename 
 
186
*     const lDescr *type   - Type 
 
187
*     const char *obj_name - 
 
188
*
 
189
*  RESULT
 
190
*     lListElem* - Read CULL element
 
191
*******************************************************************************/
 
192
lListElem *lReadElemFromDisk(const char *prefix, const char *name, 
 
193
                             const lDescr *type, const char *obj_name) 
 
194
{
 
195
   stringT filename;
 
196
   sge_pack_buffer pb;
 
197
   SGE_STRUCT_STAT statbuf;
 
198
   lListElem *ep;
 
199
   int ret, fd;
 
200
   void* buf;
 
201
   size_t size;
 
202
 
 
203
   DENTER(TOP_LAYER, "lReadElemFromDisk");
 
204
 
 
205
   if (!prefix && !name) {
 
206
      ERROR((SGE_EVENT,  MSG_CULL_NOPREFIXANDNOFILENAMEINREADELEMFROMDISK ));
 
207
      DEXIT;
 
208
      return NULL;
 
209
   }
 
210
 
 
211
   /* create full file name */
 
212
   if (prefix && name)
 
213
      sprintf(filename, "%s/%s", prefix, name);
 
214
   else if (prefix)
 
215
      sprintf(filename, "%s", prefix);
 
216
   else
 
217
      sprintf(filename, "%s", name);
 
218
 
 
219
   /* get file size */
 
220
   if (SGE_STAT(filename, &statbuf) == -1) {
 
221
      CRITICAL((SGE_EVENT, MSG_CULL_CANTGETFILESTATFORXFILEY_SS , obj_name, filename));
 
222
      DEXIT;
 
223
      return NULL;
 
224
   }
 
225
 
 
226
   if (!statbuf.st_size) {
 
227
      CRITICAL((SGE_EVENT, MSG_CULL_XFILEYHASZEROSIYE_SS , obj_name, filename));
 
228
      DEXIT;
 
229
      return NULL;
 
230
   }
 
231
 
 
232
   /* init packing buffer */
 
233
   size = statbuf.st_size;
 
234
   if (((SGE_OFF_T)size != statbuf.st_size)
 
235
       || !(buf = malloc(statbuf.st_size))) {
 
236
      CRITICAL((SGE_EVENT, MSG_CULL_LEMALLOC));
 
237
      clear_packbuffer(&pb);
 
238
      DEXIT;
 
239
      return NULL;
 
240
   }
 
241
 
 
242
   /* open file */
 
243
   if ((fd = SGE_OPEN2(filename, O_RDONLY)) < 0) {
 
244
      CRITICAL((SGE_EVENT, MSG_CULL_CANTREADXFROMFILEY_SS , obj_name, filename));
 
245
      clear_packbuffer(&pb);    /* this one frees buf */
 
246
      DEXIT;
 
247
      return NULL;
 
248
   }
 
249
 
 
250
   /* read packing buffer */
 
251
   if (sge_readnbytes(fd, buf, statbuf.st_size) != statbuf.st_size) {
 
252
      CRITICAL((SGE_EVENT, MSG_CULL_ERRORREADINGXINFILEY_SS , obj_name, filename));
 
253
      close(fd);
 
254
      DEXIT;
 
255
      return NULL;
 
256
   }
 
257
 
 
258
   if((ret = init_packbuffer_from_buffer(&pb, buf, statbuf.st_size)) != PACK_SUCCESS) {
 
259
      ERROR((SGE_EVENT, MSG_CULL_ERRORININITPACKBUFFER_S, cull_pack_strerror(ret)));
 
260
   }
 
261
   ret = cull_unpack_elem(&pb, &ep, type);
 
262
   close(fd);
 
263
   clear_packbuffer(&pb);     /* this one frees buf */
 
264
 
 
265
   switch (ret) {
 
266
   case PACK_SUCCESS:
 
267
      break;
 
268
 
 
269
   case PACK_ENOMEM:
 
270
      ERROR((SGE_EVENT, MSG_CULL_NOTENOUGHMEMORYFORUNPACKINGXY_SS ,
 
271
             obj_name, filename));
 
272
      DEXIT;
 
273
      return NULL;
 
274
 
 
275
   case PACK_FORMAT:
 
276
      ERROR((SGE_EVENT, MSG_CULL_FORMATERRORWHILEUNPACKINGXY_SS ,
 
277
             obj_name, filename));
 
278
      DEXIT;
 
279
      return NULL;
 
280
 
 
281
   case PACK_BADARG:
 
282
      ERROR((SGE_EVENT, MSG_CULL_BADARGUMENTWHILEUNPACKINGXY_SS ,
 
283
             obj_name, filename));
 
284
      DEXIT;
 
285
      return NULL;
 
286
 
 
287
   default:
 
288
      ERROR((SGE_EVENT, MSG_CULL_UNEXPECTEDERRORWHILEUNPACKINGXY_SS ,
 
289
             obj_name, filename));
 
290
      DEXIT;
 
291
      return NULL;
 
292
   }
 
293
 
 
294
   DEXIT;
 
295
   return ep;
 
296
}