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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
/* do not compile in monitoring code */
#ifndef NO_SGE_COMPILE_DEBUG
#define NO_SGE_COMPILE_DEBUG
#endif
#include "uti/sge_rmon.h"
#include "uti/sge_log.h"
#include "uti/sge_io.h"
#include "uti/sge_unistd.h"
#include "uti/sge_profiling.h"
#include "cull/cull_list.h"
#include "cull/cull_lerrnoP.h"
#include "cull/cull_listP.h"
#include "cull/cull_multitypeP.h"
#include "cull/cull_whatP.h"
#include "cull/cull_whereP.h"
#include "cull/cull_pack.h"
#include "cull/cull_parse.h"
#include "cull/cull_file.h"
#include "cull/msg_cull.h"
/****** cull/file/lWriteElemToDisk() ******************************************
* NAME
* lWriteElemToDisk() -- Writes a element to file
*
* SYNOPSIS
* int lWriteElemToDisk(const lListElem *ep, const char *prefix,
* const char *name, const char *obj_name)
*
* FUNCTION
* Writes the Element 'ep' to the file named 'prefix'/'name'.
* Either 'prefix' or 'name can be null.
*
* INPUTS
* const lListElem *ep - CULL element
* const char *prefix - Path
* const char *name - Filename
* const char *obj_name -
*
* RESULT
* int - error state
* 0 - OK
* 1 - Error
******************************************************************************/
int lWriteElemToDisk(const lListElem *ep, const char *prefix, const char *name,
const char *obj_name)
{
char filename[SGE_PATH_MAX];
sge_pack_buffer pb;
int ret, fd;
DENTER(TOP_LAYER, "lWriteElemToDisk");
if (!prefix && !name) {
ERROR((SGE_EVENT, SFNMAX, MSG_CULL_NOPREFIXANDNOFILENAMEINWRITEELMTODISK));
DEXIT;
return 1;
}
/* init packing buffer */
ret = init_packbuffer(&pb, 8192, 0);
/* pack ListElement */
if (ret == PACK_SUCCESS) {
ret = cull_pack_elem(&pb, ep);
}
switch (ret) {
case PACK_SUCCESS:
break;
case PACK_ENOMEM:
ERROR((SGE_EVENT, MSG_CULL_NOTENOUGHMEMORYFORPACKINGXY_SS,
obj_name, name ? name : "null"));
clear_packbuffer(&pb);
DEXIT;
return 1;
case PACK_FORMAT:
ERROR((SGE_EVENT, MSG_CULL_FORMATERRORWHILEPACKINGXY_SS ,
obj_name, name ? name : "null"));
clear_packbuffer(&pb);
DEXIT;
return 1;
default:
ERROR((SGE_EVENT, MSG_CULL_UNEXPECTEDERRORWHILEPACKINGXY_SS ,
obj_name, name ? name : "null"));
clear_packbuffer(&pb);
DEXIT;
return 1;
}
/* create full file name */
if (prefix && name) {
snprintf(filename, sizeof(filename), "%s/%s", prefix, name);
} else if (prefix) {
snprintf(filename, sizeof(filename), "%s", prefix);
} else {
snprintf(filename, sizeof(filename), "%s", name);
}
PROF_START_MEASUREMENT(SGE_PROF_SPOOLINGIO);
/* open file */
if ((fd = SGE_OPEN3(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
CRITICAL((SGE_EVENT, MSG_CULL_CANTOPENXFORWRITINGOFYZ_SSS ,
filename, obj_name, strerror(errno)));
clear_packbuffer(&pb);
PROF_STOP_MEASUREMENT(SGE_PROF_SPOOLINGIO);
DRETURN(1);
}
/* write packing buffer */
if (sge_writenbytes(fd, pb.head_ptr, pb_used(&pb)) != pb_used(&pb)) {
CRITICAL((SGE_EVENT, MSG_CULL_CANTWRITEXTOFILEY_SS , obj_name,
filename));
clear_packbuffer(&pb);
close(fd);
PROF_STOP_MEASUREMENT(SGE_PROF_SPOOLINGIO);
DRETURN(1);
}
/* close file and exit */
close(fd);
PROF_STOP_MEASUREMENT(SGE_PROF_SPOOLINGIO);
clear_packbuffer(&pb);
DRETURN(0);
}
/****** cull/file/lReadElemFromDisk() ****************************************
* NAME
* lReadElemFromDisk() -- Reads a cull element from file
*
* SYNOPSIS
* lListElem* lReadElemFromDisk(const char *prefix,
* const char *name,
* const lDescr *type,
* const char *obj_name)
*
* FUNCTION
* Reads a lListElem of the specified 'type' from the file
* 'prefix'/'name'. Either 'prefix' or 'name' can be null.
* Returns a pointer to the read element or NULL in case
* of an error
*
* INPUTS
* const char *prefix - Path
* const char *name - Filename
* const lDescr *type - Type
* const char *obj_name -
*
* RESULT
* lListElem* - Read CULL element
*******************************************************************************/
lListElem *lReadElemFromDisk(const char *prefix, const char *name,
const lDescr *type, const char *obj_name)
{
char filename[SGE_PATH_MAX];
sge_pack_buffer pb;
SGE_STRUCT_STAT statbuf;
lListElem *ep;
int ret, fd;
void* buf;
size_t size;
DENTER(TOP_LAYER, "lReadElemFromDisk");
if (!prefix && !name) {
ERROR((SGE_EVENT, SFNMAX, MSG_CULL_NOPREFIXANDNOFILENAMEINREADELEMFROMDISK));
DEXIT;
return NULL;
}
/* create full file name */
if (prefix && name)
snprintf(filename, sizeof(filename), "%s/%s", prefix, name);
else if (prefix)
snprintf(filename, sizeof(filename), "%s", prefix);
else
snprintf(filename, sizeof(filename), "%s", name);
/* get file size */
if (SGE_STAT(filename, &statbuf) == -1) {
CRITICAL((SGE_EVENT, MSG_CULL_CANTGETFILESTATFORXFILEY_SS , obj_name, filename));
DEXIT;
return NULL;
}
if (!statbuf.st_size) {
CRITICAL((SGE_EVENT, MSG_CULL_XFILEYHASZEROSIYE_SS , obj_name, filename));
DEXIT;
return NULL;
}
/* init packing buffer */
size = statbuf.st_size;
if (((off_t)size != statbuf.st_size)
|| !(buf = malloc(statbuf.st_size))) {
CRITICAL((SGE_EVENT, SFNMAX, MSG_CULL_LEMALLOC));
clear_packbuffer(&pb);
DEXIT;
return NULL;
}
/* open file */
if ((fd = SGE_OPEN2(filename, O_RDONLY)) < 0) {
CRITICAL((SGE_EVENT, MSG_CULL_CANTREADXFROMFILEY_SS , obj_name, filename));
clear_packbuffer(&pb); /* this one frees buf */
DEXIT;
return NULL;
}
/* read packing buffer */
if (sge_readnbytes(fd, buf, statbuf.st_size) != statbuf.st_size) {
CRITICAL((SGE_EVENT, MSG_CULL_ERRORREADINGXINFILEY_SS , obj_name, filename));
close(fd);
DEXIT;
return NULL;
}
if((ret = init_packbuffer_from_buffer(&pb, buf, statbuf.st_size)) != PACK_SUCCESS) {
ERROR((SGE_EVENT, MSG_CULL_ERRORININITPACKBUFFER_S, cull_pack_strerror(ret)));
}
ret = cull_unpack_elem(&pb, &ep, type);
close(fd);
clear_packbuffer(&pb); /* this one frees buf */
switch (ret) {
case PACK_SUCCESS:
break;
case PACK_ENOMEM:
ERROR((SGE_EVENT, MSG_CULL_NOTENOUGHMEMORYFORUNPACKINGXY_SS ,
obj_name, filename));
DEXIT;
return NULL;
case PACK_FORMAT:
ERROR((SGE_EVENT, MSG_CULL_FORMATERRORWHILEUNPACKINGXY_SS ,
obj_name, filename));
DEXIT;
return NULL;
case PACK_BADARG:
ERROR((SGE_EVENT, MSG_CULL_BADARGUMENTWHILEUNPACKINGXY_SS ,
obj_name, filename));
DEXIT;
return NULL;
default:
ERROR((SGE_EVENT, MSG_CULL_UNEXPECTEDERRORWHILEUNPACKINGXY_SS ,
obj_name, filename));
DEXIT;
return NULL;
}
DEXIT;
return ep;
}
|