int tar_append_tree(TAR *t, char *realdir, char *savedir) { char realpath[MAXPATHLEN]; char savepath[MAXPATHLEN]; struct dirent *dent; DIR *dp; struct stat s; #ifdef TAR_DEBUG printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n", t, realdir, (savedir ? savedir : "[NULL]")); #endif if (tar_append_file(t, realdir, savedir) != 0) return -1; #ifdef TAR_DEBUG puts(" tar_append_tree(): done with tar_append_file()..."); #endif dp = opendir(realdir); if (dp == NULL) { if (errno == ENOTDIR) return 0; return -1; } while ((dent = readdir(dp)) != NULL) { if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; snprintf(realpath, MAXPATHLEN, "%s/%s", realdir, dent->d_name); if (savedir) snprintf(savepath, MAXPATHLEN, "%s/%s", savedir, dent->d_name); if (lstat(realpath, &s) != 0) return -1; if (S_ISDIR(s.st_mode)) { if (tar_append_tree(t, realpath, (savedir ? savepath : NULL)) != 0) return -1; continue; } if (tar_append_file(t, realpath, (savedir ? savepath : NULL)) != 0) return -1; } closedir(dp); return 0; }
/** Archive a tar or tar.gz archive in \arg tarfile, from the path in \a srcpath. If \a verbose is true, echo a line on stdout for each file archived. The libtar and libz implementations are used. If the filename \a tarfile ends with the characters "gz" then gz stream i/o is used for storing the tar. If the \arg gzip is true, then gz will be used regardless of the filename. Otherwise the file is created as a normal .tar archive. The return result is true if the operation succeeded. */ bool targz_archive_all( const QString &tarfile, const QString &srcpath, bool gzip, bool verbose ) { QByteArray pathnameArr = tarfile.toLocal8Bit(); char *pathname = pathnameArr.data(); QByteArray prefixArr = srcpath.toLocal8Bit(); char *prefix = prefixArr.data(); TAR *tarHandle; int options = TAR_GNU; if ( verbose ) options |= TAR_VERBOSE; int filemode = 0; // only care about this if creating files (ie untar) tartype_t *arctype = 0; if ( gzip || tarfile.endsWith( "gz" )) arctype = &zlibtype; int result = tar_open( &tarHandle, pathname, arctype, O_WRONLY, filemode, options); if ( result < 0 ) { qWarning( "error opening tar file %s: %s", pathname, strerror(errno) ); return false; } result = tar_append_tree( tarHandle, prefix, prefix ); if ( result < 0 ) qWarning( "error archiving to tar file %s: %s", pathname, strerror(errno) ); tar_close( tarHandle ); return ( result >= 0 ); }
static int create(char *tarfile, char *rootdir, libtar_list_t *l) { TAR *t; char *pathname; char buf[TAR_MAXPATHLEN]; libtar_listptr_t lp; if (tar_open(&t, tarfile, #ifdef HAVE_LIBZ (use_zlib ? &gztype : NULL), #else NULL, #endif O_WRONLY | O_CREAT, 0644, (verbose ? TAR_VERBOSE : 0) | (use_gnu ? TAR_GNU : 0)) == -1) { fprintf(stderr, "tar_open(): %s\n", strerror(errno)); return -1; } libtar_listptr_reset(&lp); while (libtar_list_next(l, &lp) != 0) { pathname = (char *)libtar_listptr_data(&lp); if (pathname[0] != '/' && rootdir != NULL) snprintf(buf, sizeof(buf), "%s/%s", rootdir, pathname); else strlcpy(buf, pathname, sizeof(buf)); if (tar_append_tree(t, buf, pathname) != 0) { fprintf(stderr, "tar_append_tree(\"%s\", \"%s\"): %s\n", buf, pathname, strerror(errno)); tar_close(t); return -1; } } if (tar_append_eof(t) != 0) { fprintf(stderr, "tar_append_eof(): %s\n", strerror(errno)); tar_close(t); return -1; } if (tar_close(t) != 0) { fprintf(stderr, "tar_close(): %s\n", strerror(errno)); return -1; } return 0; }
static int do_add(struct dirent *d) { int ret; char realname[PATH_MAX]; char savename[PATH_MAX]; sprintf(realname, "%s%s/%s", base2, prefix, d->d_name); sprintf(savename, "%s%s/%s", "add", prefix, d->d_name); fprintf(stderr, "%s/%s was added\n", prefix, d->d_name); if(!t) return 0; if(d->d_type == DT_DIR) ret = tar_append_tree(t, realname, savename); else ret = tar_append_file(t, realname, savename); if(ret < 0) perror("tar_append_file"); }
static int tarupthelist(char *tarfile, char *rootdir, libtar_list_t *l) { TAR *t; char *pathname; char buf[TAR_MAXPATHLEN]; if (tar_open(&t, tarfile, NULL, O_WRONLY | O_CREAT, 0644, (use_gnu ? TAR_GNU : 0)) == -1) { fprintf(stderr, "tar_open(): %s\n", strerror(errno)); return -1; } libtar_listptr_t lp; libtar_listptr_reset(&lp); while (libtar_list_next(l, &lp) != 0) { pathname = (char *)libtar_listptr_data(&lp); if (pathname[0] != '/' && rootdir != NULL) snprintf(buf, sizeof(buf), "%s/%s", rootdir, pathname); else strncpy(buf, pathname, sizeof(buf)); if (tar_append_tree(t, buf, pathname) != 0) { fprintf(stderr, "tar_append_tree(\"%s\", \"%s\"): %s\n", buf, pathname, strerror(errno)); tar_close(t); return -1; } } if (tar_append_eof(t) != 0) { fprintf(stderr, "tar_append_eof(): %s\n", strerror(errno)); tar_close(t); return -1; } if (tar_close(t) != 0) { fprintf(stderr, "tar_close(): %s\n", strerror(errno)); return -1; } return 0; }
int mpk_package_packmpk(struct mpk_pkginfo *pkg, const char *srcdir, const char *outdir) { TAR *tar; BZFILE *bz2; FILE *tbz2_file; int tar_fd; int bzerr; char src[PATH_MAX + 1]; char dst[PATH_MAX + 1]; char tar_fpath[PATH_MAX + 1]; char tbz2_fpath[PATH_MAX + 1]; unsigned char buffer[CHUNKSIZE]; int size; struct mpk_file *file; /* create tar */ sprintf(tar_fpath, "/tmp/%s_files.tar", pkg->name); if (access(tar_fpath, F_OK) == 0) if (unlink(tar_fpath) != 0) goto err0; if (tar_open(&tar, tar_fpath, NULL, O_WRONLY|O_CREAT, 0644, 0) != 0) goto err0; for (file = pkg->tool.lh_first; file; file = file->items.le_next) { sprintf(src, "%s/tool/%s", srcdir, file->name); sprintf(dst, "tool/%s", file->name); if (tar_append_tree(tar, src, dst) != 0) goto err2; } for (file = pkg->data.lh_first; file; file = file->items.le_next) { if (file->type == MPK_FILE_TYPE_R || file->type == MPK_FILE_TYPE_EXE || file->type == MPK_FILE_TYPE_W || file->type == MPK_FILE_TYPE_S) { sprintf(src, "%s/data/%s", srcdir, file->name); sprintf(dst, "data/%s", file->name); if (tar_append_tree(tar, src, dst) != 0) goto err2; } } sprintf(src, "%s/manifest.txt", srcdir); if (tar_append_file(tar, src, "manifest.txt") != 0) goto err2; tar_close(tar); /* compress using bz2 */ int version_str_len = mpk_version_serializedsize(&pkg->version); char *version_str; if (!(version_str = malloc(version_str_len + 1))) goto err2; if (mpk_version_serialize(version_str, NULL, version_str_len, &pkg->version) != MPK_SUCCESS) { free(version_str); goto err2; } version_str[version_str_len] = 0; sprintf(tbz2_fpath, "%s/%s-%s.mpk", outdir, pkg->name, version_str); free(version_str); printf("path:%s\n", tbz2_fpath); if ((tar_fd = open(tar_fpath, O_RDONLY)) == -1) goto err1; if ((tbz2_file = fopen(tbz2_fpath, "wb")) == NULL) goto err3; bz2 = BZ2_bzWriteOpen(&bzerr, tbz2_file, 9, 0, 30); if (bzerr != BZ_OK) goto err4; while ((size = read(tar_fd, buffer, CHUNKSIZE)) > 0) BZ2_bzWrite(&bzerr, bz2, buffer, size); BZ2_bzWriteClose(&bzerr, bz2, 0, NULL, NULL); fclose(tbz2_file); close(tar_fd); if (bzerr != BZ_OK || size < 0) goto err1; if (unlink(tar_fpath) != 0) goto err0; return MPK_SUCCESS; err4: fclose(tbz2_file); err3: close(tar_fd); goto err1; err2: tar_close(tar); err1: unlink(tar_fpath); err0: return MPK_FAILURE; }
int tar_append_tree(TAR *t, char *realdir, char *savedir) { char realpath[TAR_MAXPATHLEN]; char savepath[TAR_MAXPATHLEN]; size_t plen; #if defined(HAVE_DIRENT_H) struct dirent *dent; DIR *dp; #else kwDirEntry * dent; kwDirectory *dp; #endif struct stat s; strncpy(realpath, realdir, sizeof(realpath)); realpath[sizeof(realpath)-1] = 0; plen = strlen(realpath); if ( realpath[plen-1] == '/' ) { realpath[plen-1] = 0; } #ifdef DEBUG printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n", t, realdir, (savedir ? savedir : "[NULL]")); #endif if (tar_append_file(t, realdir, savedir) != 0) return -1; #ifdef DEBUG puts(" tar_append_tree(): done with tar_append_file()..."); #endif if ( stat(realpath, &s) != 0 ) { return -1; } if ( #if defined(_WIN32) && !defined(__CYGWIN__) (s.st_mode & _S_IFDIR) == 0 #else !S_ISDIR(s.st_mode) #endif ) return 0; #if defined(HAVE_DIRENT_H) dp = opendir(realdir); #else dp = kwOpenDir(realdir); #endif if (dp == NULL) { if (errno == ENOTDIR) return 0; return -1; } #if defined(HAVE_DIRENT_H) while ((dent = readdir(dp)) != NULL) #else while ((dent = kwReadDir(dp)) != NULL) #endif { if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; snprintf(realpath, TAR_MAXPATHLEN, "%s/%s", realdir, dent->d_name); if (savedir) snprintf(savepath, TAR_MAXPATHLEN, "%s/%s", savedir, dent->d_name); #ifndef WIN32 if (lstat(realpath, &s) != 0) return -1; #else if (stat(realpath, &s) != 0) return -1; #endif if (S_ISDIR(s.st_mode)) { if (tar_append_tree(t, realpath, (savedir ? savepath : NULL)) != 0) return -1; continue; } if (tar_append_file(t, realpath, (savedir ? savepath : NULL)) != 0) return -1; } #if defined(HAVE_DIRENT_H) closedir(dp); #else kwCloseDir(dp); #endif return 0; }
ResponseCode UpdateRepoTagOfTarball(std::string pathTarball, std::string repo, std::string tag, std::string &idImage) { TAR *tar = NULL; int ret = 0; int th_found = 0; int exitcode = 0; // char tmp_filepath[] = P_tmpdir "/libtar-tmpfile-XXXXXX"; //gen path to upload image char* tmp_imagepath = tempnam (FileUtils::GetPathDir(pathTarball).c_str(), "imageDirTmp_"); std::string pathTmpDir = tmp_imagepath; free(tmp_imagepath); std::cout << "path tmp dir: " << pathTmpDir << std::endl; int tmp_fd = -1; ret = tar_open(&tar, (char*)pathTarball.c_str(), NULL, O_RDONLY, 0, 0); if (ret != 0) { fprintf(stdout, "Fail to open file: %s\n", pathTarball.c_str()); // return FILE_ACTION_ERROR; exitcode = 2; } if (exitcode == 0) { if (tar_extract_all(tar, (char*)pathTmpDir.c_str()) != 0) { fprintf(stderr, "Fail to extract file: %s\n", pathTarball.c_str()); exitcode = 2; } } if (exitcode == 0) { ret = tar_close(tar); if (ret != 0) { perror("Failed to close tar file."); exitcode = 2; } tar = NULL; } //Modify repository file if (exitcode == 0) { char buf[BUFSIZ]; ssize_t n_buf = 0; FILE *tmpfile = NULL; std::ifstream in((pathTmpDir + "/" + REPOSITORIES_FILE).c_str(), std::ios::binary); std::string info((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); JSONNode n; try { n = libjson::parse(info); } catch(const std::invalid_argument& e) { std::cerr << "Invalid argument: " << e.what() << '\n'; // return FILE_ACTION_ERROR; exitcode = 1; } if(exitcode == 0){ JSONNode::const_iterator i = n.begin(); //if (i != n.end() && i -> name() == TAG_SERVICE_STR && i -> type() != JSON_ARRAY && i -> type() != JSON_NODE) if (i != n.end() && i -> type() == JSON_NODE){ JSONNode tag_id_node = i->as_node(); i = tag_id_node.begin(); if (i != n.end() && i -> type() != JSON_ARRAY && i -> type() != JSON_NODE){ idImage = i->as_string(); }else{ std::cout << "Tarball format error.\n"; // return FILE_ACTION_ERROR; exitcode = 1; } } }else{ std::cout << "Tarball format error.\n"; // return FILE_ACTION_ERROR; exitcode = 1; } } if(exitcode == 0){ JSONNode newRepoNode(JSON_NODE); newRepoNode.set_name(repo); newRepoNode.push_back(JSONNode(tag, idImage)); JSONNode newNode; newNode.push_back(newRepoNode); std::string content = newNode.write(); FILE * pFile; if (exitcode == 0) { pFile = fopen ((pathTmpDir + "/" + REPOSITORIES_FILE).c_str() , "w"); if (pFile == NULL) { printf("Error opening file %s\n", (pathTmpDir + "/" + REPOSITORIES_FILE).c_str()); // return FILE_ACTION_ERROR; exitcode = 1; } } if (exitcode == 0) { fwrite (content.c_str() , sizeof(char), content.size(), pFile); fclose (pFile); printf("content tmp file: %s\n", content.c_str()); } } if (exitcode == 0) { remove (pathTarball.c_str()); ret = tar_open(&tar, (char*)pathTarball.c_str(), NULL, O_WRONLY | O_CREAT, 0600, 0); if (ret != 0) { fprintf(stderr, "Fail to open file: %s\n", pathTarball.c_str()); // return FILE_ACTION_ERROR; exitcode = 2; } if (exitcode == 0) { if (tar_append_tree(tar, (char*)pathTmpDir.c_str(), "") != 0) { fprintf(stderr, "Fail to compress file: %s\n", pathTarball.c_str()); // return FILE_ACTION_ERROR; exitcode = 2; } } if (exitcode == 0) { ret = tar_close(tar); if (ret != 0) { perror("Failed to close tar file."); exitcode = 2; } else { tar = NULL; } } } std::cout << "delete_folder_tree: " << pathTmpDir.c_str() << std::endl; if (FileUtils::delete_folder_tree(pathTmpDir.c_str())) { fprintf(stderr, "Fail to delete temp dir: %s\n", pathTmpDir.c_str()); } if (exitcode == 0) { return FILE_ACTION_SUCCESS; } else if (exitcode == 1) { return FILE_ACTION_ERROR; } else { return DATA_ERROR; } }