00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "server.h"
00037 #include <sys/types.h>
00038 #include <sys/stat.h>
00039 #include <fcntl.h>
00040 #include <unistd.h>
00041 #include <string.h>
00042 #include <stdio.h>
00043 #include "common.h"
00044 #include "fd_io_handler.h"
00045
00046 using namespace zmm;
00047 using namespace mxml;
00048
00049 FDIOHandler::FDIOHandler(String filename) : IOHandler()
00050 {
00051 this->filename = filename;
00052 this->fd = -1;
00053 this->other = nil;
00054 this->reference_list = Ref<Array<Object> >(new Array<Object >(4));
00055 this->closed = false;
00056 }
00057
00058 FDIOHandler::FDIOHandler(int fd) : IOHandler()
00059 {
00060 this->filename = nil;
00061 this->fd = fd;
00062 this->other = nil;
00063 this->reference_list = Ref<Array<Object> >(new Array<Object >(4));
00064 this->closed = false;
00065 }
00066
00067 void FDIOHandler::addReference(Ref<Object> reference)
00068 {
00069 reference_list->append(reference);
00070 }
00071
00072 void FDIOHandler::closeOther(Ref<IOHandler> other)
00073 {
00074 this->other = other;
00075 }
00076
00077 void FDIOHandler::open(IN enum UpnpOpenFileMode mode)
00078 {
00079
00080 if (fd != -1)
00081 {
00082 log_debug("Assuming valid fd %d\n", fd);
00083 return;
00084 }
00085
00086 if (!string_ok(filename))
00087 throw _Exception(_("Missing filename!"));
00088
00089 if (mode == UPNP_READ)
00090 {
00091 fd = ::open(filename.c_str(), O_RDONLY);
00092 }
00093 else if (mode == UPNP_WRITE)
00094 {
00095 fd = ::open(filename.c_str(), O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
00096 }
00097 else
00098 {
00099 throw _Exception(_("FDIOHandler::open: invdalid read/write mode"));
00100 }
00101
00102 if (fd == -1)
00103 {
00104 throw _Exception(_("FDIOHandler::open: failed to open: ") + filename.c_str());
00105 }
00106
00107 }
00108
00109 int FDIOHandler::read(OUT char *buf, IN size_t length)
00110 {
00111 int ret = 0;
00112
00113 ret = ::read(fd, buf, length);
00114
00115 return ret;
00116 }
00117
00118 int FDIOHandler::write(IN char *buf, IN size_t length)
00119 {
00120 int ret = 0;
00121
00122 ret = ::write(fd, buf, length);
00123
00124 return ret;
00125 }
00126
00127 void FDIOHandler::seek(IN off_t offset, IN int whence)
00128 {
00129 if (lseek(fd, offset, whence) != 0)
00130 {
00131 throw _Exception(_("fseek failed"));
00132 }
00133 }
00134
00135 void FDIOHandler::close()
00136 {
00137
00138 if (closed)
00139 return;
00140
00141 log_debug("Closing...\n");
00142 try
00143 {
00144 if (other != nil)
00145 other->close();
00146 }
00147 catch (Exception ex)
00148 {
00149 log_debug("Error closing \"other\" handler: %s\n", ex.getMessage().c_str());
00150 }
00151
00152
00153 if (::close(fd) != 0)
00154 {
00155 throw _Exception(_("fclose failed"));
00156 }
00157 fd = -1;
00158 closed = true;
00159 }