diff --git a/include/main.h b/include/main.h index 129493d..d6303e4 100644 --- a/include/main.h +++ b/include/main.h @@ -7,7 +7,8 @@ enum ResultCode { RC_OK = 0, // No problem (so far) RC_EXISTS = 1, // On upload, the file to be uploaded already existed RC_OPEN_FAILED = 2, // On upload, opening the file for writing failed - RC_WRONG_TARGET = 3 // On upload, the given URL was not a directory + RC_WRONG_TARGET = 3, // On upload, the given URL was not a directory + RC_WRITE_FAILED = 4 // On upload, a write operation to the file failed }; enum RequestType { @@ -23,6 +24,7 @@ struct ConnectionState { uint8_t uploadRequest; FILE *upload_fd; + char uploadFilename[PATH_MAX]; enum ResultCode result; enum RequestType requestType; diff --git a/src/main.c b/src/main.c index 670b9b8..c8ab643 100644 --- a/src/main.c +++ b/src/main.c @@ -231,7 +231,7 @@ int serv_result_page(struct MHD_Connection *connection, struct ConnectionState * int ret; char *message, *title; - unsigned int resultCode; + unsigned int resultCode = MHD_HTTP_OK; switch(connstate->result) { case RC_OK: @@ -257,6 +257,12 @@ int serv_result_page(struct MHD_Connection *connection, struct ConnectionState * title = "Target error"; resultCode = MHD_HTTP_INTERNAL_SERVER_ERROR; break; + + case RC_WRITE_FAILED: + message = "A write error occurred while storing uploaded data to the file."; + title = "Write error"; + resultCode = MHD_HTTP_INTERNAL_SERVER_ERROR; + break; } char *buffer = malloc(strlen(HEADER1 HEADER2 FOOTER) + 1024); @@ -299,40 +305,50 @@ static int post_processor(void *cls, if(strcmp("data", key) == 0) { if(ci->upload_fd == NULL) { // file pointer not allocated yet - // determine the path where to store the uploaded file - char uploadFilename[PATH_MAX]; + // check if we are storing the new file into a directory if(!S_ISDIR(ci->targetStat.st_mode)) { LOG(LVL_WARN, "Cannot upload %s: Target path is not an existing directory.", filename); ci->result = RC_WRONG_TARGET; return MHD_NO; } - strncpy(uploadFilename, ci->localFileName, PATH_MAX); - strncat(uploadFilename, "/", PATH_MAX); - strncat(uploadFilename, filename, PATH_MAX); + // determine the path where to store the uploaded file + strncpy(ci->uploadFilename, ci->localFileName, PATH_MAX); + strcat(ci->uploadFilename, "/"); + strncat(ci->uploadFilename, filename, PATH_MAX-strlen(ci->uploadFilename)); // check if the file already exists - FILE *tmp = fopen(uploadFilename, "rb"); + FILE *tmp = fopen(ci->uploadFilename, "rb"); if(tmp) { - LOG(LVL_WARN, "Cannot upload to %s: file exists.", uploadFilename); + LOG(LVL_WARN, "Cannot upload to %s: file exists.", ci->uploadFilename); ci->result = RC_EXISTS; fclose(tmp); return MHD_NO; } // file does not exist and can therefore be opened for writing - ci->upload_fd = fopen(uploadFilename, "wb"); + ci->upload_fd = fopen(ci->uploadFilename, "wb"); if(!ci->upload_fd) { - LOG(LVL_WARN, "Cannot upload to %s: file cannot be created.", uploadFilename); + LOG(LVL_WARN, "Cannot upload to %s: file cannot be created.", ci->uploadFilename); ci->result = RC_OPEN_FAILED; return MHD_NO; } - fwrite(data, size, 1, ci->upload_fd); + if(0 == fwrite(data, size, 1, ci->upload_fd)) { + LOG(LVL_WARN, "Cannot upload to %s: write operation failed.", ci->uploadFilename); + ci->result = RC_WRITE_FAILED; + return MHD_NO; + } + + LOG(LVL_WARN, "Upload to %s started successfully.", ci->uploadFilename); } else { // file descriptor is already open - fwrite(data, size, 1, ci->upload_fd); + if(0 == fwrite(data, size, 1, ci->upload_fd)) { + LOG(LVL_WARN, "Cannot upload to %s: write operation failed.", ci->uploadFilename); + ci->result = RC_WRITE_FAILED; + return MHD_NO; + } } } return MHD_YES;