int hdfsDelete(hdfsFS fs, const char* path, int recursive) { char *url = NULL, *absPath = NULL; struct Response *resp = NULL; int ret = 0; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } ret = createUrlForDELETE(fs->nn, fs->port, absPath, recursive, fs->userName, &url); if (ret) { goto done; } ret = launchDELETE(url, &resp); if (ret) { goto done; } ret = parseDELETE(resp->body->content); done: freeResponse(resp); free(absPath); free(url); if (ret) { errno = ret; return -1; } return 0; }
int hdfsUtime(hdfsFS fs, const char* path, tTime mtime, tTime atime) { char *url = NULL, *absPath = NULL; struct Response *resp = NULL; int ret = 0; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } ret = createUrlForUTIMES(fs->nn, fs->port, absPath, mtime, atime, fs->userName, &url); if (ret) { goto done; } ret = launchUTIMES(url, &resp); if (ret) { goto done; } ret = parseUTIMES(resp->header->content, resp->body->content); done: freeResponse(resp); free(absPath); free(url); if (ret) { errno = ret; return -1; } return 0; }
int hdfsChown(hdfsFS fs, const char* path, const char *owner, const char *group) { int ret = 0; char *absPath = NULL, *url = NULL; struct Response *resp = NULL; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } ret = createUrlForCHOWN(fs->nn, fs->port, absPath, owner, group, fs->userName, &url); if (ret) { goto done; } ret = launchCHOWN(url, &resp); if (ret) { goto done; } ret = parseCHOWN(resp->header->content, resp->body->content); done: freeResponse(resp); free(absPath); free(url); if (ret) { errno = ret; return -1; } return 0; }
int hdfsSetReplication(hdfsFS fs, const char* path, int16_t replication) { char *url = NULL, *absPath = NULL; struct Response *resp = NULL; int ret = 0; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } ret = createUrlForSETREPLICATION(fs->nn, fs->port, absPath, replication, fs->userName, &url); if (ret) { goto done; } ret = launchSETREPLICATION(url, &resp); if (ret) { goto done; } ret = parseSETREPLICATION(resp->body->content); done: freeResponse(resp); free(absPath); free(url); if (ret) { errno = ret; return -1; } return 0; }
int hdfsChmod(hdfsFS fs, const char* path, short mode) { char *absPath = NULL, *url = NULL; struct Response *resp = NULL; int ret = 0; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } ret = createUrlForCHMOD(fs->nn, fs->port, absPath, (int) mode, fs->userName, &url); if (ret) { goto done; } ret = launchCHMOD(url, &resp); if (ret) { goto done; } ret = parseCHMOD(resp->header->content, resp->body->content); done: freeResponse(resp); free(absPath); free(url); if (ret) { errno = ret; return -1; } return 0; }
int hdfsCreateDirectory(hdfsFS fs, const char* path) { char *url = NULL, *absPath = NULL; struct Response *resp = NULL; int ret = 0; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } ret = createUrlForMKDIR(fs->nn, fs->port, absPath, fs->userName, &url); if (ret) { goto done; } ret = launchMKDIR(url, &resp); if (ret) { goto done; } ret = parseMKDIR(resp->body->content); done: freeResponse(resp); free(url); free(absPath); if (ret) { errno = ret; return -1; } return 0; }
/** * Free the threadData struct instance, * including the response and url contained in it * @param data The threadData instance to free */ static void freeThreadData(threadData *data) { if (data) { if (data->url) { free(data->url); } if (data->resp) { freeResponse(data->resp); } // The uploadBuffer would be freed by freeWebFileHandle() free(data); data = NULL; } }
/** * Get the file status for a given path. * * @param fs hdfsFS handle containing * NameNode hostName/port information * @param path Path for file * @param printError Whether or not to print out error information * (mainly remote FileNotFoundException) * @return File information for the given path */ static hdfsFileInfo *hdfsGetPathInfoImpl(hdfsFS fs, const char* path, int printError) { char *absPath = NULL; char *url=NULL; struct Response *resp = NULL; int ret = 0; hdfsFileInfo *fileInfo = NULL; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } fileInfo = (hdfsFileInfo *) calloc(1, sizeof(hdfsFileInfo)); if (!fileInfo) { ret = ENOMEM; goto done; } fileInfo->mKind = kObjectKindFile; ret = createUrlForGetFileStatus(fs->nn, fs->port, absPath, fs->userName, &url); if (ret) { goto done; } ret = launchGFS(url, &resp); if (ret) { goto done; } ret = parseGFS(resp->body->content, fileInfo, printError); done: freeResponse(resp); free(absPath); free(url); if (ret == 0) { return fileInfo; } else { free(fileInfo); errno = ret; return NULL; } }
int main(void) { eLibrary_CLT_registerClient(); for (;;) { eLibrary_CLT_createRequest(str, &request); cache_hit = readCache(request, &response); if (false == cache_hit) { eLibrary_CLT_getResponse(); } fillCache(response); writeOutput(); freeResponse(); } eLibrary_CLT_destroyClient(); }
hdfsFileInfo *hdfsListDirectory(hdfsFS fs, const char* path, int *numEntries) { char *url = NULL, *absPath = NULL; struct Response *resp = NULL; int ret = 0; hdfsFileInfo *fileInfo = NULL; if (fs == NULL || path == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, path, &absPath); if (ret) { goto done; } fileInfo = calloc(1, sizeof(*fileInfo)); if (!fileInfo) { ret = ENOMEM; goto done; } ret = createUrlForLS(fs->nn, fs->port, absPath, fs->userName, &url); if (ret) { goto done; } ret = launchLS(url, &resp); if (ret) { goto done; } ret = parseLS(resp->body->content, &fileInfo, numEntries); done: freeResponse(resp); free(absPath); free(url); if (ret == 0) { return fileInfo; } else { errno = ret; return NULL; } }
int hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath) { char *oldAbsPath = NULL, *newAbsPath = NULL, *url = NULL; int ret = 0; struct Response *resp = NULL; if (fs == NULL || oldPath == NULL || newPath == NULL) { ret = EINVAL; goto done; } ret = getAbsolutePath(fs, oldPath, &oldAbsPath); if (ret) { goto done; } ret = getAbsolutePath(fs, newPath, &newAbsPath); if (ret) { goto done; } ret = createUrlForRENAME(fs->nn, fs->port, oldAbsPath, newAbsPath, fs->userName, &url); if (ret) { goto done; } ret = launchRENAME(url, &resp); if (ret) { goto done; } ret = parseRENAME(resp->body->content); done: freeResponse(resp); free(oldAbsPath); free(newAbsPath); free(url); if (ret) { errno = ret; return -1; } return 0; }
int main(int argc, char* argv[]) { // Parse command line arguments, use defaults when needed ArgumentStruct* args; args = parseArguments(argc, argv); // Struct to store response from CURL MessageStruct response; struct tm* lastUpdated = getTime(&response, args->url); // Preapre response for re-use freeResponse(response); // Get RSS feed from server if (args->update) { // If we're either not refetching the same URL, or it has updates if (args->ignoreTimestamp || fetchingNewURL(args) || args->previouslyFetchedURL == NULL || timeDiff(&(args->lastUpdated), lastUpdated)) { if(get(&response, args->url)) { printf("ERROR : Could not find feed at %s\n", args->url); freeArgs(args); return 1; } // Write result out to file if(storeFeed(response, args->dataFile, args->tempDirectory)) { printf("ERROR : Writing response to file (Running again should fix this)\n"); freeArgs(args); return 1; } } else { printf("No updates for feed : %s\n", args->url); freeArgs(args); return 0; } } // Create array of article sturctures RSSFeed* feed = NULL; feed = parseFeed(args); // Display feed displayFeed(feed, args); storeSettings(args, feed); // Free everything that has been dynamically allocated freeFeed(feed); freeResponse(response); freeArgs(args); return 0; }
/** * Helper function for opening a file for OUTPUT. * * As part of the open process for OUTPUT files, we have to connect to the * NameNode and get the URL of the corresponding DataNode. * We also create a background thread here for doing I/O. * * @param webhandle The webhandle being opened * @return 0 on success; error code otherwise */ static int hdfsOpenOutputFileImpl(hdfsFS fs, hdfsFile file) { struct webhdfsFileHandle *webhandle = file->file; struct Response *resp = NULL; int append, ret = 0; char *nnUrl = NULL, *dnUrl = NULL; threadData *data = NULL; ret = initWebHdfsBuffer(&webhandle->uploadBuffer); if (ret) { goto done; } append = file->flags & O_APPEND; if (!append) { // If we're not appending, send a create request to the NN ret = createUrlForNnWRITE(fs->nn, fs->port, webhandle->absPath, fs->userName, webhandle->replication, webhandle->blockSize, &nnUrl); } else { ret = createUrlForNnAPPEND(fs->nn, fs->port, webhandle->absPath, fs->userName, &nnUrl); } if (ret) { fprintf(stderr, "Failed to create the url connecting to namenode " "for file creation/appending, <%d>: %s.\n", ret, hdfs_strerror(ret)); goto done; } if (!append) { ret = launchNnWRITE(nnUrl, &resp); } else { ret = launchNnAPPEND(nnUrl, &resp); } if (ret) { fprintf(stderr, "fail to get the response from namenode for " "file creation/appending, <%d>: %s.\n", ret, hdfs_strerror(ret)); goto done; } if (!append) { ret = parseNnWRITE(resp->header->content, resp->body->content); } else { ret = parseNnAPPEND(resp->header->content, resp->body->content); } if (ret) { fprintf(stderr, "fail to parse the response from namenode for " "file creation/appending, <%d>: %s.\n", ret, hdfs_strerror(ret)); goto done; } ret = parseDnLoc(resp->header->content, &dnUrl); if (ret) { fprintf(stderr, "fail to get the datanode url from namenode " "for file creation/appending, <%d>: %s.\n", ret, hdfs_strerror(ret)); goto done; } //store the datanode url in the file handle webhandle->datanode = strdup(dnUrl); if (!webhandle->datanode) { ret = ENOMEM; goto done; } //create a new thread for performing the http transferring data = calloc(1, sizeof(*data)); if (!data) { ret = ENOMEM; goto done; } data->url = strdup(dnUrl); if (!data->url) { ret = ENOMEM; goto done; } data->flags = file->flags; data->uploadBuffer = webhandle->uploadBuffer; ret = pthread_create(&webhandle->connThread, NULL, writeThreadOperation, data); if (ret) { fprintf(stderr, "ERROR: failed to create the writing thread " "in hdfsOpenOutputFileImpl, <%d>: %s.\n", ret, hdfs_strerror(ret)); goto done; } webhandle->uploadBuffer->openFlag = 1; done: freeResponse(resp); free(nnUrl); free(dnUrl); if (ret) { errno = ret; if (data) { free(data->url); free(data); } } return ret; }