Skip to content

Commit

Permalink
Tidy file handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
joan2937 committed Aug 17, 2020
1 parent 93965f8 commit 606d805
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pigpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -13282,7 +13282,7 @@ int fileOpen(char *file, unsigned mode)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int fd=-1;
int i, slot, oflag, omode;
int i, slot, oflag, omode, pmode, rmode;
struct stat statbuf;

DBG(DBG_USER, "file=%s mode=%d", file, mode);
Expand All @@ -13294,9 +13294,15 @@ int fileOpen(char *file, unsigned mode)
((mode & PI_FILE_RW) == 0) )
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);

if ((mode > 3) && ((mode & PI_FILE_WRITE) == 0))
SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to write file (%s)", file);

slot = -1;

pthread_mutex_lock(&mutex);
Expand All @@ -13320,7 +13326,6 @@ int fileOpen(char *file, unsigned mode)

if (mode & PI_FILE_APPEND)
{
mode |= PI_FILE_WRITE;
oflag |= O_APPEND;
}

Expand All @@ -13332,7 +13337,6 @@ int fileOpen(char *file, unsigned mode)

if (mode & PI_FILE_TRUNC)
{
mode |= PI_FILE_WRITE;
oflag |= O_TRUNC;
}

Expand Down Expand Up @@ -13517,7 +13521,7 @@ int fileList(char *fpat, char *buf, unsigned count)

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);

bufpos = 0;
Expand Down

0 comments on commit 606d805

Please sign in to comment.