iotcloudroot(NULL),
dir(NULL),
length(0),
+ firstentry(0),
+ lastentry(0),
+ numqueueentries(DEFAULT_SIZE),
fd(-1)
{
}
-
IoTQuery::~IoTQuery() {
- if (fd >= 0) {
+ if (fd >= 0)
close(fd);
- }
if (directory)
delete directory;
if (data)
if (dir != NULL)
closedir(dir);
}
-
+
+int IoTQuery::checkDirectory() {
+ struct stat s;
+ int err=stat(directory, &s);
+ if (-1 == err)
+ return 0;
+ return S_ISDIR(s.st_mode);
+}
+
void IoTQuery::processQuery() {
parseQuery();
getDirectory();
return;
if (directory == NULL ||
- (dir = opendir(directory)) == NULL)
+ checkDirectory())
return;
- if (openMaxFile() < 0)
+ if (openStatusFile() < 0)
return;
flock(fd, LOCK_EX);
-
+
cout << "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
} while (!cin.eof());
}
-
void IoTQuery::parseQuery() {
uri = FCGX_GetParam(uri_str, request->envp);
query = FCGX_GetParam(query_str, request->envp);
directory[directory_len]=0;
}
-int IoTQuery::openMaxFile() {
- char maxfile[]="queuesize";
+int doread(int fd, void *ptr, size_t count, off_t offset) {
+ do {
+ size_t bytesread=pread(fd, ptr, count, offset);
+ if (bytesread==count) {
+ return 1;
+ } else if (bytesread==0) {
+ return 0;
+ }
+ } while(1);
+}
+
+void IoTQuery::updateStatusFile() {
+ pwrite(fd, &numqueueentries, sizeof(numqueueentries), OFFSET_MAX);
+ pwrite(fd, &firstentry, sizeof(firstentry), OFFSET_FIRST);
+ pwrite(fd, &lastentry, sizeof(lastentry), OFFSET_LAST);
+}
+
+int IoTQuery::openStatusFile() {
+ char statusfile[]="queuestatus";
int len=strlen(directory);
- char * filename=new char[len+sizeof(maxfile)+2];
+ char * filename=new char[len+sizeof(statusfile)+2];
memcpy(filename, directory, len);
filename[len]='/';
- memcpy(filename+len+1, maxfile, sizeof(maxfile));
- filename[len+sizeof(maxfile)+1]=0;
+ memcpy(filename+len+1, statusfile, sizeof(statusfile));
+ filename[len+sizeof(statusfile)+1]=0;
fd=open(filename, O_CREAT| O_RDWR, S_IRUSR| S_IWUSR);
delete filename;
+
+ if (fd < 0)
+ return fd;
+
+ int size;
+ int needwrite=0;
+ if (doread(fd, &size, sizeof(size), OFFSET_MAX))
+ numqueueentries=size;
+ else
+ needwrite=1;
+
+ long entry;
+ if (doread(fd, &entry, sizeof(entry), OFFSET_FIRST))
+ firstentry=entry;
+ else
+ needwrite=1;
+
+ if (doread(fd, &entry, sizeof(entry), OFFSET_LAST))
+ lastentry=entry;
+ else
+ needwrite=1;
+
+ if (needwrite)
+ updateStatusFile();
+
return fd;
}
+
+