/**
 * 任务命令解析
 * 实现父类的纯虚函数
 */
bool LzBackupQueue::parseMsg(QString msg)
{
    QStringList strList = msg.split(",", QString::SkipEmptyParts);
 
    int msgid = strList.value(0).toInt();
    qDebug() << "msgid= " << msgid;

    if (msgid > 3000)
    {
        switch(msgid)
        {
            case 3001: // 3001,filename=xxx,tunnelid=xx,isinterrupted=false(true),interruptedfilepos=xx,beginfc=xx,endfc=xx
            {
                if (strList.length() < 7 || !strList.at(1).startsWith("filename") || !strList.at(2).startsWith("tunnelid") || !strList.at(3).startsWith("isinterrupted")
                                         || !strList.at(4).startsWith("interruptedfilepos") || !strList.at(5).startsWith("beginfc") || !strList.at(6).startsWith("endfc"))
                {
                    qDebug() << tr("解析字符出错") << msg;
                    return false;
                }
                QString filename = strList.at(1).mid(9);
                int tunnelid = strList.at(2).mid(9).toInt();
                current_tunnelid = tunnelid;
                QString isinterruptstr = strList.at(3).mid(14);
                bool isinterrupt = false;
                if (isinterruptstr.compare("true") == 0)
                    isinterrupt = true;
                qint64 interruptfilepos = strList.at(4).mid(19).toLongLong();
                qint64 beginfc = strList.at(5).mid(8).toLongLong();
                qint64 endfc = strList.at(6).mid(6).toLongLong();
                if (isinterrupt)
                    emit signalParsedMsgToSlave(tr("[主控] 命令:开始备份文件为%1,文件起始帧号%2,终止帧号%3,从中断位置%4帧开始").arg(filename).arg(beginfc).arg(endfc).arg(interruptfilepos));
                else
                    emit signalParsedMsgToSlave(tr("[主控] 命令:开始备份文件为%1,文件起始帧号%2,终止帧号%3,从头开始").arg(filename).arg(beginfc).arg(endfc));

                // 确认开始备份
                backup_feedbackStartToMaster(filename, tunnelid, beginfc, endfc);
                // 备份该隧道
                int ret = backup_start(filename, tunnelid, isinterrupt, interruptfilepos);
                if (ret != 0)
                    endMsg();

                break;
            }
            case 3401://暂停备份
            {
                qDebug() << QObject::tr("[主控] 命令:暂停备份");
                /************TODO*************/
                backup_suspend();

                emit signalParsedMsgToSlave(tr("[主控] 命令:暂停备份"));
                break;
            }
            default: return false;
        }
    }

    return true;
}
Пример #2
0
// main
int main(int argc, char *argv[])
{
    // arg analysis
    invocation_name = argv[0];
    bool bdeamon = false;
	const Arg_parser::Option options[] =
	{
		{ 'v', "version",    Arg_parser::no    },
		{ 'h', "help",       Arg_parser::no    },
		{ 256, "deamon",     Arg_parser::no    }};

	const Arg_parser parser( argc, argv, options );
	if( parser.error().size() )
	{ show_error( parser.error().c_str(), 0, true ); return BK_ERROR; }

	for( int argind = 0; argind < parser.arguments(); ++argind )
	{
		const int code = parser.code( argind );
		switch( code )
		{
		case 'v': print_version(); return 0;
		case 'h': show_help(); return 0;
		case 256: bdeamon = true; break;
		default : internal_error( "uncaught option" );
		}
	}
    if(bdeamon)
    {
        setdeamon();
        string db, ip, user, psw;
        get_dbconfig(db, ip, user, psw);
        if (backup_init(db.c_str(), ip.c_str(), user.c_str(), psw.c_str()) != 0)
        //if (backup_init("backup", "127.0.0.1", "root", "root") != 0)
        {
            cout << "init backupd error "<< endl;
            return 0;
        }
        backup_start();
        while(_running) sleep(10);
    }
    else
    {
        Gtk::Main kit(argc, argv);
        BackupMain window;
    //Shows the window and returns when it is closed.
        Gtk::Main::run(window);
        backup_stop();
    }

    return 0;
}
Пример #3
0
int
copy_main (int argc, char **argv)
{
	char source_a[PATH_MAX], source_b[PATH_MAX];
	char dest_a[PATH_MAX], dest_b[PATH_MAX];
	char *tmp;
	struct backup_info *info_b, *info_r;
	int opt, loop, thresh = 0, threshopt = 0;
	unsigned int varsize = 0, swapsize = 0;
	unsigned int bflags = BF_BACKUPVAR, rflags = 0;
	int quiet = 0;
	int bswap = 0;
	int expand = 0;
	int expandscale = 2;

	tivo_partition_direct ();

	while ((opt = getopt (argc, argv, "hqf:L:tTaspxr:v:S:lbBzE")) > 0)
	{
		switch (opt)
		{
		case 'q':
			quiet++;
			break;
		case 's':
			bflags |= BF_SHRINK;
			break;
		case 'E':
			bflags |= BF_TRUNCATED;
			break;
		case 'f':
			if (threshopt)
			{
				fprintf (stderr, "%s: -f and -%c cannot be used together\n", argv[0], threshopt);
				return 1;
			}
			threshopt = loop;
			thresh = strtoul (optarg, &tmp, 10);
			if (*tmp)
			{
				fprintf (stderr, "%s: Non integer argument to -f\n", argv[0]);
				return 1;
			}
			break;
		case 'L':
			if (threshopt)
			{
				fprintf (stderr, "%s: -l and -%c cannot be used together\n", argv[0], threshopt);
				return 1;
			}
			threshopt = loop;
			thresh = strtoul (optarg, &tmp, 10);
			thresh *= 1024 * 2;
			bflags |= BF_THRESHSIZE;
			if (*tmp)
			{
				fprintf (stderr, "%s: Non integer argument to -L\n", argv[0]);
				return 1;
			}
			break;
		case 't':
			bflags |= BF_THRESHTOT;
			break;
		case 'T':
			bflags |= BF_STREAMTOT;
			break;
		case 'a':
			if (threshopt)
			{
				fprintf (stderr, "%s: -a and -%c cannot be used together\n", argv[0], threshopt);
				return 1;
			}
			threshopt = loop;
			thresh = ~0;
			break;

		case 'v':
			bflags &= ~BF_BACKUPVAR;
			varsize = strtoul (optarg, &tmp, 10);
			varsize *= 1024 * 2;
			if (tmp && *tmp)
			{
				fprintf (stderr, "%s: Integer argument expected for -v.\n", argv[0]);
				return 1;
			}
			break;
		case 'S':
			swapsize = strtoul (optarg, &tmp, 10);
			swapsize *= 1024 * 2;
			if (tmp && *tmp)
			{
				fprintf (stderr, "%s: Integer argument expected for -s.\n", argv[0]);
				return 1;
			}
			break;
		case 'z':
			rflags |= RF_ZEROPART;
			break;
		case 'b':
			if (bswap != 0)
			{
				fprintf (stderr, "%s: Only one byte swapping option (-b/-B) allowed.\n", argv[0]);
				return 1;
			}
			bswap = -1;
			break;
		case 'B':
			if (bswap != 0)
			{
				fprintf (stderr, "%s: Only one byte swapping option (-b/-B) allowed.\n", argv[0]);
				return 1;
			}
			bswap = 1;
			break;
		case 'p':
			rflags |= RF_BALANCE;
			break;
		case 'l':
			rflags |= RF_NOFILL;
			break;
		case 'x':
			expand = 1;
			break;
		case 'r':
			expandscale = strtoul (optarg, &tmp, 10);
			if (tmp && *tmp)
			{
				fprintf (stderr, "%s: Integer argument expected for -r.\n", argv[0]);
				return 1;
			}
			if (expandscale < 0 || expandscale > 4)
			{
				fprintf (stderr, "%s: Scale value for -r must be in the range 0 to 4.\n", argv[0]);
				return 1;
			}
			break;
		default:
			copy_usage (argv[0]);
			return 1;
		}
	}

	// Split out the drive names
	source_a[0] = 0;
	source_b[0] = 0;
	dest_a[0] = 0;
	dest_b[0] = 0;

	if (argc - optind < 4)
	{
		if (optind < argc)
		{
			get_drives (argv[optind++], source_a, source_b);
		}
		if (optind < argc)
		{
			get_drives (argv[optind++], dest_a, dest_b);
		}
	}
	else
	{
// Special case for convenience - 2 source and 2 target named
		strcpy (source_a, argv[optind++]);
		strcpy (source_b, argv[optind++]);
		strcpy (dest_a, argv[optind++]);
		strcpy (dest_b, argv[optind++]);
	}

	if (optind < argc || !*source_a || !*dest_a)
	{
		copy_usage (argv[0]);
		return 1;
	}

	if (expand > 0)
		rflags |= RF_NOFILL;

	info_b = init_backup (source_a, source_b, bflags);

	// Try to continue anyway despite error.
	if (bflags & BF_TRUNCATED && backup_has_error (info_b))
	{
		backup_perror (info_b, "WARNING");
		fprintf (stderr, "Attempting copy anyway\n");
		backup_check_truncated_volume (info_b);
		if (info_b && backup_has_error (info_b))
		{
			backup_perror (info_b, "Copy source");
			return 1;
		}
	}

	if (info_b && backup_has_error (info_b))
	{
		backup_perror (info_b, "Copy source");

		fprintf (stderr, "To attempt copy anyway, try again with -E.  -s is implied by -E.\n");
		return 1;
	}

	info_r = init_restore (rflags);
	if (info_r && restore_has_error (info_r))
	{
		restore_perror (info_r, "Copy target");
		return 1;
	}

	if (!info_b || !info_r)
	{
		fprintf (stderr, "%s: Copy failed to start.  Make sure you specified the right\ndevices, and that the drives are not locked.\n", argv[0]);
		return 1;
	}
	else
	{
		unsigned starttime;
		char buf[BUFSIZE];
		unsigned int curcount = 0;
		int nread, nwrit;

		if (threshopt)
			backup_set_thresh (info_b, thresh);

		if (varsize)
			restore_set_varsize (info_r, varsize);
		if (swapsize)
			restore_set_swapsize (info_r, swapsize);
		if (bswap)
			restore_set_bswap (info_r, bswap);

		if (quiet < 2)
			fprintf (stderr, "Scanning source drive.  Please wait a moment.\n");

		if (backup_start (info_b) < 0)
		{
			if (backup_has_error (info_b))
				backup_perror (info_b, "Copy source");
			else
				fprintf (stderr, "Copy source failed.\n");
			return 1;
		}

// Fill the buffer up to start.  Restore needs some information to bootstrap
// the process.
		while (curcount < BUFSIZE && (nread = backup_read (info_b, buf, BUFSIZE - curcount)) > 0)
		{
			curcount += nread;
		}

		if (curcount < BUFSIZE)
		{
			if (backup_has_error (info_b))
				backup_perror (info_b, "Copy source");
			else
				fprintf (stderr, "Copy source failed.\n");
			return 1;
		}

		nread = curcount;

		nwrit = restore_write (info_r, buf, nread);
		if (nwrit < 0)
		{
			if (restore_has_error (info_r))
				restore_perror (info_r, "Copy target");
			else
				fprintf (stderr, "Copy target failed.\n");
			return 1;
		}

		if (restore_trydev (info_r, dest_a, dest_b) < 0)
		{
			if (restore_has_error (info_r))
				restore_perror (info_r, "Copy target");
			else
				fprintf (stderr, "Copy target failed.\n");
			return 1;
		}

		if (restore_start (info_r) < 0)
		{
			if (restore_has_error (info_r))
				restore_perror (info_r, "Copy target");
			else
				fprintf (stderr, "Copy target failed.\n");
			return 1;
		}

		if (restore_write (info_r, buf + nwrit, nread - nwrit) != nread - nwrit)
		{
			if (restore_has_error (info_r))
				restore_perror (info_r, "Copy target");
			else
				fprintf (stderr, "Copy target failed.\n");
			return 1;
		}

		starttime = time (NULL);

		fprintf (stderr, "Starting copy\nSize: %d megabytes\n", info_r->nsectors / 2048);
		while ((curcount = backup_read (info_b, buf, BUFSIZE)) > 0)
		{
			unsigned int prcnt, compr;
			if (restore_write (info_r, buf, curcount) != curcount)
			{
				if (quiet < 1)
					fprintf (stderr, "\n");
				if (restore_has_error (info_r))
					restore_perror (info_r, "Copy source");
				else
					fprintf (stderr, "Copy source failed.\n");
				return 1;
			}
			prcnt = get_percent (info_r->cursector, info_r->nsectors);
			if (quiet < 1)
			{
				unsigned timedelta = time(NULL) - starttime;

				fprintf (stderr, "\rCopying %d of %d mb (%d.%02d%%)", info_r->cursector / 2048, info_r->nsectors / 2048, prcnt / 100, prcnt % 100);

				if (prcnt > 100 && timedelta > 15)
				{
					unsigned ETA = timedelta * (10000 - prcnt) / prcnt;
					fprintf (stderr, " %d mb/sec (ETA %d:%02d:%02d)", info_r->cursector / timedelta / 2048, ETA / 3600, ETA / 60 % 60, ETA % 60);
				}
			}
		}

		if (quiet < 1)
			fprintf (stderr, "\n");

		if (backup_has_error (info_b))
		{
			backup_perror (info_b, "Copy source");
			return 1;
		}

		if (restore_has_error (info_r))
		{
			restore_perror (info_r, "Copy target");
			return 1;
		}
	}

	if (backup_finish (info_b) < 0)
	{
		if (backup_has_error (info_b))
			backup_perror (info_b, "Copy source");
		else
			fprintf (stderr, "Copy source failed.\n");
		return 1;
	}

	if (info_b->back_flags & BF_TRUNCATED)
	{
		fprintf (stderr, "***WARNING***\nCopy was made of an incomplete volume.  While the copy succeeded,\nit is possible there was some required data missing.  Verify your copy.\n");
	}

	if (quiet < 2)
		fprintf (stderr, "Cleaning up target.  Please wait a moment.\n");

	if (restore_finish (info_r) < 0)
	{
		if (restore_has_error (info_r))
			restore_perror (info_r, "Copy target");
		else
			fprintf (stderr, "Copy target failed.\n");
		return 1;
	}

	if (quiet < 2)
		fprintf (stderr, "Copy done!\n");

	if (expand > 0)
	{
		int blocksize = 0x800;
		struct mfs_handle *mfshnd;

		expand = 0;

		mfshnd = mfs_init (dest_a, dest_b, O_RDWR);
		if (!mfshnd)
		{
			fprintf (stderr, "Drive expansion failed.\n");
			return 1;
		}

		if (mfs_has_error (mfshnd))
		{
			mfs_perror (mfshnd, "Target expand");
			return 1;
		}

		while (expandscale-- > 0)
			blocksize *= 2;

		if (tivo_partition_largest_free (dest_a) > 1024 * 1024 * 2)
		{
			if (expand_drive (mfshnd, "/dev/hda", dest_a, blocksize) < 0)
			{
				if (mfs_has_error (mfshnd))
					mfs_perror (mfshnd, "Expand drive A");
				else
					fprintf (stderr, "Drive A expansion failed.\n");
				return 1;
			}
			expand++;
		}

		if (dest_b[0] && tivo_partition_largest_free (dest_b) > 1024 * 1024 * 2)
		{
			if (expand_drive (mfshnd, "/dev/hdb", dest_b, blocksize) < 0)
			{
				if (mfs_has_error (mfshnd))
					mfs_perror (mfshnd, "Expand drive B");
				else
					fprintf (stderr, "Drive B expansion failed.\n");
				return 1;
			}
			expand++;
		}

		if (!expand)
		{
			fprintf (stderr, "Not enough extra space to expand on A drive%s.\n", dest_b[0]? " or B drive": "");
		}
	}

	return 0;
}