File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / blob.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Jun 23 16:23:31 2010 UTC (14 years, 1 month ago) by misho
Branches: rpc1_0
added new functions

    1: #include "global.h"
    2: 
    3: 
    4: /*
    5:  * rpc_srv_blobMap() Map blob to memory region 
    6:  * @srv = RPC Server instance
    7:  * @blob = Map to this BLOB element
    8:  * return: -1 error or 0 ok
    9:  */
   10: inline int
   11: rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
   12: {
   13: 	int f;
   14: 	struct stat sb;
   15: 	char szFName[MAXPATHLEN];
   16: 
   17: 	if (!blob) {
   18: 		rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
   19: 		return -1;
   20: 	}
   21: 
   22: 	memset(szFName, 0, MAXPATHLEN);
   23: 	snprintf(szFName, MAXPATHLEN, BLOB_FILE, srv->srv_blob.dir, blob->blob_var);
   24: 	f = open(szFName, O_RDONLY);
   25: 	if (f == -1) {
   26: 		LOGERR;
   27: 		return -1;
   28: 	}
   29: 	if (fstat(f, &sb) == -1) {
   30: 		LOGERR;
   31: 		close(f);
   32: 		return -1;
   33: 	}
   34: 
   35: 	blob->blob_data = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, f, 0);
   36: 	if (blob->blob_data == MAP_FAILED) {
   37: 		LOGERR;
   38: 		close(f);
   39: 		blob->blob_data = NULL;
   40: 		return -1;
   41: 	} else {
   42: 		blob->blob_len = sb.st_size;
   43: 		close(f);
   44: 	}
   45: 
   46: 	return 0;
   47: }
   48: 
   49: /*
   50:  * rpc_srv_blobUnmap() Unmap blob memory region 
   51:  * @blob = Mapped BLOB element
   52:  * return: none
   53:  */
   54: inline void
   55: rpc_srv_blobUnmap(rpc_blob_t * __restrict blob)
   56: {
   57: 	if (!blob || !blob->blob_data)
   58: 		rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
   59: 	else {
   60: 		munmap(blob->blob_data, blob->blob_len);
   61: 		blob->blob_data = NULL;
   62: 		blob->blob_len = 0;
   63: 	}
   64: }
   65: 
   66: /*
   67:  * rpc_srv_blobFree() Free blob from disk & memory
   68:  * @srv = RPC Server instance
   69:  * @blob = Mapped BLOB element
   70:  * return: -1 error or 0 ok
   71:  */
   72: inline int
   73: rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
   74: {
   75: 	char szFName[MAXPATHLEN];
   76: 
   77: 	if (!blob) {
   78: 		rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
   79: 		return -1;
   80: 	}
   81: 
   82: 	if (blob->blob_data)
   83: 		rpc_srv_blobUnmap(blob);
   84: 
   85: 	memset(szFName, 0, MAXPATHLEN);
   86: 	snprintf(szFName, MAXPATHLEN, BLOB_FILE, srv->srv_blob.dir, blob->blob_var);
   87: 	if (remove(szFName) == -1) {
   88: 		LOGERR;
   89: 		return -1;
   90: 	}
   91: 
   92: 	return 0;
   93: }
   94: 
   95: // ------------------------------------------------------------
   96: 
   97: int
   98: rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_t *blob)
   99: {
  100: 	int ret = 0;
  101: 	uint8_t buf[BLOBSIZ];
  102: 
  103: 	if (!cli || !blob) {
  104: 		rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
  105: 		return -1;
  106: 	}
  107: 
  108: 	do {
  109: 	} while (ret);
  110: 
  111: 	return blob->blob_len;
  112: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>