mirror of https://github.com/joan2937/pigpio
Tidy file handling.
This commit is contained in:
parent
ad83b33426
commit
031decc9f6
14
pigpio.c
14
pigpio.c
|
@ -13282,7 +13282,7 @@ int fileOpen(char *file, unsigned mode)
|
||||||
{
|
{
|
||||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
int fd=-1;
|
int fd=-1;
|
||||||
int i, slot, oflag, omode;
|
int i, slot, oflag, omode, pmode, rmode;
|
||||||
struct stat statbuf;
|
struct stat statbuf;
|
||||||
|
|
||||||
DBG(DBG_USER, "file=%s mode=%d", file, mode);
|
DBG(DBG_USER, "file=%s mode=%d", file, mode);
|
||||||
|
@ -13294,9 +13294,15 @@ int fileOpen(char *file, unsigned mode)
|
||||||
((mode & PI_FILE_RW) == 0) )
|
((mode & PI_FILE_RW) == 0) )
|
||||||
SOFT_ERROR(PI_BAD_FILE_MODE, "bad mode (%d)", mode);
|
SOFT_ERROR(PI_BAD_FILE_MODE, "bad mode (%d)", mode);
|
||||||
|
|
||||||
if ((fileApprove(file) & mode) == PI_FILE_NONE)
|
pmode = fileApprove(file); // 0=NONE, 1=READ, 2=WRITE, 3=RW
|
||||||
|
rmode = mode & PI_FILE_RW; // 0=NONE, 1=READ, 2=WRITE, 3=RW
|
||||||
|
|
||||||
|
if (((pmode & rmode) != rmode) || (rmode == PI_FILE_NONE))
|
||||||
SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to access file (%s)", file);
|
SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to access file (%s)", file);
|
||||||
|
|
||||||
|
if ((mode > 3) && ((mode & PI_FILE_WRITE) == 0))
|
||||||
|
SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to write file (%s)", file);
|
||||||
|
|
||||||
slot = -1;
|
slot = -1;
|
||||||
|
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
|
@ -13320,7 +13326,6 @@ int fileOpen(char *file, unsigned mode)
|
||||||
|
|
||||||
if (mode & PI_FILE_APPEND)
|
if (mode & PI_FILE_APPEND)
|
||||||
{
|
{
|
||||||
mode |= PI_FILE_WRITE;
|
|
||||||
oflag |= O_APPEND;
|
oflag |= O_APPEND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13332,7 +13337,6 @@ int fileOpen(char *file, unsigned mode)
|
||||||
|
|
||||||
if (mode & PI_FILE_TRUNC)
|
if (mode & PI_FILE_TRUNC)
|
||||||
{
|
{
|
||||||
mode |= PI_FILE_WRITE;
|
|
||||||
oflag |= O_TRUNC;
|
oflag |= O_TRUNC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13517,7 +13521,7 @@ int fileList(char *fpat, char *buf, unsigned count)
|
||||||
|
|
||||||
CHECK_INITED;
|
CHECK_INITED;
|
||||||
|
|
||||||
if (fileApprove(fpat) == PI_FILE_NONE)
|
if ((fileApprove(fpat) & PI_FILE_READ) != PI_FILE_READ)
|
||||||
SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to access file (%s)", fpat);
|
SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to access file (%s)", fpat);
|
||||||
|
|
||||||
bufpos = 0;
|
bufpos = 0;
|
||||||
|
|
Loading…
Reference in New Issue