예제 #1
0
/* Extract the file */
static size_t RPMCopy(install_info *info, const char *path, const char *dest, const char *current_option_name, 
		      xmlNodePtr node,
		      int (*update)(install_info *info, const char *path, size_t progress, size_t size, const char *current))
{
    FD_t fdi;
    Header hd;
    size_t size;
    int_32 type, c;
    int rc, isSource;
    void *p;
	const char *reloc = xmlGetProp(node, "relocate");
	const char *autorm = xmlGetProp(node, "autoremove");
	const char *depsoff = xmlGetProp(node, "nodeps");
	int relocate = (reloc && !strcasecmp(reloc, "true"));
	int autoremove = (autorm && !strcasecmp(autorm, "true"));
	int nodeps = (depsoff && !strcasecmp(depsoff, "true"));

    fdi = fdOpen(path, O_RDONLY, 0644);
    rc = rpmReadPackageHeader(fdi, &hd, &isSource, NULL, NULL);
    if ( rc ) {
		log_warning(_("RPM error: %s"), rpmErrorString());
        return 0;
    }

    size = 0;
    if ( rpm_access && ! force_manual ) { /* We can call RPM directly */
        char cmd[300];
        FILE *fp;
        float percent = 0.0;
		double bytes_copied = 0.0;
		double previous_bytes = 0.0;
        char *name = "", *version = "", *release = "";
		char *options = (char *) malloc(PATH_MAX);
		options[0] = '\0';

        headerGetEntry(hd, RPMTAG_SIZE, &type, &p, &c);
        if(type==RPM_INT32_TYPE){
			size = *(int_32*) p;
        }
        headerGetEntry(hd, RPMTAG_RELEASE, &type, &p, &c);
        if(type==RPM_STRING_TYPE){
			release = (char *) p;
        }
        headerGetEntry(hd, RPMTAG_NAME, &type, &p, &c);
        if(type==RPM_STRING_TYPE){
			name = (char*)p;
        }
        headerGetEntry(hd, RPMTAG_VERSION, &type, &p, &c);
        if(type==RPM_STRING_TYPE){
			version = (char*)p;
        }
        fdClose(fdi);

		if (relocate) { /* force relocating RPM to install directory */
			sprintf(options, " --relocate /=%s --badreloc ", dest);
		}

		if (nodeps) {
			strcat(options, " --nodeps ");
		}

		snprintf(cmd,sizeof(cmd),"rpm -U --percent --root %s %s %s", rpm_root,
				 options, path);

		fp = popen(cmd, "r");
		while ( percent < 100.0 ) {
			if(!fp || feof(fp)){
				pclose(fp);
				free(options);
				log_warning(_("Unable to install RPM file: '%s'"), path);
				return 0;
			}
			fscanf(fp,"%s", cmd);
			if(strcmp(cmd,"%%")){
				pclose(fp);
				free(options);
				log_warning(_("Unable to install RPM file: '%s'"), path);
				return 0;
			}
			fscanf(fp,"%f", &percent);
			/* calculate the bytes installed in this pass of the loop */
			bytes_copied = (percent/100.0)*size - previous_bytes;
			previous_bytes += bytes_copied;
			info->installed_bytes += bytes_copied;
			if ( ! update(info, path, (percent/100.0)*size, size, current_option_name) )
				break;
		}
		pclose(fp);
		free (options);
		/* Log the RPM installation */
		add_rpm_entry(info, current_option, name, version, atoi(release), autoremove);
    } else { /* Manually install the RPM file */
        gzFile gzdi = NULL;
		BZFILE *bzdi = NULL;
		FILE *fd = NULL;
		unsigned char magic[2];
        stream *cpio;
    
        if(headerIsEntry(hd, RPMTAG_PREIN)){      
			headerGetEntry(hd, RPMTAG_PREIN, &type, &p, &c);
			if(type==RPM_STRING_TYPE)
				run_script(info, (char*)p, 1, 1);
        }

		/* Identify the type of compression for the archive */
		if ( fdRead(fdi, magic, 2) < 2 ) {
			return 0;
		}
		lseek(fdFileno(fdi), -2L, SEEK_CUR); 
		if ( magic[0]==037 && magic[1]==0213 ) {
			gzdi = gzdopen(fdFileno(fdi), "r");    /* XXX gzdi == fdi */
		} else if ( magic[0]=='B' && magic[1]=='Z' ) {
			bzdi = BZDOPEN(fdFileno(fdi), "r");
		} else { /* Assume not compressed */
			fd = fdopen(fdFileno(fdi), "r");
		}
		
        cpio = file_fdopen(info, path, fd, gzdi, bzdi, "r");

        /* if relocate="true", copy the files into dest instead of rpm_root */
		if (relocate) {
			size = copy_cpio_stream(info, cpio, dest, current_option_name, node, update);
		} else {
			size = copy_cpio_stream(info, cpio, rpm_root, current_option_name, node, update);
		}

        if(headerIsEntry(hd, RPMTAG_POSTIN)){      
			headerGetEntry(hd, RPMTAG_POSTIN, &type, &p, &c);
			if(type==RPM_STRING_TYPE)
				run_script(info, (char*)p, 1, 1);
        }

        /* Append the uninstall scripts to the uninstall */
        if(headerIsEntry(hd, RPMTAG_PREUN)){      
			headerGetEntry(hd, RPMTAG_PREUN, &type, &p, &c);
			if(type==RPM_STRING_TYPE)
				add_script_entry(info, current_option, (char*)p, 0);
        }
        if(headerIsEntry(hd, RPMTAG_POSTUN)){      
			headerGetEntry(hd, RPMTAG_POSTUN, &type, &p, &c);
			if(type==RPM_STRING_TYPE)
				add_script_entry(info, current_option, (char*)p, 1);
        }
        fdClose(fdi);
    }
    return size;
}
예제 #2
0
/* Opens a file and prepares a ftap struct.
   If "do_random" is TRUE, it opens the file twice; the second open
   allows the application to do random-access I/O without moving
   the seek offset for sequential I/O, which is used by Wireshark
   so that it can do sequential I/O to a capture file that's being
   written to as new packets arrive independently of random I/O done
   to display protocol trees for packets when they're selected. */
ftap* ftap_open_offline(const char *filename, int *err, char **err_info,
			gboolean do_random)
{
	int	fd;
	ws_statb64 statb;
	ftap	*fth;
	unsigned int	i;
	gboolean use_stdin = FALSE;
	gchar *extension;

	/* open standard input if filename is '-' */
	if (strcmp(filename, "-") == 0)
		use_stdin = TRUE;

	/* First, make sure the file is valid */
	if (use_stdin) {
		if (ws_fstat64(0, &statb) < 0) {
			*err = errno;
			return NULL;
		}
	} else {
		if (ws_stat64(filename, &statb) < 0) {
			*err = errno;
			return NULL;
		}
	}
	if (S_ISFIFO(statb.st_mode)) {
		/*
		 * Opens of FIFOs are allowed only when not opening
		 * for random access.
		 *
		 * XXX - currently, we do seeking when trying to find
		 * out the file type, so we don't actually support
		 * opening FIFOs.  However, we may eventually
		 * do buffering that allows us to do at least some
		 * file type determination even on pipes, so we
		 * allow FIFO opens and let things fail later when
		 * we try to seek.
		 */
		if (do_random) {
			*err = FTAP_ERR_RANDOM_OPEN_PIPE;
			return NULL;
		}
	} else if (S_ISDIR(statb.st_mode)) {
		/*
		 * Return different errors for "this is a directory"
		 * and "this is some random special file type", so
		 * the user can get a potentially more helpful error.
		 */
		*err = EISDIR;
		return NULL;
	} else if (! S_ISREG(statb.st_mode)) {
		*err = FTAP_ERR_NOT_REGULAR_FILE;
		return NULL;
	}

	/*
	 * We need two independent descriptors for random access, so
	 * they have different file positions.  If we're opening the
	 * standard input, we can only dup it to get additional
	 * descriptors, so we can't have two independent descriptors,
	 * and thus can't do random access.
	 */
	if (use_stdin && do_random) {
		*err = FTAP_ERR_RANDOM_OPEN_STDIN;
		return NULL;
	}

	errno = ENOMEM;
	fth = (ftap *)g_malloc0(sizeof(ftap));

	/* Open the file */
	errno = FTAP_ERR_CANT_OPEN;
	if (use_stdin) {
		/*
		 * We dup FD 0, so that we don't have to worry about
		 * a file_close of wth->fh closing the standard
		 * input of the process.
		 */
		fd = ws_dup(0);
		if (fd < 0) {
			*err = errno;
			g_free(fth);
			return NULL;
		}
#ifdef _WIN32
		if (_setmode(fd, O_BINARY) == -1) {
			/* "Shouldn't happen" */
			*err = errno;
			g_free(fth);
			return NULL;
		}
#endif
		if (!(fth->fh = file_fdopen(fd))) {
			*err = errno;
			ws_close(fd);
			g_free(fth);
			return NULL;
		}
	} else {
		if (!(fth->fh = file_open(filename))) {
			*err = errno;
			g_free(fth);
			return NULL;
		}
	}

	if (do_random) {
		if (!(fth->random_fh = file_open(filename))) {
			*err = errno;
			file_close(fth->fh);
			g_free(fth);
			return NULL;
		}
	} else
		fth->random_fh = NULL;

	/* initialization */
	fth->file_encap = FTAP_ENCAP_UNKNOWN;
	fth->subtype_sequential_close = NULL;
	fth->subtype_close = NULL;
    fth->priv = NULL;

    init_magic_number_open_routines();
	init_heuristic_open_info();
	if (fth->random_fh) {
		fth->fast_seek = g_ptr_array_new();

		file_set_random_access(fth->fh, FALSE, fth->fast_seek);
		file_set_random_access(fth->random_fh, TRUE, fth->fast_seek);
	}

	/* Try all file types that support magic numbers */
	for (i = 0; i < magic_number_open_routines_arr->len; i++) {
		/* Seek back to the beginning of the file; the open routine
		   for the previous file type may have left the file
		   position somewhere other than the beginning, and the
		   open routine for this file type will probably want
		   to start reading at the beginning.

		   Initialize the data offset while we're at it. */
		if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
			/* I/O error - give up */
			ftap_close(fth);
			return NULL;
		}

		switch ((*magic_number_open_routines[i])(fth, err, err_info)) {

		case -1:
			/* I/O error - give up */
			ftap_close(fth);
			return NULL;

		case 0:
			/* No I/O error, but not that type of file */
			break;

		case 1:
			/* We found the file type */
			goto success;
		}
	}

	/* Does this file's name have an extension? */
	extension = get_file_extension(filename);
	if (extension != NULL) {
		/* Yes - try the heuristic types that use that extension first. */
		for (i = 0; i < heuristic_open_info_arr->len; i++) {
			/* Does this type use that extension? */
			if (heuristic_uses_extension(i, extension)) {
				/* Yes. */
				if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;
				}

				switch ((*heuristic_open_info[i].open_routine)(fth,
				    err, err_info)) {

				case -1:
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;

				case 0:
					/* No I/O error, but not that type of file */
					break;

				case 1:
					/* We found the file type */
					g_free(extension);
					goto success;
				}
			}
		}

		/* Now try the ones that don't use it. */
		for (i = 0; i < heuristic_open_info_arr->len; i++) {
			/* Does this type use that extension? */
			if (!heuristic_uses_extension(i, extension)) {
				/* No. */
				if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;
				}

				switch ((*heuristic_open_info[i].open_routine)(fth,
				    err, err_info)) {

				case -1:
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;

				case 0:
					/* No I/O error, but not that type of file */
					break;

				case 1:
					/* We found the file type */
					g_free(extension);
					goto success;
				}
			}
		}
		g_free(extension);
	} else {
		/* No - try all the heuristics types in order. */
		for (i = 0; i < heuristic_open_info_arr->len; i++) {
			if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
				/* I/O error - give up */
				ftap_close(fth);
				return NULL;
			}

			switch ((*heuristic_open_info[i].open_routine)(fth,
			    err, err_info)) {

			case -1:
				/* I/O error - give up */
				ftap_close(fth);
				return NULL;

			case 0:
				/* No I/O error, but not that type of file */
				break;

			case 1:
				/* We found the file type */
				goto success;
			}
		}
	}

    /* Well, it's not one of the types of file we know about. */
	ftap_close(fth);
	*err = FTAP_ERR_FILE_UNKNOWN_FORMAT;
	return NULL;

success:
	fth->frame_buffer = (struct Buffer *)g_malloc(sizeof(struct Buffer));
	buffer_init(fth->frame_buffer, 1500);

	return fth;
}