static int file_open(struct archive *a, void *client_data) { int flags; struct write_file_data *mine; struct stat st; mine = (struct write_file_data *)client_data; flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* * Open the file. */ if (mine->filename[0] != '\0') { mine->fd = open(mine->filename, flags, 0666); if (mine->fd < 0) { archive_set_error(a, errno, "Failed to open '%s'", mine->filename); return (ARCHIVE_FATAL); } } else { /* * NULL filename is stdout. */ mine->fd = 1; /* By default, pad archive when writing to stdout. */ if (archive_write_get_bytes_in_last_block(a) < 0) archive_write_set_bytes_in_last_block(a, 0); } if (fstat(mine->fd, &st) != 0) { archive_set_error(a, errno, "Couldn't stat '%s'", mine->filename); return (ARCHIVE_FATAL); } /* * Set up default last block handling. */ if (archive_write_get_bytes_in_last_block(a) < 0) { if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode)) /* Pad last block when writing to device or FIFO. */ archive_write_set_bytes_in_last_block(a, 0); else /* Don't pad last block otherwise. */ archive_write_set_bytes_in_last_block(a, 1); } /* * If the output file is a regular file, don't add it to * itself. If it's a device file, it's okay to add the device * entry to the output archive. */ if (S_ISREG(st.st_mode)) archive_write_set_skip_file(a, st.st_dev, st.st_ino); return (ARCHIVE_OK); }
static int file_open(struct archive *a, void *client_data) { struct write_fd_data *mine; struct stat st; mine = (struct write_fd_data *)client_data; if (fstat(mine->fd, &st) != 0) { archive_set_error(a, errno, "Couldn't stat fd %d", mine->fd); return (ARCHIVE_FATAL); } /* * If this is a regular file, don't add it to itself. */ if (S_ISREG(st.st_mode)) archive_write_set_skip_file(a, st.st_dev, st.st_ino); /* * If client hasn't explicitly set the last block handling, * then set it here. */ if (archive_write_get_bytes_in_last_block(a) < 0) { /* If the output is a block or character device, fifo, * or stdout, pad the last block, otherwise leave it * unpadded. */ if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode) || (mine->fd == 1)) /* Last block will be fully padded. */ archive_write_set_bytes_in_last_block(a, 0); else archive_write_set_bytes_in_last_block(a, 1); } return (ARCHIVE_OK); }
static int file_open(struct archive *a, void *client_data) { int flags; struct write_file_data *mine; struct stat st; #if defined(_WIN32) && !defined(__CYGWIN__) wchar_t *fullpath; #endif const wchar_t *wcs; const char *mbs; mine = (struct write_file_data *)client_data; flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC; /* * Open the file. */ mbs = NULL; wcs = NULL; #if defined(_WIN32) && !defined(__CYGWIN__) if (archive_mstring_get_wcs(a, &mine->filename, &wcs) != 0) { if (errno == ENOMEM) archive_set_error(a, errno, "No memory"); else { archive_mstring_get_mbs(a, &mine->filename, &mbs); archive_set_error(a, errno, "Can't convert '%s' to WCS", mbs); } return (ARCHIVE_FATAL); } fullpath = __la_win_permissive_name_w(wcs); if (fullpath != NULL) { mine->fd = _wopen(fullpath, flags, 0666); free(fullpath); } else mine->fd = _wopen(wcs, flags, 0666); #else if (archive_mstring_get_mbs(a, &mine->filename, &mbs) != 0) { if (errno == ENOMEM) archive_set_error(a, errno, "No memory"); else { archive_mstring_get_wcs(a, &mine->filename, &wcs); archive_set_error(a, errno, "Can't convert '%S' to MBS", wcs); } return (ARCHIVE_FATAL); } mine->fd = open(mbs, flags, 0666); __archive_ensure_cloexec_flag(mine->fd); #endif if (mine->fd < 0) { if (mbs != NULL) archive_set_error(a, errno, "Failed to open '%s'", mbs); else archive_set_error(a, errno, "Failed to open '%S'", wcs); return (ARCHIVE_FATAL); } if (fstat(mine->fd, &st) != 0) { if (mbs != NULL) archive_set_error(a, errno, "Couldn't stat '%s'", mbs); else archive_set_error(a, errno, "Couldn't stat '%S'", wcs); return (ARCHIVE_FATAL); } /* * Set up default last block handling. */ if (archive_write_get_bytes_in_last_block(a) < 0) { if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || S_ISFIFO(st.st_mode)) /* Pad last block when writing to device or FIFO. */ archive_write_set_bytes_in_last_block(a, 0); else /* Don't pad last block otherwise. */ archive_write_set_bytes_in_last_block(a, 1); } /* * If the output file is a regular file, don't add it to * itself. If it's a device file, it's okay to add the device * entry to the output archive. */ if (S_ISREG(st.st_mode)) archive_write_set_skip_file(a, st.st_dev, st.st_ino); return (ARCHIVE_OK); }
////////////////////////////////////////////////////////////////////// // Constructor: static int ar_write(lua_State *L) { struct archive** self_ref; static struct { const char *name; int (*setter)(struct archive *); } names[] = { /* Copied from archive_write_set_format_by_name.c */ { "ar", archive_write_set_format_ar_bsd }, { "arbsd", archive_write_set_format_ar_bsd }, { "argnu", archive_write_set_format_ar_svr4 }, { "arsvr4", archive_write_set_format_ar_svr4 }, { "cpio", archive_write_set_format_cpio }, { "mtree", archive_write_set_format_mtree }, { "newc", archive_write_set_format_cpio_newc }, { "odc", archive_write_set_format_cpio }, { "pax", archive_write_set_format_pax }, { "posix", archive_write_set_format_pax }, { "shar", archive_write_set_format_shar }, { "shardump", archive_write_set_format_shar_dump }, { "ustar", archive_write_set_format_ustar }, /* New ones to more closely match the C API */ { "ar_bsd", archive_write_set_format_ar_bsd }, { "ar_svr4", archive_write_set_format_ar_svr4 }, { "cpio_newc", archive_write_set_format_cpio_newc }, { "pax_restricted", archive_write_set_format_pax_restricted }, { "shar_dump", archive_write_set_format_shar_dump }, { NULL, NULL } }; int idx = 0; const char* name; luaL_checktype(L, 1, LUA_TTABLE); self_ref = (struct archive**) lua_newuserdata(L, sizeof(struct archive*)); // {ud} luaL_getmetatable(L, AR_WRITE); // {ud}, [write] lua_setmetatable(L, -2); // {ud} __ref_count++; *self_ref = archive_write_new(); // Register it in the weak metatable: ar_registry_set(L, *self_ref); // Create an environment to store a reference to the writer: lua_createtable(L, 1, 0); // {ud}, {} lua_pushliteral(L, "writer"); // {ud}, {}, "writer" lua_rawget(L, 1); // {ud}, {}, fn if ( ! lua_isfunction(L, -1) ) { err("MissingArgument: required parameter 'writer' must be a function"); } lua_setfield(L, -2, "writer"); lua_setfenv(L, -2); // {ud} // Extract various fields and prepare the archive: lua_getfield(L, 1, "bytes_per_block"); if ( ! lua_isnil(L, -1) && ARCHIVE_OK != archive_write_set_bytes_per_block(*self_ref, lua_tointeger(L, -1)) ) { err("archive_write_set_bytes_per_block: %s", archive_error_string(*self_ref)); } lua_pop(L, 1); lua_getfield(L, 1, "bytes_in_last_block"); if ( ! lua_isnil(L, -1) && ARCHIVE_OK != archive_write_set_bytes_in_last_block(*self_ref, lua_tointeger(L, -1)) ) { err("archive_write_set_bytes_in_last_block: %s", archive_error_string(*self_ref)); } lua_pop(L, 1); lua_getfield(L, 1, "skip_file"); if ( ! lua_isnil(L, -1) ) { dev_t dev; ino_t ino; if ( LUA_TTABLE != lua_type(L, -1) ) { err("skip_file member must be a table object"); } lua_getfield(L, -1, "dev"); if ( ! lua_isnumber(L, -1) ) { err("skip_file.dev member must be a number"); } dev = (dev_t)lua_tonumber(L, -1); lua_pop(L, 1); lua_getfield(L, -1, "ino"); if ( ! lua_isnumber(L, -1) ) { err("skip_file.ino member must be a number"); } ino = (ino_t)lua_tonumber(L, -1); lua_pop(L, 1); if ( ARCHIVE_OK != archive_write_set_skip_file(*self_ref, dev, ino) ) { err("archive_write_set_skip_file: %s", archive_error_string(*self_ref)); } } lua_pop(L, 1); lua_getfield(L, 1, "format"); if ( lua_isnil(L, -1) ) { lua_pop(L, 1); lua_pushliteral(L, "posix"); } name = lua_tostring(L, -1); for ( ;; idx++ ) { if ( names[idx].name == NULL ) { err("archive_write_set_format_*: No such format '%s'", name); } if ( strcmp(name, names[idx].name) == 0 ) break; } if ( ARCHIVE_OK != (names[idx].setter)(*self_ref) ) { err("archive_write_set_format_%s: %s", name, archive_error_string(*self_ref)); } lua_pop(L, 1); lua_getfield(L, 1, "compression"); if ( ! lua_isnil(L, -1) ) { static struct { const char *name; int (*setter)(struct archive *); } names[] = { { "bzip2", archive_write_add_filter_bzip2 }, { "compress", archive_write_add_filter_compress }, { "gzip", archive_write_add_filter_gzip }, { "lzma", archive_write_add_filter_lzma }, { "xz", archive_write_add_filter_xz }, { NULL, NULL } }; int idx = 0; const char* name = lua_tostring(L, -1); for ( ;; idx++ ) { if ( names[idx].name == NULL ) { err("archive_write_set_compression_*: No such compression '%s'", name); } if ( strcmp(name, names[idx].name) == 0 ) break; } if ( ARCHIVE_OK != (names[idx].setter)(*self_ref) ) { err("archive_write_set_compression_%s: %s", name, archive_error_string(*self_ref)); } } lua_pop(L, 1); lua_getfield(L, 1, "options"); if ( ! lua_isnil(L, -1) && ARCHIVE_OK != archive_write_set_options(*self_ref, lua_tostring(L, -1)) ) { err("archive_write_set_options: %s", archive_error_string(*self_ref)); } lua_pop(L, 1); if ( ARCHIVE_OK != archive_write_open(*self_ref, L, NULL, &ar_write_cb, NULL) ) { err("archive_write_open: %s", archive_error_string(*self_ref)); } return 1; }