示例#1
0
int acl_cfg_parser_dump(const ACL_CFG_PARSER *parser,
			const char *pathname,
			const char *delimiter)
{
	char  myname[] = "acl_cfg_parser_dump";
	ACL_CFG_LINE *cfg_line;
	ACL_FILE_HANDLE filefd = ACL_FILE_INVALID;
	int   i, n, ret;
	char  tbuf[256];

#undef	RETURN
#define	RETURN(x) do { \
	if (filefd != ACL_FILE_INVALID) \
		acl_file_close(filefd); \
	return (x); \
} while (0);

	if (parser == NULL || pathname == NULL || *pathname == 0)
		return (-1);
#ifdef ACL_UNIX
# ifdef ACL_ANDROID
	filefd = acl_file_open(pathname,
			O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
# else
	filefd = acl_file_open(pathname,
			O_CREAT | O_TRUNC | O_APPEND | O_WRONLY,
			S_IREAD | S_IWRITE | S_IRGRP);
# endif
#elif defined(ACL_WINDOWS)
	filefd = acl_file_open(pathname,
		O_CREAT | O_TRUNC | O_APPEND | O_WRONLY,
		S_IREAD | S_IWRITE);
#else
# error "unknown OS"
#endif
	if (filefd == ACL_FILE_INVALID) {
		printf("%s: can't open, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		RETURN(-1);
	}

	n = acl_array_size(parser->_cfg_array);
	for (i = 0; i < n; i++) {
		cfg_line = (ACL_CFG_LINE *)
			acl_array_index(parser->_cfg_array, i);
		if (cfg_line == NULL)
			break;
		ret = _cfg_line_dump(filefd, cfg_line, delimiter);
		if (ret < 0) {
			RETURN (-1);
		}
	}

	RETURN (0);
#ifdef ACL_BCB_COMPILER
	return (0);
#endif
}
示例#2
0
ACL_VSTREAM *private_vstream_fopen(const char *path, unsigned int oflags,
	int mode, size_t buflen)
{
	ACL_VSTREAM *fp;
	ACL_FILE_HANDLE fh;

	/* for linux2.6 */
#ifdef  _LARGEFILE64_SOURCE
	oflags |= O_LARGEFILE;
#endif

#ifdef	ACL_WINDOWS
	oflags |= O_BINARY;
#endif

	fh = acl_file_open(path, oflags, mode);

	if (fh == ACL_FILE_INVALID)
		return (NULL);

	fp = private_vstream_fdopen(ACL_SOCKET_INVALID,
		oflags, buflen, 0, ACL_VSTREAM_TYPE_FILE);
	if (fp == NULL)
		return (NULL);

	fp->fd.h_file = fh;
	snprintf(fp->addr_peer, MAX_ADDR_SIZE, "%s", path);
	return (fp);
}
示例#3
0
	bool fstream::open(const char* path, unsigned int oflags, int mode)
	{
		if (path == NULL)
			return (false);

		ACL_FILE_HANDLE fh;

		fh = acl_file_open(path, oflags, mode);
		if (fh == ACL_FILE_INVALID)
			return (false);

		m_pStream->fread_fn  = acl_file_read;
		m_pStream->fwrite_fn = acl_file_write;
		m_pStream->fwritev_fn = acl_file_writev;
		m_pStream->fclose_fn = acl_file_close;

		m_pStream->fd.h_file = fh;
		m_pStream->type = ACL_VSTREAM_TYPE_FILE;
		m_pStream->oflags = oflags;
		ACL_SAFE_STRNCPY(m_pStream->remote_addr, path,
			sizeof(m_pStream->remote_addr));
		m_bOpened = true;
		m_bEof = false;
		return (true);
	}
示例#4
0
文件: locker.cpp 项目: 1514louluo/acl
bool locker::open(const char* file_path)
{
	acl_assert(file_path && *file_path);
	acl_assert(pFile_ == NULL);
	acl_assert(fHandle_ == ACL_FILE_INVALID);

	fHandle_ = acl_file_open(file_path, O_RDWR | O_CREAT, 0600);
	if (fHandle_ == ACL_FILE_INVALID)
		return false;
	myFHandle_ = true;
	pFile_ = acl_mystrdup(file_path);
	return true;
}
示例#5
0
static ACL_FILE_HANDLE __mylock_open(const char *filename)
{
    ACL_FILE_HANDLE   fd;
    int   flag = O_WRONLY|O_APPEND|O_CREAT;
#ifdef ACL_UNIX
    int   mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
#else
    int   mode = 0600;
#endif

    fd = acl_file_open(filename, flag, mode);
    return (fd);
}
示例#6
0
TLS_PRNG_SRC *tls_prng_exch_open(const char *name)
{
    const char *myname = "tls_prng_exch_open";
    TLS_PRNG_SRC *eh;
    ACL_FILE_HANDLE     fd;

    if ((fd = acl_file_open(name, O_RDWR | O_CREAT, 0600)) < 0)
	acl_msg_fatal("%s: cannot open PRNG exchange file %s: %s",
		myname, name, acl_last_serror());
    eh = (TLS_PRNG_SRC *) acl_mymalloc(sizeof(*eh));
    eh->fd.file = fd;
    eh->name = acl_mystrdup(name);
    eh->timeout = 0;
    if (acl_msg_verbose)
	acl_msg_info("%s: opened PRNG exchange file %s", myname, name);
    return (eh);
}
示例#7
0
文件: main.c 项目: 1514louluo/acl
int main(int argc, char *argv[])
{
	char  filename[256];
	int   ch, debug = 0;
	ssize_t max = 8192;
	ACL_FILE_HANDLE fd;

	snprintf(filename, sizeof(filename), "mmap.local");

	while ((ch = getopt(argc, argv, "hf:n:d")) > 0) {
		switch (ch) {
		case 'h':
			usage(argv[0]);
			return 0;
		case 'f':
			snprintf(filename, sizeof(filename), "%s", optarg);
			break;
		case 'n':
			max = atol(optarg);
			break;
		case 'd':
			debug = 1;
			break;
		default:
			break;
		}
	}

	acl_msg_stdout_enable(1);

	printf("filename: %s, max size: %ld\r\n", filename, (long) max);

	fd = acl_file_open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600);

	if (fd  == ACL_FILE_INVALID) {
		printf("open %s error %s\r\n", filename, acl_last_serror());
		return 1;
	}

	test_string(fd, max, debug);

	acl_file_close(fd);
	return 0;
}
示例#8
0
TLS_PRNG_SRC *tls_prng_dev_open(const char *name, int timeout)
{
    const char *myname = "tls_prng_dev_open";
    TLS_PRNG_SRC *dev;
    ACL_FILE_HANDLE     fd;

    if ((fd = acl_file_open(name, O_RDONLY, 0)) < 0) {
	if (acl_msg_verbose)
	    acl_msg_info("%s: cannot open entropy device %s: %s",
		myname, name, acl_last_serror());
	return (0);
    } else {
	dev = (TLS_PRNG_SRC *) acl_mymalloc(sizeof(*dev));
	dev->fd.file = fd;
	dev->name = acl_mystrdup(name);
	dev->timeout = timeout;
	if (acl_msg_verbose)
	    acl_msg_info("%s: opened entropy device %s", myname, name);
	return (dev);
    }
}
示例#9
0
ACL_CFG_PARSER *acl_cfg_parser_load(const char *pathname, const char *delimiter)
{
	char  myname[] = "acl_cfg_parse_load";
	ACL_CFG_PARSER *parser = NULL;
	ACL_CFG_LINE *cfg_line;
	struct stat stat_buf;
	int   buf_size;
	char *content_buf = NULL, *ptr;
	char *pline_begin;
	ACL_FILE_HANDLE   filefd = ACL_FILE_INVALID;
	char  tbuf[256];
	
#undef	ERETURN
#define	ERETURN(x) do { \
	if (content_buf != NULL) \
		acl_myfree(content_buf); \
	if (filefd != ACL_FILE_INVALID) \
		acl_file_close(filefd); \
	if (parser != NULL) { \
		acl_array_destroy(parser->_cfg_array, NULL); \
		acl_myfree(parser); \
	} \
	return (x); \
} while (0);

#undef	RETURN
#define	RETURN(x) do { \
	if (content_buf != NULL) \
		acl_myfree(content_buf); \
	if (filefd != ACL_FILE_INVALID) \
		acl_file_close(filefd); \
	return (x); \
} while (0);

	if (pathname == NULL || *pathname == 0) {
		printf("%s: invalid pathname\n", myname);
		return (NULL);
	}

	if (stat(pathname, &stat_buf) < 0) {
		printf("%s: can't stat, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}

	parser = (ACL_CFG_PARSER *) acl_mycalloc(1, sizeof(*parser));
	if (parser == NULL) {
		printf("%s: can't calloc ACL_CFG_PARSER, pathname=%s, errmsg=%s",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}

	parser->_cfg_array = acl_array_create(10);
	if (parser->_cfg_array == NULL) {
		printf("%s: can't create array, pathname=%s, errmsg=%s",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}
	parser->total_line = 0;
	parser->valid_line = 0;

	buf_size = (int) stat_buf.st_size + 256;
	content_buf = (char *) acl_mycalloc(1, buf_size);
	if (content_buf == NULL) {
		printf("%s: can't calloc, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}
	
#ifdef ACL_UNIX
# ifdef ACL_ANDROID
	filefd = acl_file_open(pathname, O_RDWR, 0644);
# else
	filefd = acl_file_open(pathname, O_RDWR, S_IREAD | S_IWRITE | S_IRGRP);
# endif
#elif defined(ACL_WINDOWS)
	filefd = acl_file_open(pathname, O_RDWR, S_IREAD | S_IWRITE);
#else
# error "unknown OS"
#endif

	if (filefd == ACL_FILE_INVALID) {
		printf("%s: can't open, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));

		ERETURN (NULL);
	}

	if (_cfg_file_load(filefd, content_buf, buf_size) < 0) {
		printf("%s: can't read, pathname=%s, errmsg=%s\n",
			myname, pathname, acl_last_strerror(tbuf, sizeof(tbuf)));
		ERETURN (NULL);
	}

#undef	SKIP
#define SKIP(var, cond) \
        for (; *var && (cond); var++) {}

	ptr = content_buf;
	while (*ptr) {
		pline_begin = ptr;  /* keep the line header */
		/* first, skip all ' ' and '\t' */
		SKIP(ptr, (*ptr == ' ' || *ptr == '\t'));

		if  (*ptr == '#') {  /* the comment line */
			SKIP(ptr, *ptr != '\n'); /* find the line's end */
			if (*ptr) {  /* this must be '\n' */
				*ptr++ = 0;  /* set '\0' and skip one byte */
			}

			cfg_line = _backup_junk_line(pline_begin);
			if (cfg_line == NULL)
				ERETURN (NULL);
			if (acl_array_append(parser->_cfg_array,
					(void *) cfg_line) < 0) {
				printf("%s: can't add ACL_CFG_LINE to array, "
					"errmsg=%s", myname, acl_last_strerror(tbuf, sizeof(tbuf)));
				ERETURN (NULL);
			}
			parser->total_line++;
			cfg_line->line_number = parser->total_line;
			continue;
		} else if (*ptr == '\r' || *ptr == '\n') {
			/* SKIP(ptr, (*ptr == '\r' || *ptr == '\n')); */
			if (*ptr == '\r' && *(ptr + 1) == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr += 2;
			} else if (*ptr == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr++;
			}

			cfg_line = _backup_junk_line(pline_begin);
			if (cfg_line == NULL)
				ERETURN (NULL);
			if (acl_array_append(parser->_cfg_array,
					(void *) cfg_line) < 0) {
				printf("%s: can't add ACL_CFG_LINE to array, "
					"errmsg=%s", myname, acl_last_strerror(tbuf, sizeof(tbuf)));
				ERETURN (NULL);
			}
			parser->total_line++;
			cfg_line->line_number = parser->total_line;
			continue;
		}

		pline_begin = ptr;  /* reset the line header */

		/* find the line's end */
		SKIP(ptr, (*ptr != '\n' && *ptr != '\r'));
		if (*ptr) {  /* this must be '\r' or '\n' */
			if (*ptr == '\r' && *(ptr + 1) == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr += 2;
			} else if (*ptr == '\n') {
				*ptr = 0; /* set '\0' first and go on */
				ptr++;
			}
		}

		/* make ptr to the next line's beginning */
		/* SKIP(ptr, (*ptr == '\r' || *ptr == '\n')); */

		cfg_line = _create_cfg_line(pline_begin, delimiter);
		if (cfg_line == NULL)
			ERETURN (NULL);
		if (acl_array_append(parser->_cfg_array, (void *) cfg_line) < 0) {
			printf("%s: can't add ACL_CFG_LINE to array, errmsg=%s",
				myname, acl_last_strerror(tbuf, sizeof(tbuf)));
			ERETURN (NULL);
		}
		parser->total_line++;
		parser->valid_line++;
		cfg_line->line_number = parser->total_line;
	}

	if (parser->total_line != acl_array_size(parser->_cfg_array)) {
		printf("%s: total_line=%d, acl_array_size=%d, errmsg=not equal\n",
			myname, parser->total_line,
			acl_array_size(parser->_cfg_array));
	}

	RETURN (parser);
#ifdef ACL_BCB_COMPILER
	return (NULL);
#endif
}