--- libaitio/src/Attic/tools.c 2012/11/19 21:12:03 1.19 +++ libaitio/src/Attic/tools.c 2012/12/19 11:03:04 1.20 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tools.c,v 1.19 2012/11/19 21:12:03 misho Exp $ +* $Id: tools.c,v 1.20 2012/12/19 11:03:04 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -687,4 +687,95 @@ io_usleep(u_int usec) struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) }; return select(0, NULL, NULL, NULL, &tv); +} + +/* + * io_AV2Path() - Attribute/Value pair store to file + * + * @csPath = Directory + * @csAttr = Attribute + * @csVal = Value + * @update = Update if a/v exists + * @perm = File permissions, if =0 set default perm=0600 + * return: -1 error or >-1 written bytes + */ +int +io_AV2Path(const char *csPath, const char *csAttr, const char *csVal, + int update, int perm) +{ + int fd, wlen = 0; + char szFile[MAXPATHLEN]; + + if (!csAttr) + return -1; + else + memset(szFile, 0, sizeof szFile); + snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr); + + wlen = O_CREAT | O_WRONLY; + if (!update) + wlen |= O_EXCL; + fd = open(szFile, wlen, perm ? perm : 0600); + if (fd == -1) { + LOGERR; + return -1; + } else + wlen ^= wlen; + + if (csVal) + if ((wlen = write(fd, csVal, strlen(csVal))) == -1) { + LOGERR; + close(fd); + unlink(szFile); + return -1; + } + + close(fd); + return wlen; +} + +/* + * io_Path2AV() - Get stored Attribute/Value + * + * @csPath = Directory + * @csAttr = Attribute + * @psVal = Value + * @valLen = Value length + * @del = Delete a/v pair after read + * return: -1 error or >-1 readed bytes + */ +int +io_Path2AV(const char *csPath, const char *csAttr, char *psVal, int valLen, int del) +{ + int fd, rlen = 0; + char szFile[MAXPATHLEN]; + + if (!csAttr) + return -1; + else + memset(szFile, 0, sizeof szFile); + snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr); + + if (psVal && valLen) { + fd = open(szFile, O_RDONLY); + if (fd == -1) { + LOGERR; + return -1; + } else + rlen ^= rlen; + + memset(psVal, 0, valLen); + rlen = read(fd, psVal, valLen - 1); + if (rlen == -1) { + LOGERR; + close(fd); + return -1; + } + + close(fd); + } + + if (del) + unlink(szFile); + return rlen; }