Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
DeviceFile.cxx
Go to the documentation of this file.
1
34#include "DeviceFile.hxx"
35
36#include <fcntl.h>
37
45int DeviceFile::open(File* file, const char *path, int flags, int mode)
46{
47 if (flags & O_APPEND || flags & O_TRUNC)
48 {
49 return -EINVAL;
50 }
51
52 if ((flags & O_CREAT) && (flags & O_EXCL))
53 {
54 return -EEXIST;
55 }
56
57 file->offset = 0;
58
59 return Node::open(file, path, flags, mode);
60}
61
68ssize_t DeviceFile::read(File *file, void *buf, size_t count)
69{
70 ssize_t result = 0;
71
72 if ((file->flags & O_ACCMODE) == O_WRONLY)
73 {
74 return -EBADF;
75 }
76
77 lock_.lock();
78 if (count > 0)
79 {
80 /* if there is anything left to read */
81 result = read(file->offset, buf, count);
82 if (result > 0)
83 {
84 file->offset += result;
85 }
86 }
87 lock_.unlock();
88
89 return result;
90}
91
98ssize_t DeviceFile::write(File *file, const void *buf, size_t count)
99{
100 ssize_t result = 0;
101
102 if ((file->flags & O_ACCMODE) == O_RDONLY)
103 {
104 return -EBADF;
105 }
106
107 lock_.lock();
108 if (count > 0)
109 {
110 /* if there is anything left to write */
111 result = write(file->offset, buf, count);
112 if (result > 0)
113 {
114 file->offset += result;
115 }
116 }
117 lock_.unlock();
118
119 return result;
120}
121
122
virtual ssize_t read(unsigned int index, void *buf, size_t len)=0
Read from the DeviceFile.
int open(File *file, const char *path, int flags, int mode) OVERRIDE
Open a device.
virtual ssize_t write(unsigned int index, const void *buf, size_t len)=0
Write to the DeviceFile.
int open(File *, const char *, int, int) OVERRIDE
Open method.
OSMutex lock_
protects internal structures.
Definition Devtab.hxx:588
void lock()
Lock a mutex.
Definition OS.hxx:446
void unlock()
Unlock a mutex.
Definition OS.hxx:453
File information.
Definition Devtab.hxx:52
off_t offset
current offset within file
Definition Devtab.hxx:62
int flags
open flags
Definition Devtab.hxx:63