2011-03-11 12:44:19 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ioall.h>
|
|
|
|
#include <unistd.h>
|
2011-03-15 13:12:21 +01:00
|
|
|
#include <gui-fatal.h>
|
2011-03-11 12:44:19 +01:00
|
|
|
#include "dvm2.h"
|
|
|
|
|
|
|
|
void send_file(char *fname)
|
|
|
|
{
|
|
|
|
char *base;
|
2013-12-29 13:02:09 +01:00
|
|
|
char sendbuf[DVM_FILENAME_SIZE];
|
2011-03-11 12:44:19 +01:00
|
|
|
int fd = open(fname, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_fatal("open %s", fname);
|
2011-03-11 12:44:19 +01:00
|
|
|
base = rindex(fname, '/');
|
|
|
|
if (!base)
|
|
|
|
base = fname;
|
|
|
|
else
|
|
|
|
base++;
|
|
|
|
if (strlen(base) >= DVM_FILENAME_SIZE)
|
|
|
|
base += strlen(base) - DVM_FILENAME_SIZE + 1;
|
2013-12-29 13:02:09 +01:00
|
|
|
strncpy(sendbuf,base,DVM_FILENAME_SIZE); /* fills out with NULs */
|
|
|
|
if (!write_all(1, sendbuf, DVM_FILENAME_SIZE))
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_fatal("send filename to dispVM");
|
2011-03-11 12:44:19 +01:00
|
|
|
if (!copy_fd_all(1, fd))
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_fatal("send file to dispVM");
|
2011-03-11 12:44:19 +01:00
|
|
|
close(1);
|
2013-12-29 13:01:45 +01:00
|
|
|
close(fd);
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int copy_and_return_nonemptiness(int tmpfd)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (!copy_fd_all(tmpfd, 0))
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_fatal("receiving file from dispVM");
|
2011-03-11 12:44:19 +01:00
|
|
|
if (fstat(tmpfd, &st))
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_fatal("fstat");
|
2011-03-11 12:44:19 +01:00
|
|
|
close(tmpfd);
|
|
|
|
|
2013-12-29 13:04:11 +01:00
|
|
|
return st.st_size > 0;
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void recv_file_nowrite(char *fname)
|
|
|
|
{
|
|
|
|
char *tempfile;
|
|
|
|
char *errmsg;
|
|
|
|
int tmpfd;
|
|
|
|
|
|
|
|
asprintf(&tempfile, "/tmp/file_edited_in_dvm.XXXXXX");
|
|
|
|
tmpfd = mkstemp(tempfile);
|
|
|
|
if (tmpfd < 0)
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_fatal("unable to create any temporary file, aborting");
|
2011-03-11 12:44:19 +01:00
|
|
|
if (!copy_and_return_nonemptiness(tmpfd)) {
|
|
|
|
unlink(tempfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
asprintf(&errmsg,
|
|
|
|
"The file %s has been edited in Disposable VM and the modified content has been received, "
|
|
|
|
"but this file is in nonwritable directory and thus cannot be modified safely. The edited file has been "
|
|
|
|
"saved to %s", fname, tempfile);
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_nonfatal(errmsg);
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void actually_recv_file(char *fname, char *tempfile, int tmpfd)
|
|
|
|
{
|
|
|
|
if (!copy_and_return_nonemptiness(tmpfd)) {
|
|
|
|
unlink(tempfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rename(tempfile, fname))
|
2011-03-15 13:12:21 +01:00
|
|
|
gui_fatal("rename");
|
2011-03-11 12:44:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void recv_file(char *fname)
|
|
|
|
{
|
|
|
|
int tmpfd;
|
|
|
|
char *tempfile;
|
|
|
|
asprintf(&tempfile, "%s.XXXXXX", fname);
|
|
|
|
tmpfd = mkstemp(tempfile);
|
|
|
|
if (tmpfd < 0)
|
|
|
|
recv_file_nowrite(fname);
|
|
|
|
else
|
|
|
|
actually_recv_file(fname, tempfile, tmpfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void talk_to_daemon(char *fname)
|
|
|
|
{
|
|
|
|
send_file(fname);
|
|
|
|
recv_file(fname);
|
|
|
|
}
|
|
|
|
|
2011-07-06 12:32:20 +02:00
|
|
|
int main(int argc, char ** argv)
|
2011-03-11 12:44:19 +01:00
|
|
|
{
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2011-07-06 12:32:20 +02:00
|
|
|
if (argc!=2)
|
|
|
|
gui_fatal("OpenInVM - no file given?");
|
|
|
|
talk_to_daemon(argv[1]);
|
2011-03-11 12:44:19 +01:00
|
|
|
return 0;
|
2011-07-06 12:32:20 +02:00
|
|
|
}
|