add function fc_safe_read and fc_safe_write

pull/37/head
yuqing 2017-02-16 16:34:54 +08:00
parent f5a1e6f320
commit 16455133f3
4 changed files with 69 additions and 8 deletions

View File

@ -1,7 +1,8 @@
Version 1.35 2017-02-15
Version 1.35 2017-02-16
* logger judge log_level in function log_it_ex and log_it_ex1
* add php extension function: fastcommon_file_put_contents
* add function fc_safe_read and fc_safe_write
Version 1.34 2017-02-06
* ini_file_reader: LOCAL_IP support CIDR addresses

View File

@ -1164,11 +1164,11 @@ static PHPFileContext *fc_get_file_context(const char *filename)
return alloc_file_context(filename);
}
static int fc_file_put_contents(const char *filename,
static ssize_t fc_file_put_contents(const char *filename,
const char *data, const int data_len, const long flags)
{
PHPFileContext *ctx;
int bytes;
ssize_t bytes;
ctx = fc_get_file_context(filename);
if (ctx == NULL) {
@ -1226,7 +1226,7 @@ ZEND_FUNCTION(fastcommon_file_put_contents)
if ((flags == PHP_FILE_APPEND || flags == (PHP_FILE_APPEND | PHP_LOCK_EX))
&& (Z_TYPE_P(zdata) == IS_STRING) && (zcontext == NULL))
{
int bytes;
ssize_t bytes;
if ((bytes=fc_file_put_contents(filename, Z_STRVAL_P(zdata),
Z_STRLEN_P(zdata), flags)) >= 0)
{

View File

@ -2449,8 +2449,8 @@ bool isTrailingSpacesLine(const char *tail, const char *end)
ssize_t fc_safe_write(int fd, const char *buf, const size_t nbyte)
{
int n;
int remain;
ssize_t n;
ssize_t remain;
const char *p;
n = write(fd, buf, nbyte);
@ -2474,7 +2474,7 @@ ssize_t fc_safe_write(int fd, const char *buf, const size_t nbyte)
n = write(fd, p, remain);
if (n < 0)
{
int written;
ssize_t written;
if (errno == EINTR)
{
continue;
@ -2494,7 +2494,7 @@ ssize_t fc_safe_write(int fd, const char *buf, const size_t nbyte)
ssize_t fc_lock_write(int fd, const char *buf, const size_t nbyte)
{
int lock_result;
int result;
ssize_t result;
lock_result = file_write_lock(fd);
result = fc_safe_write(fd, buf, nbyte);
@ -2505,3 +2505,54 @@ ssize_t fc_lock_write(int fd, const char *buf, const size_t nbyte)
return result;
}
ssize_t fc_safe_read(int fd, char *buf, const size_t count)
{
ssize_t n;
ssize_t remain;
char *p;
n = read(fd, buf, count);
if (n < 0)
{
if (errno != EINTR)
{
return -1;
}
n = 0;
}
else
{
if (n == 0 || n == count)
{
return n;
}
}
p = buf + n;
remain = count - n;
while (remain > 0)
{
n = read(fd, p, remain);
if (n < 0)
{
ssize_t done;
if (errno == EINTR)
{
continue;
}
done = count - remain;
return done > 0 ? done : -1;
}
else if (n == 0)
{
break;
}
p += n;
remain -= n;
}
return count - remain;
}

View File

@ -642,6 +642,15 @@ ssize_t fc_safe_write(int fd, const char *buf, const size_t nbyte);
*/
ssize_t fc_lock_write(int fd, const char *buf, const size_t nbyte);
/** read from file
* parameters:
* fd: the fd to read
* buf: the buffer
* count: expect read bytes
* return: read bytes for success, -1 when fail
*/
ssize_t fc_safe_read(int fd, char *buf, const size_t count);
#ifdef __cplusplus
}
#endif