コード例 #1
1
ファイル: disk_ripper.cpp プロジェクト: WMTH/rivendell
DiskRipper::DiskRipper(QString *filter,QString *group,QString *schedcode,
		       bool profile_rip,QWidget *parent,const char *name) 
  : QDialog(parent,name)
{
  rip_isrc_read=false;
  rip_filter_text=filter;
  rip_group_text=group;
  rip_schedcode_text=schedcode;
  rip_profile_rip=profile_rip;
  rip_aborting=false;

  //
  // Fix the Window Size
  //
  setMinimumWidth(sizeHint().width());
  setMinimumHeight(sizeHint().height());

  //
  // Generate Fonts
  //
  QFont button_font=QFont("Helvetica",12,QFont::Bold);
  button_font.setPixelSize(12);
  QFont label_font=QFont("Helvetica",12,QFont::Bold);
  label_font.setPixelSize(12);

  setCaption(tr("Rip Disk"));

  //
  // Create Dialogs
  //
  rip_wavedata_dialog=new RDWaveDataDialog("RDLibrary",this);

  //
  // Create Temporary Directory
  //
  char path[PATH_MAX];
  strncpy(path,RDTempDir(),PATH_MAX);
  strcat(path,"/XXXXXX");
  if(mkdtemp(path)==NULL) {
    QMessageBox::warning(this,"RDLibrary - "+tr("Ripper Error"),
			 tr("Unable to create temporary directory!"));
  }
  else {
    rip_cdda_dir.setPath(path);
  }

  //
  // The CDROM Drive
  //
  if(rip_profile_rip) {
    rip_cdrom=new RDCdPlayer(stdout,this);
  }
  else {
    rip_cdrom=new RDCdPlayer(NULL,this);
  }
  connect(rip_cdrom,SIGNAL(ejected()),this,SLOT(ejectedData()));
  connect(rip_cdrom,SIGNAL(mediaChanged()),this,SLOT(mediaChangedData()));
  connect(rip_cdrom,SIGNAL(played(int)),this,SLOT(playedData(int)));
  connect(rip_cdrom,SIGNAL(stopped()),this,SLOT(stoppedData()));
  rip_cdrom->setDevice(rdlibrary_conf->ripperDevice());
  rip_cdrom->open();

  //
  // CDDB Stuff
  //
  if(rip_profile_rip) {
    rip_cddb_lookup=new RDCddbLookup(stdout,this);
  }
  else {
    rip_cddb_lookup=new RDCddbLookup(NULL,this);
  }
  connect(rip_cddb_lookup,SIGNAL(done(RDCddbLookup::Result)),
	  this,SLOT(cddbDoneData(RDCddbLookup::Result)));

  //
  // Artist Label
  //
  QLabel *label=new QLabel(tr("Artist:"),this);
  label->setGeometry(10,10,50,18);
  label->setFont(label_font);
  label->setAlignment(AlignRight|AlignVCenter);
  rip_artist_edit=new QLineEdit(this);

  //
  // Album Edit
  //
  label=new QLabel(tr("Album:"),this);
  label->setGeometry(10,32,50,18);
  label->setFont(label_font);
  label->setAlignment(AlignRight|AlignVCenter);
  rip_album_edit=new QLineEdit(this);

  //
  // Other Edit
  //
  label=new QLabel(tr("Other:"),this);
  label->setGeometry(10,54,50,16);
  label->setFont(label_font);
  label->setAlignment(AlignRight);
  rip_other_edit=new QTextEdit(this);
  rip_other_edit->setReadOnly(true);

  //
  // Apply FreeDB Check Box
  //
  rip_apply_box=new QCheckBox(this,"rip_apply_box");
  rip_apply_box->setChecked(true);
  rip_apply_box->setDisabled(true);
  rip_apply_label=
    new QLabel(rip_apply_box,tr("Apply FreeDB Values to Carts"),this);
  rip_apply_label->setFont(label_font);
  rip_apply_label->setAlignment(AlignLeft);
  rip_apply_box->setChecked(false);
  rip_apply_label->setDisabled(true);

  //
  // Track List
  //
  rip_track_list=new QListView(this);
  rip_track_list->setAllColumnsShowFocus(true);
  rip_track_list->setItemMargin(5);
  rip_track_list->setSorting(-1);
  rip_track_list->setSelectionMode(QListView::Extended);
  connect(rip_track_list,SIGNAL(selectionChanged()),
	  this,SLOT(selectionChangedData()));
  connect(rip_track_list,
	  SIGNAL(doubleClicked(QListViewItem *,const QPoint &,int)),
	  this,
	  SLOT(doubleClickedData(QListViewItem *,const QPoint &,int)));
  rip_track_label=new QLabel(rip_track_list,tr("Tracks"),this);
  rip_track_label->setFont(label_font);
  rip_track_list->addColumn(tr("TRACK"));
  rip_track_list->setColumnAlignment(0,Qt::AlignHCenter);
  rip_track_list->addColumn(tr("LENGTH"));
  rip_track_list->setColumnAlignment(1,Qt::AlignRight);
  rip_track_list->addColumn(tr("TITLE"));
  rip_track_list->setColumnAlignment(2,Qt::AlignLeft);
  rip_track_list->addColumn(tr("OTHER"));
  rip_track_list->setColumnAlignment(3,Qt::AlignLeft);
  rip_track_list->addColumn(tr("TYPE"));
  rip_track_list->setColumnAlignment(4,Qt::AlignLeft);
  rip_track_list->addColumn(tr("CUT"));
  rip_track_list->setColumnAlignment(5,Qt::AlignLeft);

  //
  // Progress Bars
  //
  rip_disk_bar=new QProgressBar(this);
  rip_diskbar_label=new QLabel(tr("Disk Progress"),this);
  rip_diskbar_label->setFont(label_font);
  rip_diskbar_label->setAlignment(AlignLeft|AlignVCenter);
  rip_diskbar_label->setDisabled(true);
  rip_track_bar=new QProgressBar(this);
  rip_trackbar_label=new QLabel(tr("Track Progress"),this);
  rip_trackbar_label->setFont(label_font);
  rip_trackbar_label->setAlignment(AlignLeft|AlignVCenter);
  rip_trackbar_label->setDisabled(true);

  //
  // Eject Button
  //
  rip_eject_button=new RDTransportButton(RDTransportButton::Eject,this);
  connect(rip_eject_button,SIGNAL(clicked()),this,SLOT(ejectButtonData()));
  
  //
  // Play Button
  //
  rip_play_button=new RDTransportButton(RDTransportButton::Play,this);
  connect(rip_play_button,SIGNAL(clicked()),this,SLOT(playButtonData()));
  
  //
  // Stop Button
  //
  rip_stop_button=new RDTransportButton(RDTransportButton::Stop,this);
  rip_stop_button->setOnColor(red);
  rip_stop_button->on();
  connect(rip_stop_button,SIGNAL(clicked()),this,SLOT(stopButtonData()));
  
  //
  // Set Cut Button
  //
  rip_setcut_button=new QPushButton(tr("Set\n&Cart/Cut"),this);
  rip_setcut_button->setFont(button_font);
  rip_setcut_button->setDisabled(true);
  connect(rip_setcut_button,SIGNAL(clicked()),this,SLOT(setCutButtonData()));

  //
  // Set Multi Tracks Button
  //
  rip_setall_button=new QPushButton(tr("Add Cart\nPer Track"),this);
  rip_setall_button->setFont(button_font);
  rip_setall_button->setDisabled(true);
  connect(rip_setall_button,SIGNAL(clicked()),this,SLOT(setMultiButtonData()));

  //
  // Set Single Button
  //
  rip_setsingle_button=new QPushButton(tr("Add Single\nCart"),this);
  rip_setsingle_button->setFont(button_font);
  rip_setsingle_button->setDisabled(true);
  connect(rip_setsingle_button,SIGNAL(clicked()),
	  this,SLOT(setSingleButtonData()));

  //
  // Set Cart Label Button
  //
  rip_cartlabel_button=new QPushButton(tr("Modify\nCart Label"),this);
  rip_cartlabel_button->setFont(button_font);
  rip_cartlabel_button->setDisabled(true);
  connect(rip_cartlabel_button,SIGNAL(clicked()),
	  this,SLOT(modifyCartLabelData()));

  //
  // Clear Selection Button
  //
  rip_clear_button=new QPushButton(tr("Clear\nSelection"),this);
  rip_clear_button->setFont(button_font);
  rip_clear_button->setDisabled(true);
  connect(rip_clear_button,SIGNAL(clicked()),this,SLOT(clearSelectionData()));

  //
  // Normalize Check Box
  //
  rip_normalize_box=new QCheckBox(this);
  rip_normalize_box->setChecked(true);
  rip_normalizebox_label=new QLabel(rip_normalize_box,tr("Normalize"),this);
  rip_normalizebox_label->setFont(label_font);
  rip_normalizebox_label->setAlignment(AlignLeft|AlignVCenter);
  connect(rip_normalize_box,SIGNAL(toggled(bool)),
	  this,SLOT(normalizeCheckData(bool)));

  //
  // Normalize Level
  //
  rip_normalize_spin=new QSpinBox(this);
  rip_normalize_spin->setRange(-30,0);
  rip_normalize_label=new QLabel(rip_normalize_spin,tr("Level:"),this);
  rip_normalize_label->setFont(label_font);
  rip_normalize_label->setAlignment(AlignRight|AlignVCenter);
  rip_normalize_unit=new QLabel(tr("dBFS"),this);
  rip_normalize_unit->setFont(label_font);
  rip_normalize_unit->setAlignment(AlignLeft|AlignVCenter);

  //
  // Autotrim Check Box
  //
  rip_autotrim_box=new QCheckBox(this);
  rip_autotrim_box->setChecked(true);
  rip_autotrimbox_label=new QLabel(rip_autotrim_box,tr("Autotrim"),this);
  rip_autotrimbox_label->setFont(label_font);
  rip_autotrimbox_label->setAlignment(AlignLeft|AlignVCenter);
  connect(rip_autotrim_box,SIGNAL(toggled(bool)),
	  this,SLOT(autotrimCheckData(bool)));

  //
  // Autotrim Level
  //
  rip_autotrim_spin=new QSpinBox(this);
  rip_autotrim_spin->setRange(-99,0);
  rip_autotrim_label=new QLabel(rip_autotrim_spin,tr("Level:"),this);
  rip_autotrim_label->setFont(label_font);
  rip_autotrim_label->setAlignment(AlignRight|AlignVCenter);
  rip_autotrim_unit=new QLabel(tr("dBFS"),this);
  rip_autotrim_unit->setFont(label_font);
  rip_autotrim_unit->setAlignment(AlignLeft|AlignVCenter);

  //
  // Channels
  //
  rip_channels_box=new QComboBox(this);
  rip_channels_label=new QLabel(rip_channels_box,tr("Channels:"),this);
  rip_channels_label->setFont(label_font);
  rip_channels_label->setAlignment(AlignRight|AlignVCenter);

  //
  // Rip Disc Button
  //
  rip_rip_button=new QPushButton(tr("&Rip\nDisc"),this);
  rip_rip_button->setFont(button_font);
  connect(rip_rip_button,SIGNAL(clicked()),this,SLOT(ripDiskButtonData()));
  rip_rip_button->setDisabled(true);

  //
  // Close Button
  //
  rip_close_button=new QPushButton(tr("&Close"),this);
  rip_close_button->setFont(button_font);
  connect(rip_close_button,SIGNAL(clicked()),this,SLOT(closeData()));

  //
  // Populate Data
  //
  rip_normalize_spin->setValue(rdlibrary_conf->ripperLevel()/100);
  rip_autotrim_spin->setValue(rdlibrary_conf->trimThreshold()/100);
  rip_channels_box->insertItem("1");
  rip_channels_box->insertItem("2");
  rip_channels_box->setCurrentItem(rdlibrary_conf->defaultChannels()-1);
  rip_done=false;
}
コード例 #2
0
ファイル: df.c プロジェクト: Der-Jan/freebsd-crypto
int
main(int argc, char *argv[])
{
	struct stat stbuf;
	struct statfs statfsbuf, totalbuf;
	struct maxwidths maxwidths;
	struct statfs *mntbuf;
	const char *fstype;
	char *mntpath, *mntpt;
	const char **vfslist;
	int i, mntsize;
	int ch, rv;

	fstype = "ufs";
	(void)setlocale(LC_ALL, "");
	memset(&totalbuf, 0, sizeof(totalbuf));
	totalbuf.f_bsize = DEV_BSIZE;
	strlcpy(totalbuf.f_mntfromname, "total", MNAMELEN);
	vfslist = NULL;
	while ((ch = getopt(argc, argv, "abcgHhiklmnPt:T,")) != -1)
		switch (ch) {
		case 'a':
			aflag = 1;
			break;
		case 'b':
				/* FALLTHROUGH */
		case 'P':
			/*
			 * POSIX specifically discusses the behavior of
			 * both -k and -P. It states that the blocksize should
			 * be set to 1024. Thus, if this occurs, simply break
			 * rather than clobbering the old blocksize.
			 */
			if (kflag)
				break;
			setenv("BLOCKSIZE", "512", 1);
			hflag = 0;
			break;
		case 'c':
			cflag = 1;
			break;
		case 'g':
			setenv("BLOCKSIZE", "1g", 1);
			hflag = 0;
			break;
		case 'H':
			hflag = UNITS_SI;
			break;
		case 'h':
			hflag = UNITS_2;
			break;
		case 'i':
			iflag = 1;
			break;
		case 'k':
			kflag++;
			setenv("BLOCKSIZE", "1024", 1);
			hflag = 0;
			break;
		case 'l':
			if (vfslist != NULL)
				errx(1, "-l and -t are mutually exclusive.");
			vfslist = makevfslist(makenetvfslist());
			lflag = 1;
			break;
		case 'm':
			setenv("BLOCKSIZE", "1m", 1);
			hflag = 0;
			break;
		case 'n':
			nflag = 1;
			break;
		case 't':
			if (lflag)
				errx(1, "-l and -t are mutually exclusive.");
			if (vfslist != NULL)
				errx(1, "only one -t option may be specified");
			fstype = optarg;
			vfslist = makevfslist(optarg);
			break;
		case 'T':
			Tflag = 1;
			break;
		case ',':
			thousands = 1;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	rv = 0;
	if (!*argv) {
		/* everything (modulo -t) */
		mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
	} else {
		/* just the filesystems specified on the command line */
		mntbuf = malloc(argc * sizeof(*mntbuf));
		if (mntbuf == 0)
			err(1, "malloc()");
		mntsize = 0;
		/* continued in for loop below */
	}

	/* iterate through specified filesystems */
	for (; *argv; argv++) {
		if (stat(*argv, &stbuf) < 0) {
			if ((mntpt = getmntpt(*argv)) == 0) {
				warn("%s", *argv);
				rv = 1;
				continue;
			}
		} else if (S_ISCHR(stbuf.st_mode)) {
			if ((mntpt = getmntpt(*argv)) == 0) {
				mdev.fspec = *argv;
				mntpath = strdup("/tmp/df.XXXXXX");
				if (mntpath == NULL) {
					warn("strdup failed");
					rv = 1;
					continue;
				}
				mntpt = mkdtemp(mntpath);
				if (mntpt == NULL) {
					warn("mkdtemp(\"%s\") failed", mntpath);
					rv = 1;
					free(mntpath);
					continue;
				}
				if (mount(fstype, mntpt, MNT_RDONLY,
				    &mdev) != 0) {
					warn("%s", *argv);
					rv = 1;
					(void)rmdir(mntpt);
					free(mntpath);
					continue;
				} else if (statfs(mntpt, &statfsbuf) == 0) {
					statfsbuf.f_mntonname[0] = '\0';
					prtstat(&statfsbuf, &maxwidths);
					if (cflag)
						addstat(&totalbuf, &statfsbuf);
				} else {
					warn("%s", *argv);
					rv = 1;
				}
				(void)unmount(mntpt, 0);
				(void)rmdir(mntpt);
				free(mntpath);
				continue;
			}
		} else
			mntpt = *argv;

		/*
		 * Statfs does not take a `wait' flag, so we cannot
		 * implement nflag here.
		 */
		if (statfs(mntpt, &statfsbuf) < 0) {
			warn("%s", mntpt);
			rv = 1;
			continue;
		}

		/*
		 * Check to make sure the arguments we've been given are
		 * satisfied.  Return an error if we have been asked to
		 * list a mount point that does not match the other args
		 * we've been given (-l, -t, etc.).
		 */
		if (checkvfsname(statfsbuf.f_fstypename, vfslist)) {
			rv = 1;
			continue;
		}

		/* the user asked for it, so ignore the ignore flag */
		statfsbuf.f_flags &= ~MNT_IGNORE;

		/* add to list */
		mntbuf[mntsize++] = statfsbuf;
	}

	bzero(&maxwidths, sizeof(maxwidths));
	for (i = 0; i < mntsize; i++) {
		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
			update_maxwidths(&maxwidths, &mntbuf[i]);
			if (cflag)
				addstat(&totalbuf, &mntbuf[i]);
		}
	}
	for (i = 0; i < mntsize; i++)
		if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
			prtstat(&mntbuf[i], &maxwidths);
	if (cflag)
		prtstat(&totalbuf, &maxwidths);
	return (rv);
}
コード例 #3
0
ファイル: stapsh.c プロジェクト: rth7680/systemtap
int
main(int argc, char* const argv[])
{
  // set those right away so that *print* statements don't fail
  stapsh_in  = stdin;
  stapsh_out = stdout;
  stapsh_err = stderr;

  parse_args(argc, argv);

  // we're gonna need this for setup_signals so let's do it now
  if (uname(&uts))
    die("Error calling uname");

  setup_signals();

  if (listening_port != NULL)
    {
      listening_port_fd = open(listening_port, O_RDWR);
      if (listening_port_fd == -1)
        // no one might be watching but might as well print
        die("Error calling open()");

      if (can_sigio())
        {
          // make ourselves the recipient of SIGIO signals for this port
          if (fcntl(listening_port_fd, F_SETOWN, getpid()) < 0)
            die("Error calling fcntl F_SETOWN");

          // add O_ASYNC flag to enable SIGIO notifications on this port
          int flags = fcntl(listening_port_fd, F_GETFL);
          if (flags == -1)
            die("Error calling fcntl F_GETFL");
          if (fcntl(listening_port_fd, F_SETFL, flags | O_ASYNC) == -1)
            die("Error calling fcntl F_SETFL");
        }

      // We need to have different FILE* for each direction, otherwise
      // we'll have trouble down the line due to stdio's buffering
      // (doing "r+" on sockets is very wonky)
      stapsh_in  = fdopen(listening_port_fd, "r");
      stapsh_out = fdopen(listening_port_fd, "w");
      stapsh_err = stapsh_out;
      if (!stapsh_in || !stapsh_out)
        die("Could not open serial port");
    }

  umask(0077);
  snprintf(tmpdir, sizeof(tmpdir), "%s/stapsh.XXXXXX",
           getenv("TMPDIR") ?: "/tmp");
  if (!mkdtemp(tmpdir))
    die ("Can't make a temporary working directory");
  if (chdir(tmpdir))
    die ("Can't change to temporary working directory \"%s\"", tmpdir);

  // Prep pfds. For now we're only interested in commands from stap, and we
  // don't poll staprun until it is started.
  pfds[PFD_STAP_OUT].fd = fileno(stapsh_in);
  pfds[PFD_STAP_OUT].events = POLLIN;
  pfds[PFD_STAPRUN_OUT].events = 0;
  pfds[PFD_STAPRUN_ERR].events = 0;

  // Wait until a host connects before entering the command loop or poll() will
  // return with POLLHUP right away. Note that sleep() will be interrupted upon
  // SIGIO.
  if (listening_port != NULL)
    while (!host_connected())
      sleep(2); // Once we support only platforms with guaranteed SIGIO support,
                // we could replace this with a pause().

  // keep polling as long as we're listening for stap commands
  while (pfds[PFD_STAP_OUT].events)
    {
      if (poll(pfds, 3, -1) < 0)
        {
          if (errno == EINTR)
            continue; // go back to poll()
          else
            die ("poll() failed with critical error");
        }
      if (pfds[PFD_STAP_OUT].revents & POLLHUP)
        break;
      if (pfds[PFD_STAP_OUT].revents & POLLIN)
        process_command();
      if (pfds[PFD_STAPRUN_OUT].revents & POLLIN)
        prefix_staprun(PFD_STAPRUN_OUT, stapsh_out, "stdout");
      if (pfds[PFD_STAPRUN_ERR].revents & POLLIN)
        prefix_staprun(PFD_STAPRUN_ERR, stapsh_err, "stderr");
    }

  cleanup(0);
}
コード例 #4
0
ファイル: test_dir.hpp プロジェクト: atoulme/rubinius
 char* make_directory() {
   char* templ = strdup("/tmp/rubinius_TestDir.XXXXXX");
   return mkdtemp(templ);
 }
コード例 #5
0
ファイル: lr_reader.c プロジェクト: Xyratex/lustre-stable
int main(int argc, char *const argv[])
{
	char tmpdir[] = "/tmp/dirXXXXXX";
	char cmd[128];
	char filepnm[128] = "";
	char *dev;
	struct lr_server_data lsd;
	FILE *filep = NULL;
	int ret;
	int c;
	int opt_client = 0;
	int opt_reply = 0;

	progname = argv[0];
	while ((c = getopt_long(argc, argv, "chr", long_opts, NULL)) != -1) {
		switch (c) {
		case 'c':
			opt_client = 1;
			break;
		case 'r':
			opt_reply = 1;
			break;
		case 'h':
		default:
			display_usage();
			return -1;
		}
	}
	dev = argv[optind];
	if (!dev) {
		display_usage();
		return -1;
	}

	/* Make a temporary directory to hold Lustre data files. */
	if (!mkdtemp(tmpdir)) {
		fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
			progname, tmpdir, strerror(errno));
		return errno;
	}

	memset(cmd, 0, sizeof(cmd));
	snprintf(cmd, sizeof(cmd),
		"%s -c -R 'dump /%s %s/%s' %s",
		DEBUGFS, LAST_RCVD, tmpdir, LAST_RCVD, dev);

	ret = run_command(cmd, sizeof(cmd));
	if (ret) {
		fprintf(stderr, "%s: Unable to dump %s file\n",
			progname, LAST_RCVD);
		goto out_rmdir;
	}

	snprintf(filepnm, 128, "%s/%s", tmpdir, LAST_RCVD);
	filep = fopen(filepnm, "r");
	if (!filep) {
		fprintf(stderr, "%s: Unable to read old data\n",
			progname);
		ret = -errno;
		goto out_rmdir;
	}
	unlink(filepnm);

	/* read lr_server_data structure */
	printf("%s:\n", LAST_RCVD);
	ret = fread(&lsd, 1, sizeof(lsd), filep);
	if (ret < sizeof(lsd)) {
		fprintf(stderr, "%s: Short read (%d of %d)\n",
			progname, ret, (int)sizeof(lsd));
		ret = -ferror(filep);
		if (ret)
			goto out_close;
	}

	/* swab structure fields of interest */
	lsd.lsd_feature_compat = __le32_to_cpu(lsd.lsd_feature_compat);
	lsd.lsd_feature_incompat = __le32_to_cpu(lsd.lsd_feature_incompat);
	lsd.lsd_feature_rocompat = __le32_to_cpu(lsd.lsd_feature_rocompat);
	lsd.lsd_last_transno = __le64_to_cpu(lsd.lsd_last_transno);
	lsd.lsd_osd_index = __le32_to_cpu(lsd.lsd_osd_index);
	lsd.lsd_mount_count = __le64_to_cpu(lsd.lsd_mount_count);

	/* display */
	printf("  uuid: %.40s\n", lsd.lsd_uuid);
	printf("  feature_compat: %#x\n", lsd.lsd_feature_compat);
	printf("  feature_incompat: %#x\n", lsd.lsd_feature_incompat);
	printf("  feature_rocompat: %#x\n", lsd.lsd_feature_rocompat);
	printf("  last_transaction: %llu\n", lsd.lsd_last_transno);
	printf("  target_index: %u\n", lsd.lsd_osd_index);
	printf("  mount_count: %llu\n", lsd.lsd_mount_count);

	/* read client information */
	if (opt_client) {
		lsd.lsd_client_start = __le32_to_cpu(lsd.lsd_client_start);
		lsd.lsd_client_size = __le16_to_cpu(lsd.lsd_client_size);
		printf("  client_area_start: %u\n", lsd.lsd_client_start);
		printf("  client_area_size: %hu\n", lsd.lsd_client_size);

		/* seek to per-client data area */
		ret = fseek(filep, lsd.lsd_client_start, SEEK_SET);
		if (ret) {
			fprintf(stderr, "%s: seek failed. %s\n",
				progname, strerror(errno));
			ret = errno;
			goto out_close;
		}

		/* walk throuh the per-client data area */
		while (true) {
			struct lsd_client_data lcd;

			/* read a per-client data area */
			ret = fread(&lcd, 1, sizeof(lcd), filep);
			if (ret < sizeof(lcd)) {
				if (feof(filep))
					break;
				fprintf(stderr, "%s: Short read (%d of %d)\n",
					progname, ret, (int)sizeof(lcd));
				ret = -ferror(filep);
				goto out_close;
			}

			if (lcd.lcd_uuid[0] == '\0')
				continue;

			/* swab structure fields */
			lcd.lcd_last_transno =
					__le64_to_cpu(lcd.lcd_last_transno);
			lcd.lcd_last_xid = __le64_to_cpu(lcd.lcd_last_xid);
			lcd.lcd_last_result = __le32_to_cpu(lcd.lcd_last_result);
			lcd.lcd_last_data = __le32_to_cpu(lcd.lcd_last_data);
			lcd.lcd_generation = __le32_to_cpu(lcd.lcd_generation);

			/* display per-client data area */
			printf("\n  %.40s:\n", lcd.lcd_uuid);
			printf("    generation: %u\n", lcd.lcd_generation);
			printf("    last_transaction: %llu\n",
			       lcd.lcd_last_transno);
			printf("    last_xid: %llu\n", lcd.lcd_last_xid);
			printf("    last_result: %u\n", lcd.lcd_last_result);
			printf("    last_data: %u\n", lcd.lcd_last_data);

			if (lcd.lcd_last_close_transno != 0 &&
			    lcd.lcd_last_close_xid != 0) {
				lcd.lcd_last_close_transno =
					__le64_to_cpu(lcd.lcd_last_close_transno);
				lcd.lcd_last_close_xid =
					__le64_to_cpu(lcd.lcd_last_close_xid);
				lcd.lcd_last_close_result =
					__le32_to_cpu(lcd.lcd_last_close_result);
				lcd.lcd_last_close_data =
					__le32_to_cpu(lcd.lcd_last_close_data);
				printf("    last_close_transation: %llu\n",
				       lcd.lcd_last_close_transno);
				printf("    last_close_xid: %llu\n",
				       lcd.lcd_last_close_xid);
				printf("    last_close_result: %u\n",
				       lcd.lcd_last_close_result);
				printf("    last_close_data: %u\n",
				       lcd.lcd_last_close_data);
			}
		}
	}
	fclose(filep);
	filep = NULL;

	/* read reply data information */
	if (opt_reply) {
		struct lsd_reply_header lrh;
		struct lsd_reply_data lrd;
		unsigned long long slot;

		snprintf(cmd, sizeof(cmd),
			 "%s -c -R 'dump /%s %s/%s' %s",
			 DEBUGFS, REPLY_DATA, tmpdir, REPLY_DATA, dev);

		ret = run_command(cmd, sizeof(cmd));
		if (ret) {
			fprintf(stderr, "%s: Unable to dump %s file\n",
				progname, REPLY_DATA);
			goto out_rmdir;
		}

		snprintf(filepnm, sizeof(filepnm),
			 "%s/%s", tmpdir, REPLY_DATA);
		filep = fopen(filepnm, "r");
		if (!filep) {
			fprintf(stderr, "%s: Unable to read reply data\n",
				progname);
			ret = -errno;
			goto out_rmdir;
		}
		unlink(filepnm);

		/* read reply_data header */
		printf("\n%s:\n", REPLY_DATA);
		ret = fread(&lrh, 1, sizeof(lrh), filep);
		if (ret < sizeof(lrh)) {
			fprintf(stderr, "%s: Short read (%d of %d)\n",
				progname, ret, (int)sizeof(lrh));
			ret = -ferror(filep);
			if (ret)
				goto out_close;
		}

		/* check header */
		lrh.lrh_magic = __le32_to_cpu(lrh.lrh_magic);
		lrh.lrh_header_size = __le32_to_cpu(lrh.lrh_header_size);
		lrh.lrh_reply_size = __le32_to_cpu(lrh.lrh_reply_size);
		if (lrh.lrh_magic != LRH_MAGIC) {
			fprintf(stderr, "%s: invalid %s header: "
				"lrh_magic=%08x expected %08x\n",
				progname, REPLY_DATA, lrh.lrh_magic, LRH_MAGIC);
			goto out_close;
		}
		if (lrh.lrh_header_size != sizeof(struct lsd_reply_header)) {
			fprintf(stderr, "%s: invalid %s header: "
				"lrh_header_size=%08x expected %08x\n",
				progname, REPLY_DATA, lrh.lrh_header_size,
				(unsigned int)sizeof(struct lsd_reply_header));
			goto out_close;
		}
		if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data)) {
			fprintf(stderr, "%s: invalid %s header: "
				"lrh_reply_size=%08x expected %08x\n",
				progname, REPLY_DATA, lrh.lrh_reply_size,
				(unsigned int)sizeof(struct lsd_reply_data));
			goto out_close;
		}

		/* walk throuh the reply data */
		for (slot = 0; ; slot++) {
			/* read a reply data */
			ret = fread(&lrd, 1, sizeof(lrd), filep);
			if (ret < sizeof(lrd)) {
				if (feof(filep))
					break;
				fprintf(stderr, "%s: Short read (%d of %d)\n",
					progname, ret, (int)sizeof(lrd));
				ret = -ferror(filep);
				goto out_close;
			}

			/* display reply data */
			lrd.lrd_transno = __le64_to_cpu(lrd.lrd_transno);
			lrd.lrd_xid = __le64_to_cpu(lrd.lrd_xid);
			lrd.lrd_data = __le64_to_cpu(lrd.lrd_data);
			lrd.lrd_result = __le32_to_cpu(lrd.lrd_result);
			lrd.lrd_client_gen = __le32_to_cpu(lrd.lrd_client_gen);

			printf("  %lld:\n", slot);
			printf("    client_generation: %u\n",
			       lrd.lrd_client_gen);
			printf("    last_transaction: %llu\n", lrd.lrd_transno);
			printf("    last_xid: %llu\n", lrd.lrd_xid);
			printf("    last_result: %u\n", lrd.lrd_result);
			printf("    last_data: %llu\n\n", lrd.lrd_data);
		}
	}

out_close:
	if (filep != NULL)
		fclose(filep);

out_rmdir:
	rmdir(tmpdir);
	return ret;
}
コード例 #6
0
int main (int argc, char *argv[])
{
	int i;
	char *pluginname = NULL;
	char *plugindir = NULL;
	char *configfile = NULL;
	char *objtype = NULL;
	OSyncError *error = NULL;

	if (argc < 2)
		usage (argv[0], 1);

	pluginname = argv[1];
	for (i = 2; i < argc; i++) {
		char *arg = argv[i];
		if (!strcmp (arg, "--config")) {
			configfile = argv[i + 1];
			i++;
			if (!configfile)
				usage (argv[0], 1);
		} else if (!strcmp (arg, "--type")) {
			objtype = argv[i + 1];
			i++;
			if (!objtype)
				usage (argv[0], 1);
		} else if (!strcmp (arg, "--plugindir")) {
			printf("plugindir %s\n", argv[i + 1]);
			plugindir = argv[i + 1];
			i++;
			if (!plugindir)
				usage (argv[0], 1);
		} else if (!strcmp (arg, "--random")) {
			only_random = TRUE;
		} else if (!strcmp (arg, "--help")) {
			usage (argv[0], 0);
		} else {
			usage (argv[0], 1);
		}
	}

	OSyncEnv *env = osync_env_new(NULL);
	osync_env_set_option(env, "LOAD_GROUPS", "FALSE");

	if (plugindir)
		osync_env_set_option(env, "PLUGINS_DIRECTORY", plugindir);

	if (!osync_env_initialize(env, &error)) {
		printf("Unable to initialize environment: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		return 1;
	}

	char *testdir = g_strdup_printf("%s/plgtest.XXXXXX", g_get_tmp_dir());
	char *result = mkdtemp(testdir);

	if (result == NULL)
	{
		osync_trace(TRACE_EXIT_ERROR, "unable to create temporary dir: %s", g_strerror(errno));
		return 1;
	}

	OSyncGroup *group = osync_group_new(env);
	osync_group_set_name(group, osync_rand_str(8));
	osync_group_set_configdir(group, testdir);
	OSyncMember *member = osync_member_new(group);

	char *config = NULL;
	int size = 0;
	if (configfile) {
		if (!osync_file_read(configfile, &config, &size, &error)) {
			fprintf(stderr, "Unable to read config: %s\n", osync_error_print(&error));
			osync_error_unref(&error);
			return 1;
		}
		osync_member_set_config(member, config, size);
	}

	osync_member_set_pluginname(member, pluginname);

	OSyncMember *file = osync_member_new(group);

	localdir = g_strdup_printf("%s/plgtest.XXXXXX", g_get_tmp_dir());
	result = mkdtemp(localdir);

	if (result == NULL)
	{
		osync_trace(TRACE_EXIT_ERROR, "unable to create temporary dir: %s", g_strerror(errno));
		return 1;
	}

	config = g_strdup_printf("<config><path>%s</path><recursive>0</recursive></config>", localdir);
	osync_member_set_config(file, config, strlen(config) + 1);
	osync_member_set_pluginname(file, "file-sync");

	if (!osync_group_save(group, &error)) {
		printf("Error while creating syncengine: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		goto error_free_env;
	}

	if (!g_thread_supported ()) g_thread_init (NULL);

	OSyncEngine *engine = osengine_new(group, &error);
	if (!engine) {
		printf("Error while creating syncengine: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		goto error_free_env;
	}

	if (!osengine_init(engine, &error)) {
		printf("Error while initializing syncengine: %s\n", osync_error_print(&error));
		osync_error_unref(&error);
		goto error_free_engine;
	}

	int count = 0;
	if (only_random) {
		do {
			count++;
			printf("++++++++++++++++++++++++++++++\n");
			printf("Initializing new round #%i!\n", count);

			if (g_random_int_range(0, 5) == 0) {
				int i;
				OSyncFormatEnv *env = osync_group_get_format_env(group);
				for (i = 0; i < osync_conv_num_objtypes(env); i++) {
					if (g_random_int_range(0, 5) == 0) {
						OSyncObjType *type = osync_conv_nth_objtype(env, i);
						osync_group_set_slow_sync(group, osync_objtype_get_name(type), TRUE);
						printf("Requesting slow-sync for: %s\n", osync_objtype_get_name(type));
					}
				}
				osync_conv_env_free(env);
			}

			change_content();

			check_sync(engine, "Random", 1);
		} while (g_random_int_range(0, 3) != 0);

		printf("Finalizing engine\n");
		osengine_finalize(engine);
		osengine_free(engine);

		engine = osengine_new(group, &error);
		if (!engine) {
			printf("Error while creating syncengine: %s\n", osync_error_print(&error));
			osync_error_unref(&error);
			goto error_free_env;
		}

		if (!osengine_init(engine, &error)) {
			printf("Error while initializing syncengine: %s\n", osync_error_print(&error));
			osync_error_unref(&error);
			goto error_free_engine;
		}
	} else {
		register_tests();
		run_all_tests(engine, file, member, objtype);
	}

	printf("\nCompleted successfully\n");
	return 0;

error_free_engine:
	osengine_free(engine);
error_free_env:
	osync_group_free(group);
	osync_env_free(env);
	return 1;
}
コード例 #7
0
ファイル: fileutils.cpp プロジェクト: Nonta72/DualBootPatcher
std::string FileUtils::create_temporary_dir(const std::string &directory)
{
#ifdef _WIN32
    // This Win32 code is adapted from the awesome libuv library (MIT-licensed)
    // https://github.com/libuv/libuv/blob/v1.x/src/win/fs.c

    constexpr char possible[] =
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    constexpr size_t num_chars = 62;
    constexpr size_t num_x = 6;
    constexpr char suffix[] = "\\mbpatcher-";

    HCRYPTPROV h_prov;

    bool ret = CryptAcquireContext(
        &h_prov,                // phProv
        nullptr,                // pszContainer
        nullptr,                // pszProvider
        PROV_RSA_FULL,          // dwProvType
        CRYPT_VERIFYCONTEXT     // dwFlags
    );
    if (!ret) {
        LOGE("CryptAcquireContext() failed: %s",
             ec_from_win32().message().c_str());
        return std::string();
    }

    std::string new_path = directory;           // Path
    new_path += suffix;                         // "\mbpatcher-"
    new_path.resize(new_path.size() + num_x);   // Unique part

    char *unique = &new_path[new_path.size() - num_x];

    unsigned int tries = TMP_MAX;
    do {
        uint64_t v;

        ret = CryptGenRandom(
            h_prov,                      // hProv
            sizeof(v),                   // dwLen
            reinterpret_cast<BYTE *>(&v) // pbBuffer
        );
        if (!ret) {
            LOGE("CryptGenRandom() failed: %s",
                 ec_from_win32().message().c_str());
            break;
        }

        for (size_t i = 0; i < num_x; ++i) {
            unique[i] = possible[v % num_chars];
            v /= num_chars;
        }

        // This is not particularly fast, but it'll do for now
        auto w_new_path = utf8_to_wcs(new_path);
        if (!w_new_path) {
            LOGE("Failed to convert UTF-8 to WCS: %s",
                 w_new_path.error().message().c_str());
            break;
        }

        ret = CreateDirectoryW(
            w_new_path.value().c_str(), // lpPathName
            nullptr                     // lpSecurityAttributes
        );

        if (ret) {
            break;
        } else if (GetLastError() != ERROR_ALREADY_EXISTS) {
            LOGE("CreateDirectoryW() failed: %s",
                 ec_from_win32().message().c_str());
            new_path.clear();
            break;
        }
    } while (--tries);

    bool released = CryptReleaseContext(
        h_prov, // hProv
        0       // dwFlags
    );
    assert(released);

    return new_path;
#else
    std::string dir_template(directory);
    dir_template += "/mbpatcher-XXXXXX";

    if (mkdtemp(&dir_template[0])) {
        return dir_template;
    }

    return {};
#endif
}
コード例 #8
0
ファイル: trans.c プロジェクト: DavidEGrayson/MSYS2-pacman
int _alpm_runscriptlet(alpm_handle_t *handle, const char *filepath,
		const char *script, const char *ver, const char *oldver, int is_archive)
{
	char arg0[64], arg1[3], cmdline[PATH_MAX];
	char *argv[] = { arg0, arg1, cmdline, NULL };
	char *tmpdir, *scriptfn = NULL, *scriptpath;
	int retval = 0;
	size_t len;

	if(_alpm_access(handle, NULL, filepath, R_OK) != 0) {
		_alpm_log(handle, ALPM_LOG_DEBUG, "scriptlet '%s' not found\n", filepath);
		return 0;
	}

	if(!is_archive && !grep(filepath, script)) {
		/* script not found in scriptlet file; we can only short-circuit this early
		 * if it is an actual scriptlet file and not an archive. */
		return 0;
	}

	strcpy(arg0, SCRIPTLET_SHELL);
	strcpy(arg1, "-c");

	/* create a directory in $root/tmp/ for copying/extracting the scriptlet */
	len = strlen(handle->root) + strlen("tmp/alpm_XXXXXX") + 1;
	MALLOC(tmpdir, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
	snprintf(tmpdir, len, "%stmp/", handle->root);
	if(access(tmpdir, F_OK) != 0) {
		_alpm_makepath_mode(tmpdir, 01777);
	}
	snprintf(tmpdir, len, "%stmp/alpm_XXXXXX", handle->root);
	if(mkdtemp(tmpdir) == NULL) {
		_alpm_log(handle, ALPM_LOG_ERROR, _("could not create temp directory\n"));
		free(tmpdir);
		return 1;
	}

	/* either extract or copy the scriptlet */
	len += strlen("/.INSTALL");
	MALLOC(scriptfn, len, free(tmpdir); RET_ERR(handle, ALPM_ERR_MEMORY, -1));
	snprintf(scriptfn, len, "%s/.INSTALL", tmpdir);
	if(is_archive) {
		if(_alpm_unpack_single(handle, filepath, tmpdir, ".INSTALL")) {
			retval = 1;
		}
	} else {
		if(_alpm_copyfile(filepath, scriptfn)) {
			_alpm_log(handle, ALPM_LOG_ERROR, _("could not copy tempfile to %s (%s)\n"), scriptfn, strerror(errno));
			retval = 1;
		}
	}
	if(retval == 1) {
		goto cleanup;
	}

	if(is_archive && !grep(scriptfn, script)) {
		/* script not found in extracted scriptlet file */
		goto cleanup;
	}

	/* chop off the root so we can find the tmpdir in the chroot */
	scriptpath = scriptfn + strlen(handle->root) - 1;

	if(oldver) {
		snprintf(cmdline, PATH_MAX, ". %s; %s %s %s",
				scriptpath, script, ver, oldver);
	} else {
		snprintf(cmdline, PATH_MAX, ". %s; %s %s",
				scriptpath, script, ver);
	}

	_alpm_log(handle, ALPM_LOG_DEBUG, "executing \"%s\"\n", cmdline);

	retval = _alpm_run_chroot(handle, SCRIPTLET_SHELL, argv);

cleanup:
	if(scriptfn && unlink(scriptfn)) {
		_alpm_log(handle, ALPM_LOG_WARNING,
				_("could not remove %s\n"), scriptfn);
	}
	if(rmdir(tmpdir)) {
		_alpm_log(handle, ALPM_LOG_WARNING,
				_("could not remove tmpdir %s\n"), tmpdir);
	}

	free(scriptfn);
	free(tmpdir);
	return retval;
}
コード例 #9
0
ファイル: import.cpp プロジェクト: sisu/cses-green
void Import::process(std::istream &zipData) {
	string fileName = getFileStoragePath(saveStreamToFile(zipData));
	
	char tempName[] = "zipXXXXXX";
	mkdtemp(tempName);
	string dirName = tempName;				
	
	string command = "unzip " + fileName + " -d " + dirName;
	system(command.c_str());
	DIR *dir = opendir((dirName + "/.").c_str());
	struct dirent *d;
	map<string, pair<vector<string>, vector<string>>> data;
	while ((d = readdir(dir)) != NULL) {
		string taskName = d->d_name;
		if (taskName == ".") continue;
		if (taskName == "..") continue;
		DIR *dir2 = opendir((dirName + "/" + taskName + "/.").c_str());
		struct dirent *d2;
		vector<string> inputData, outputData;
		while ((d2 = readdir(dir2)) != NULL) {
			string fileName = d2->d_name;
			if (fileName == ".") continue;
			if (fileName == "..") continue;
			if (fileName == "task.nfo") continue;
			if (fileName.find(".in") != string::npos) inputData.push_back(fileName);
			if (fileName.find(".IN") != string::npos) inputData.push_back(fileName);
			if (fileName.find(".out") != string::npos) outputData.push_back(fileName);
			if (fileName.find(".OUT") != string::npos) outputData.push_back(fileName);
		}
		sort(inputData.begin(), inputData.end());
		sort(outputData.begin(), outputData.end());
		while (outputData.size() < inputData.size()) outputData.push_back("");
		data[taskName] = make_pair(inputData, outputData);
	}	
	
	for (auto x : data) {
		string taskName = x.first;
		tasks.push_back(taskName);
		for (size_t i = 0; i < data[x.first].first.size(); i++) {
			string newInputName = data[x.first].first[i];
			string newOutputName = data[x.first].second[i];		
			FileSave inputSaver, outputSaver;
			inputSaver.writeFileContents(dirName + "/" + x.first + "/" + newInputName);
			if (newOutputName != "") {
				outputSaver.writeFileContents(dirName + "/" + x.first + "/" + newOutputName);
			}
			string inputHash = inputSaver.save();
			string outputHash = outputSaver.save();
			inputs[taskName].push_back(make_pair(inputHash, newInputName));
			outputs[taskName].push_back(make_pair(outputHash, newOutputName));
		}
		std::ifstream info;
		info.open(dirName + "/" + x.first + "/task.nfo");
		if (info.is_open()) {
			while (true) {
				string header;
				info >> header;
				if (!info.good() || header != "group") break;
				int points;
				info >> points;
				vector<string> list;
				while (true) {
					string prefix;
					info >> prefix;
					if (prefix == "end") break;
					list.push_back(prefix);
				}
				groups[taskName].push_back(make_pair(points, list));
			}
		}
	}
	
	
	command = "rm -rf " + dirName;
	system(command.c_str());	
}
コード例 #10
0
  void PhraseDictionaryFuzzyMatch::InitializeForInput(InputType const& inputSentence)
  {
#ifdef WIN32
	boost::filesystem::path _mypath = boost::filesystem::unique_path();
	boost::filesystem::create_directories(_mypath);
	string dirNameStr = boost::filesystem::canonical(_mypath).string();
#else
    char dirName[] = "/tmp/moses.XXXXXX";
    char *temp = mkdtemp(dirName);
	CHECK(temp);
    string dirNameStr(dirName);
#endif
    string inFileName(dirNameStr + "/in");
    
    ofstream inFile(inFileName.c_str());
    
    for (size_t i = 1; i < inputSentence.GetSize() - 1; ++i)
    {
      inFile << inputSentence.GetWord(i);
    }
    inFile << endl;
    inFile.close();
        
    long translationId = inputSentence.GetTranslationId();
    string ptFileName = m_FuzzyMatchWrapper->Extract(translationId, dirNameStr);

    // populate with rules for this sentence
    PhraseDictionaryNodeSCFG &rootNode = m_collection[translationId];
    FormatType format = MosesFormat;
        
    // data from file
    InputFileStream inStream(ptFileName);
    
    // copied from class LoaderStandard
    PrintUserTime("Start loading fuzzy-match phrase model");
    
    const StaticData &staticData = StaticData::Instance();
    const std::string& factorDelimiter = staticData.GetFactorDelimiter();
    
    
    string lineOrig;
    size_t count = 0;
    
    while(getline(inStream, lineOrig)) {
      const string *line;
      if (format == HieroFormat) { // reformat line
        assert(false);
        //line = ReformatHieroRule(lineOrig);
      }
      else
      { // do nothing to format of line
        line = &lineOrig;
      }
      
      vector<string> tokens;
      vector<float> scoreVector;
      
      TokenizeMultiCharSeparator(tokens, *line , "|||" );
      
      if (tokens.size() != 4 && tokens.size() != 5) {
        stringstream strme;
        strme << "Syntax error at " << ptFileName << ":" << count;
        UserMessage::Add(strme.str());
        abort();
      }
      
      const string &sourcePhraseString = tokens[0]
      , &targetPhraseString = tokens[1]
      , &scoreString        = tokens[2]
      , &alignString        = tokens[3];
      
      bool isLHSEmpty = (sourcePhraseString.find_first_not_of(" \t", 0) == string::npos);
      if (isLHSEmpty && !staticData.IsWordDeletionEnabled()) {
        TRACE_ERR( ptFileName << ":" << count << ": pt entry contains empty target, skipping\n");
        continue;
      }
      
      Tokenize<float>(scoreVector, scoreString);
      const size_t numScoreComponents = GetFeature()->GetNumScoreComponents();
      if (scoreVector.size() != numScoreComponents) {
        stringstream strme;
        strme << "Size of scoreVector != number (" << scoreVector.size() << "!="
        << numScoreComponents << ") of score components on line " << count;
        UserMessage::Add(strme.str());
        abort();
      }
      CHECK(scoreVector.size() == numScoreComponents);
      
      // parse source & find pt node
      
      // constituent labels
      Word sourceLHS, targetLHS;
      
      // source
      Phrase sourcePhrase( 0);
      sourcePhrase.CreateFromStringNewFormat(Input, *m_input, sourcePhraseString, factorDelimiter, sourceLHS);
      
      // create target phrase obj
      TargetPhrase *targetPhrase = new TargetPhrase();
      targetPhrase->CreateFromStringNewFormat(Output, *m_output, targetPhraseString, factorDelimiter, targetLHS);
      
      // rest of target phrase
      targetPhrase->SetAlignmentInfo(alignString);
      targetPhrase->SetTargetLHS(targetLHS);
      //targetPhrase->SetDebugOutput(string("New Format pt ") + line);
      
      // component score, for n-best output
      std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),TransformScore);
      std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),FloorScore);
      
      targetPhrase->SetScoreChart(GetFeature(), scoreVector, *m_weight, *m_languageModels, m_wpProducer);
      
      TargetPhraseCollection &phraseColl = GetOrCreateTargetPhraseCollection(rootNode, sourcePhrase, *targetPhrase, sourceLHS);
      phraseColl.Add(targetPhrase);
      
      count++;
      
      if (format == HieroFormat) { // reformat line
        delete line;
      }
      else
      { // do nothing
      }
      
    }
    
    // sort and prune each target phrase collection
    SortAndPrune(rootNode);
   
    //removedirectoryrecursively(dirName);
  }
コード例 #11
0
ファイル: smoothscan.c プロジェクト: ncraun/smoothscan
char*
generate_fonts (const JBDATA * data, const struct mapping *maps,
		int num_fonts, char *dir)
{
  int dirnamelen = 0;
  char *dirname = NULL;

  if (dir == NULL)
    {

      /* Create a file in the system temp dir (usually /tmp) */
      /* This may not be portable to Windows */
      char suffix[] = "smoothscan_XXXXXX";
      /* Use the value of environmental var TMPDIR if it is set */
      char *tmpdir = getenv ("TMPDIR");
      if (tmpdir == NULL)
	{
	  /* Load the default temp directory */
	  tmpdir = P_tmpdir;
	}

      int tmpdirlen = strlen (tmpdir);
      int suffixlen = strlen (suffix);

      dirnamelen = tmpdirlen + suffixlen + 1;

      dirname = malloc_guarded (dirnamelen + 1);

      sprintf (dirname, "%s/%s", tmpdir, suffix);

      if (mkdtemp (dirname) == NULL)
	{
	  error_quit ("Failed to create main temp directory.");
	}
    }
  else
    {
      if (mkdir (dir, 0700) == -1)
	{
	  if (errno != EEXIST)
	    {
	      error_quit ("Couldn't make tmpdir.");
	    }
	}
      dirname = dir;
      dirnamelen = strlen (dir);
    }

  int i;

  PIXA *templates =
    pixaCreateFromPix (data->pix, data->nclass, data->latticew,
		       data->latticeh);

  if (templates == NULL)
    {
      error_quit ("Could not create templates from JBDATA.");
    }

  /* Create a temp dir for each font */
  char **fontdirnames = malloc_guarded (num_fonts * sizeof (char *));
  int *fontdirlens = malloc_guarded (num_fonts * sizeof (int *));

  for (i = 0; i < num_fonts; i++)
    {
      /* 1 for / 8 for %08d */
      fontdirlens[i] = dirnamelen + 1 + 8;
      fontdirnames[i] = malloc_guarded (fontdirlens[i] + 1);

      sprintf (fontdirnames[i], "%s/%08d", dirname, i);

      if (mkdir (fontdirnames[i], 0700) == -1)
	{
	  error_quit ("Failed to create font temp directory.");
	}
    }


  for (i = 0; i < templates->n; i++)
    {
      l_int32 iclass;
      l_int32 x;
      l_int32 y;

      numaGetIValue (data->naclass, i, &iclass);
      ptaGetIPt (data->ptaul, i, &x, &y);
      PIX *pix_clone = pixaGetPix (templates, i, L_CLONE);	/* the template */
      double wp1 = pixGetWidth (pix_clone);
      double hp1 = pixGetHeight (pix_clone);
      PIX *pix_padded =
	pixAddBorderGeneral (pix_clone, 0, data->latticew - wp1, 0,
			     data->latticeh - hp1, 0);

      if (pix_padded == NULL)
	{
	  error_quit ("Failed to add border to image.");
	}

      int fontn = maps[i].font_num;
      int code_point = maps[i].code_point;

      /* 1 for '/', 3 for %03d, 4 for '.png' */
      int filenamelen = fontdirlens[fontn] + 1 + 3 + 4;
      char *filename = malloc_guarded (filenamelen + 1);

      sprintf (filename, "%s/%03d.png", fontdirnames[fontn], code_point);

      if (pixWrite (filename, pix_padded, IFF_PNG) == 1)
	{
	  printf ("pixWrite failed to write %s.\n", filename);
	  error_quit ("Could not write to file.");
	}

      pixDestroy (&pix_clone);
      pixDestroy (&pix_padded);
      free (filename);
    }

  pixaDestroy (&templates);

  /* This part probably won't port over to Windows as well */

  /* TODO: parallelize this */
  for (i = 0; i < num_fonts; i++)
    {
      /* 1 for '/', 8 for %08d, 4 for '.ttf' */
      int fontnamelen = dirnamelen + 1 + 8 + 4;
      char *fontnamestr = malloc_guarded (fontnamelen + 1);
      sprintf (fontnamestr, "%s/%08d.ttf", dirname, i);

      create_font_from_dir (fontdirnames[i], fontnamestr, data->latticeh,
			    data->latticew, i);
      free (fontnamestr);
    }

  /* clean up */
  for (i = 0; i < num_fonts; i++)
    {
      free (fontdirnames[i]);
    }
  free (fontdirnames);
  free (fontdirlens);

  return dirname;
}
コード例 #12
0
ファイル: rszshm.c プロジェクト: deadbits/ccan
void *rszshm_mk(struct rszshm *r, size_t flen, const char *fname, struct rszshm_scan scan)
{
	long pgsz = sysconf(_SC_PAGE_SIZE);
	int i, errno_;
	char *m, *tgt, *p = NULL;

	if (!r || flen == 0 || scan.len < flen + sizeof(*r->hdr) ||
	    !scan.start || scan.len == 0 || scan.hop == 0 || scan.iter == 0 ||
	    (fname && strnlen(fname, RSZSHM_PATH_MAX) == RSZSHM_PATH_MAX)) {
		errno = EINVAL;
		return NULL;
	}

	*r = (typeof(*r)) { -1, 0, "", NULL, NULL };
	strcpy(r->fname, fname ? fname : RSZSHM_DFLT_FNAME);

	flen = pgup(flen + sizeof(*r->hdr), pgsz);
	scan.len = pgup(scan.len, pgsz);

	for (i = 1, tgt = scan.start; i <= scan.iter; i++) {
		m = mmap(tgt, scan.len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON|MAP_NORESERVE, -1, 0);
		if (m == MAP_FAILED)
			return NULL;
		if (m == tgt)
			break;
		munmap(m, scan.len);
		m = NULL;
		tgt += (i % 2 == 0 ? 1 : -1) * i * scan.hop;
	}
	if (!m) {
		errno = ENOSPC;
		return NULL;
	}

	if ((p = strstr(r->fname, "XXXXXX/")) != NULL) {
		p += 6;
		*p = '\0';
		if (!mkdtemp(r->fname))
			goto err;
		*p = '/';
	}

	if ((r->fd = open(r->fname, O_CREAT|O_EXCL|O_RDWR, p ? 0600 : 0666)) == -1)
		goto err;

	if (ftruncate(r->fd, flen) == -1)
		goto err;

	if (mmap(m, flen, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, r->fd, 0) == MAP_FAILED)
		goto err;

	*(r->hdr = (typeof(r->hdr)) m) = (typeof(*r->hdr)) { flen, scan.len, m };

	if (msync(m, sizeof(*r->hdr), MS_SYNC) == -1)
		goto err;

	r->flen = flen;
	r->cap = flen - sizeof(*r->hdr);
	r->dat = m + sizeof(*r->hdr);

	return r->dat;

err:
	errno_ = errno;
	if (m && m != MAP_FAILED)
		munmap(m, scan.len);
	if (r->fd != -1) {
		close(r->fd);
		unlink(r->fname);
	}
	if (p) {
		*p = '\0';
		rmdir(r->fname);
		*p = '/';
	}
	errno = errno_;
	return NULL;
}
コード例 #13
0
ファイル: machine-pool.c プロジェクト: Stratoscale/systemd
int setup_machine_directory(uint64_t size, sd_bus_error *error) {
        _cleanup_release_lock_file_ LockFile lock_file = LOCK_FILE_INIT;
        struct loop_info64 info = {
                .lo_flags = LO_FLAGS_AUTOCLEAR,
        };
        _cleanup_close_ int fd = -1, control = -1, loop = -1;
        _cleanup_free_ char* loopdev = NULL;
        char tmpdir[] = "/tmp/import-mount.XXXXXX", *mntdir = NULL;
        bool tmpdir_made = false, mntdir_made = false, mntdir_mounted = false;
        char buf[FORMAT_BYTES_MAX];
        int r, nr = -1;

        /* btrfs cannot handle file systems < 16M, hence use this as minimum */
        if (size == (uint64_t) -1)
                size = VAR_LIB_MACHINES_SIZE_START;
        else if (size < 16*1024*1024)
                size = 16*1024*1024;

        /* Make sure we only set the directory up once at a time */
        r = make_lock_file("/run/systemd/machines.lock", LOCK_EX, &lock_file);
        if (r < 0)
                return r;

        r = check_btrfs();
        if (r < 0)
                return sd_bus_error_set_errnof(error, r, "Failed to determine whether /var/lib/machines is located on btrfs: %m");
        if (r > 0) {
                (void) btrfs_subvol_make_label("/var/lib/machines");

                r = btrfs_quota_enable("/var/lib/machines", true);
                if (r < 0)
                        log_warning_errno(r, "Failed to enable quota, ignoring: %m");

                return 0;
        }

        if (path_is_mount_point("/var/lib/machines", true) > 0 ||
            dir_is_empty("/var/lib/machines") == 0)
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "/var/lib/machines is not a btrfs file system. Operation is not supported on legacy file systems.");

        fd = setup_machine_raw(size, error);
        if (fd < 0)
                return fd;

        control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
        if (control < 0)
                return sd_bus_error_set_errnof(error, errno, "Failed to open /dev/loop-control: %m");

        nr = ioctl(control, LOOP_CTL_GET_FREE);
        if (nr < 0)
                return sd_bus_error_set_errnof(error, errno, "Failed to allocate loop device: %m");

        if (asprintf(&loopdev, "/dev/loop%i", nr) < 0) {
                r = -ENOMEM;
                goto fail;
        }

        loop = open(loopdev, O_CLOEXEC|O_RDWR|O_NOCTTY|O_NONBLOCK);
        if (loop < 0) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to open loopback device: %m");
                goto fail;
        }

        if (ioctl(loop, LOOP_SET_FD, fd) < 0) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to bind loopback device: %m");
                goto fail;
        }

        if (ioctl(loop, LOOP_SET_STATUS64, &info) < 0) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to enable auto-clear for loopback device: %m");
                goto fail;
        }

        /* We need to make sure the new /var/lib/machines directory
         * has an access mode of 0700 at the time it is first made
         * available. mkfs will create it with 0755 however. Hence,
         * let's mount the directory into an inaccessible directory
         * below /tmp first, fix the access mode, and move it to the
         * public place then. */

        if (!mkdtemp(tmpdir)) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to create temporary mount parent directory: %m");
                goto fail;
        }
        tmpdir_made = true;

        mntdir = strjoina(tmpdir, "/mnt");
        if (mkdir(mntdir, 0700) < 0) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to create temporary mount directory: %m");
                goto fail;
        }
        mntdir_made = true;

        if (mount(loopdev, mntdir, "btrfs", 0, NULL) < 0) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to mount loopback device: %m");
                goto fail;
        }
        mntdir_mounted = true;

        r = btrfs_quota_enable(mntdir, true);
        if (r < 0)
                log_warning_errno(r, "Failed to enable quota, ignoring: %m");

        if (chmod(mntdir, 0700) < 0) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to fix owner: %m");
                goto fail;
        }

        (void) mkdir_p_label("/var/lib/machines", 0700);

        if (mount(mntdir, "/var/lib/machines", NULL, MS_BIND, NULL) < 0) {
                r = sd_bus_error_set_errnof(error, errno, "Failed to mount directory into right place: %m");
                goto fail;
        }

        (void) syncfs(fd);

        log_info("Set up /var/lib/machines as btrfs loopback file system of size %s mounted on /var/lib/machines.raw.", format_bytes(buf, sizeof(buf), size));

        (void) umount2(mntdir, MNT_DETACH);
        (void) rmdir(mntdir);
        (void) rmdir(tmpdir);

        return 0;

fail:
        if (mntdir_mounted)
                (void) umount2(mntdir, MNT_DETACH);

        if (mntdir_made)
                (void) rmdir(mntdir);
        if (tmpdir_made)
                (void) rmdir(tmpdir);

        if (loop >= 0) {
                (void) ioctl(loop, LOOP_CLR_FD);
                loop = safe_close(loop);
        }

        if (control >= 0 && nr >= 0)
                (void) ioctl(control, LOOP_CTL_REMOVE, nr);

        return r;
}
コード例 #14
0
ファイル: test-fs-util.c プロジェクト: GuillaumeSeren/systemd
static void test_chase_symlinks(void) {
        _cleanup_free_ char *result = NULL;
        char temp[] = "/tmp/test-chase.XXXXXX";
        const char *top, *p, *q;
        int r;

        assert_se(mkdtemp(temp));

        top = strjoina(temp, "/top");
        assert_se(mkdir(top, 0700) >= 0);

        p = strjoina(top, "/dot");
        assert_se(symlink(".", p) >= 0);

        p = strjoina(top, "/dotdot");
        assert_se(symlink("..", p) >= 0);

        p = strjoina(top, "/dotdota");
        assert_se(symlink("../a", p) >= 0);

        p = strjoina(temp, "/a");
        assert_se(symlink("b", p) >= 0);

        p = strjoina(temp, "/b");
        assert_se(symlink("/usr", p) >= 0);

        p = strjoina(temp, "/start");
        assert_se(symlink("top/dot/dotdota", p) >= 0);

        /* Paths that use symlinks underneath the "root" */

        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r > 0);
        assert_se(path_equal(result, "/usr"));

        result = mfree(result);
        r = chase_symlinks(p, temp, 0, &result);
        assert_se(r == -ENOENT);

        q = strjoina(temp, "/usr");

        r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result);
        assert_se(r == 0);
        assert_se(path_equal(result, q));

        assert_se(mkdir(q, 0700) >= 0);

        result = mfree(result);
        r = chase_symlinks(p, temp, 0, &result);
        assert_se(r > 0);
        assert_se(path_equal(result, q));

        p = strjoina(temp, "/slash");
        assert_se(symlink("/", p) >= 0);

        result = mfree(result);
        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r > 0);
        assert_se(path_equal(result, "/"));

        result = mfree(result);
        r = chase_symlinks(p, temp, 0, &result);
        assert_se(r > 0);
        assert_se(path_equal(result, temp));

        /* Paths that would "escape" outside of the "root" */

        p = strjoina(temp, "/6dots");
        assert_se(symlink("../../..", p) >= 0);

        result = mfree(result);
        r = chase_symlinks(p, temp, 0, &result);
        assert_se(r > 0 && path_equal(result, temp));

        p = strjoina(temp, "/6dotsusr");
        assert_se(symlink("../../../usr", p) >= 0);

        result = mfree(result);
        r = chase_symlinks(p, temp, 0, &result);
        assert_se(r > 0 && path_equal(result, q));

        p = strjoina(temp, "/top/8dotsusr");
        assert_se(symlink("../../../../usr", p) >= 0);

        result = mfree(result);
        r = chase_symlinks(p, temp, 0, &result);
        assert_se(r > 0 && path_equal(result, q));

        /* Paths that contain repeated slashes */

        p = strjoina(temp, "/slashslash");
        assert_se(symlink("///usr///", p) >= 0);

        result = mfree(result);
        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r > 0);
        assert_se(path_equal(result, "/usr"));

        result = mfree(result);
        r = chase_symlinks(p, temp, 0, &result);
        assert_se(r > 0);
        assert_se(path_equal(result, q));

        /* Paths using . */

        result = mfree(result);
        r = chase_symlinks("/etc/./.././", NULL, 0, &result);
        assert_se(r > 0);
        assert_se(path_equal(result, "/"));

        result = mfree(result);
        r = chase_symlinks("/etc/./.././", "/etc", 0, &result);
        assert_se(r > 0 && path_equal(result, "/etc"));

        result = mfree(result);
        r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result);
        assert_se(r == -ENOTDIR);

        /* Path that loops back to self */

        result = mfree(result);
        p = strjoina(temp, "/recursive-symlink");
        assert_se(symlink("recursive-symlink", p) >= 0);
        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r == -ELOOP);

        /* Path which doesn't exist */

        p = strjoina(temp, "/idontexist");
        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r == -ENOENT);

        r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
        assert_se(r == 0);
        assert_se(path_equal(result, p));
        result = mfree(result);

        p = strjoina(temp, "/idontexist/meneither");
        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r == -ENOENT);

        r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
        assert_se(r == 0);
        assert_se(path_equal(result, p));
        result = mfree(result);

        /* Path which doesn't exist, but contains weird stuff */

        p = strjoina(temp, "/idontexist/..");
        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r == -ENOENT);

        r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
        assert_se(r == -ENOENT);

        p = strjoina(temp, "/target");
        q = strjoina(temp, "/top");
        assert_se(symlink(q, p) >= 0);
        p = strjoina(temp, "/target/idontexist");
        r = chase_symlinks(p, NULL, 0, &result);
        assert_se(r == -ENOENT);

        assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
}
コード例 #15
0
ファイル: test-journal.c プロジェクト: lnykryn/systemd
static void test_non_empty(void) {
        dual_timestamp ts;
        JournalFile *f;
        struct iovec iovec;
        static const char test[] = "TEST1=1", test2[] = "TEST2=2";
        Object *o;
        uint64_t p;
        char t[] = "/tmp/journal-XXXXXX";

        log_set_max_level(LOG_DEBUG);

        assert_se(mkdtemp(t));
        assert_se(chdir(t) >= 0);

        assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, true, NULL, NULL, NULL, &f) == 0);

        dual_timestamp_get(&ts);

        iovec.iov_base = (void*) test;
        iovec.iov_len = strlen(test);
        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);

        iovec.iov_base = (void*) test2;
        iovec.iov_len = strlen(test2);
        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);

        iovec.iov_base = (void*) test;
        iovec.iov_len = strlen(test);
        assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);

#ifdef HAVE_GCRYPT
        journal_file_append_tag(f);
#endif
        journal_file_dump(f);

        assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 3);

        assert_se(journal_file_next_entry(f, p, DIRECTION_DOWN, &o, &p) == 0);

        assert_se(journal_file_next_entry(f, 0, DIRECTION_DOWN, &o, &p) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_find_data_object(f, test, strlen(test), NULL, &p) == 1);
        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 3);

        assert_se(journal_file_find_data_object(f, test2, strlen(test2), NULL, &p) == 1);
        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_find_data_object(f, "quux", 4, NULL, &p) == 0);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 1, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 1);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 3, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 3);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 2, DIRECTION_DOWN, &o, NULL) == 1);
        assert_se(le64toh(o->entry.seqnum) == 2);

        assert_se(journal_file_move_to_entry_by_seqnum(f, 10, DIRECTION_DOWN, &o, NULL) == 0);

        journal_file_rotate(&f, true, true);
        journal_file_rotate(&f, true, true);

        journal_file_close(f);

        log_info("Done...");

        if (arg_keep)
                log_info("Not removing %s", t);
        else {
                journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);

                assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
        }

        puts("------------------------------------------------------------");
}
コード例 #16
0
ファイル: main.c プロジェクト: xtrm0/par-shell
int main() {
  int inputPipe;
  char buffer[BUFFER_LEN];
  char mktemp_filename[PATH_MAX];
  char mktemp_dir[PATH_MAX];
  int ret;
  int terminalPid;
  TerminalList * termlist = lst_new();
  pthread_t threadMonitor;
  char *args[N_ARGS];
  if (signal(SIGINT, handleSIGINT)) {
    fprintf(stderr, "Could not set sigint handler!\n");
    exit(EXIT_FAILURE);
  }
  if (pthread_mutex_init(&mutexRunningProcesses, NULL)) {
    fprintf(stderr, "Could not create runningProcesses mutex\n");
    exit(EXIT_FAILURE);
  }
  if (pthread_cond_init(&condRunningProcesses, NULL)) {
    fprintf(stderr, "Could not create runningProcesses cond\n");
    exit(EXIT_FAILURE);
  }
  if (pthread_cond_init(&condFreeSlots, NULL)) {
    fprintf(stderr, "Could not create FreeSlots cond\n");
    exit(EXIT_FAILURE);
  }

  strcpy(mktemp_dir, MKTEMP_TEMPLATE);
  TESTTRUE(mkdtemp(mktemp_dir)!=NULL, "Erro na criação do diretorio temporário (" _AT_ ")\n");
  strncpy(mktemp_filename, mktemp_dir, PATH_MAX);
  strncpy(mktemp_filename+strlen(mktemp_filename), "/" INPUT_FILE, PATH_MAX-strlen(mktemp_filename));
  fprintf(stdin, "Ficheiro de input: '%s'\n", mktemp_filename);
  if (mkfifo(mktemp_filename, 0660) <0) {
    fprintf(stderr, "Could not create fifo %s\n", mktemp_filename);
    exit(EXIT_FAILURE);
  }
  printf("A abrir o pipe %s para leitura...\n", mktemp_filename);
  if ((inputPipe = open(mktemp_filename, O_RDONLY)) < 0) {
    fprintf(stderr, "Could not create fifo " INPUT_FILE "\n");
    exit(EXIT_FAILURE);
  }
  printf("A abrir o pipe %s para escrita...\n", mktemp_filename);
  if ((outPipe = open(mktemp_filename, O_WRONLY|O_NONBLOCK)) < 0) {
    fprintf(stderr, "Erro ao abrir o ficheiro de output" INPUT_FILE "\n");
    exit(EXIT_FAILURE);
  }
  initProcessList();
  if(pthread_create(&threadMonitor, 0,processMonitor, NULL)!= 0) {
    printf("Erro na criação da tarefa\n");
    exit(EXIT_FAILURE);
  }

  while(1) {
    if (exitCalled) break;
    if (readFromPipe(inputPipe, buffer, &terminalPid, termlist)!=0) continue;
    printf("Comando: %s\n", buffer);
    ret = readLineArguments(args, N_ARGS, buffer, BUFFER_LEN);
    if (!ret) continue;
    processesWaitingToRun++;
    newProcess(args, terminalPid);
  }

  //Mata todos os processos de terminal
  lst_destroy(termlist);

  C_SIGNAL(&condRunningProcesses);

  pthread_join(threadMonitor, NULL);



  //The following function is called after all threads have joined, therefore there aren't used any mutexes
  exitParShell();
  endProcessList();

  pthread_mutex_destroy(&mutexRunningProcesses);
  pthread_cond_destroy(&condRunningProcesses);
  pthread_cond_destroy(&condFreeSlots);
  close(inputPipe); //aqui nao faz sentido testar o return destas funcoes
  close(outPipe); //aqui nao faz sentido testar o return destas funcoes
  unlink(INPUT_FILE); //aqui nao faz sentido testar o return destas funcoes

  return EXIT_SUCCESS;
}
コード例 #17
0
ファイル: test_egd.c プロジェクト: teor2345/libottery-lite
static void
test_egd_success(void *arg)
{
  int pipefds[2] = {-1,-1};
  struct sockaddr_un sun;
  char dir[128] = "/tmp/otterylite_test_XXXXXX";
  char fname[128] = {0};
  int listener = -1;
  pid_t pid;
  int r, exitstatus=0;
  u8 buf[64];
  unsigned flags;

  (void)arg;


  tt_assert(mkdtemp(dir) != NULL);
  tt_int_op(-1, ==, ottery_egd_socklen);
  tt_int_op(-1, ==, OTTERY_PUBLIC_FN(set_egd_address)((struct sockaddr*)&sun, 16384));

  tt_assert(strlen(dir) < 128 - 32);
  snprintf(fname, sizeof(fname), "%s/fifo", dir);
  tt_int_op(strlen(fname), <, sizeof(sun.sun_path));

  tt_int_op(-2, ==, ottery_getentropy_egd(buf, &flags)); /* not turned on. */

  memset(&sun, 0, sizeof(sun));
  sun.sun_family = -1; /* Bad family */
  tt_int_op(0, ==, OTTERY_PUBLIC_FN(set_egd_address)((struct sockaddr*)&sun, sizeof(sun)));
  tt_int_op(-1, ==, ottery_getentropy_egd(buf, &flags)); /* bad family */

  sun.sun_family = AF_UNIX;
  memcpy(sun.sun_path, fname, strlen(fname)+1);
  listener = socket(AF_UNIX, SOCK_STREAM, 0);
  tt_int_op(listener, >=, 0);

  tt_int_op(0, ==, pipe(pipefds));

  tt_int_op(0, ==, OTTERY_PUBLIC_FN(set_egd_address)((struct sockaddr*)&sun, sizeof(sun)));

  tt_int_op(sizeof(sun), ==, ottery_egd_socklen);

  tt_int_op(-1, ==, ottery_getentropy_egd(buf, &flags)); /* nobody listening yet */

  tt_int_op(bind(listener, (struct sockaddr*)&sun, sizeof(sun)), ==, 0);
  tt_int_op(listen(listener, 16), ==, 0);

  if ((pid = fork())) {
    /* parent */
    close(listener);
    close(pipefds[1]);
  } else {
    /* child */
    close(pipefds[0]);
    run_egd_server(listener, pipefds[1]);
    exit(1);
  }

  memset(buf, 0, sizeof(buf));
  r = ottery_getentropy_egd(buf, &flags);
  tt_int_op(r, ==, ENTROPY_CHUNK);
  tt_assert(iszero(buf + ENTROPY_CHUNK, sizeof(buf) - ENTROPY_CHUNK));
  tt_assert(! iszero(buf, ENTROPY_CHUNK));
  tt_mem_op(buf, ==, notsorandom, ENTROPY_CHUNK);

  r = (int)read(pipefds[0], buf, 1);
  tt_int_op(r, ==, 1);
  waitpid(pid, &exitstatus, 0);
  tt_int_op(exitstatus, ==, 0);
  tt_int_op(buf[0], ==, 'Y');

  tt_int_op(0, ==, OTTERY_PUBLIC_FN(set_egd_address)(NULL, 0));
  tt_int_op(-1, ==, ottery_egd_socklen);

 end:
  if (pipefds[0] >= 0)
    close(pipefds[0]);
  if (pipefds[1] >= 1)
    close(pipefds[1]);
  if (*fname)
    unlink(fname);
  rmdir(dir);
}
コード例 #18
0
ファイル: transport.c プロジェクト: Carina26/git
static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
{
	struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
	struct ref dummy = {NULL}, *tail = &dummy;
	struct child_process rsync = CHILD_PROCESS_INIT;
	const char *args[5];
	int temp_dir_len;

	if (for_push)
		return NULL;

	/* copy the refs to the temporary directory */

	strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
	if (!mkdtemp(temp_dir.buf))
		die_errno ("Could not make temporary directory");
	temp_dir_len = temp_dir.len;

	strbuf_addstr(&buf, rsync_url(transport->url));
	strbuf_addstr(&buf, "/refs");

	rsync.argv = args;
	rsync.stdout_to_stderr = 1;
	args[0] = "rsync";
	args[1] = (transport->verbose > 1) ? "-rv" : "-r";
	args[2] = buf.buf;
	args[3] = temp_dir.buf;
	args[4] = NULL;

	if (run_command(&rsync))
		die ("Could not run rsync to get refs");

	strbuf_reset(&buf);
	strbuf_addstr(&buf, rsync_url(transport->url));
	strbuf_addstr(&buf, "/packed-refs");

	args[2] = buf.buf;

	if (run_command(&rsync))
		die ("Could not run rsync to get refs");

	/* read the copied refs */

	strbuf_addstr(&temp_dir, "/refs");
	read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
	strbuf_setlen(&temp_dir, temp_dir_len);

	tail = &dummy;
	strbuf_addstr(&temp_dir, "/packed-refs");
	insert_packed_refs(temp_dir.buf, &tail);
	strbuf_setlen(&temp_dir, temp_dir_len);

	if (remove_dir_recursively(&temp_dir, 0))
		warning ("Error removing temporary directory %s.",
				temp_dir.buf);

	strbuf_release(&buf);
	strbuf_release(&temp_dir);

	return dummy.next;
}
コード例 #19
0
void PhraseDictionaryFuzzyMatch::InitializeForInput(ttasksptr const& ttask)
{
  InputType const& inputSentence = *ttask->GetSource();
#if defined __MINGW32__
  char dirName[] = "moses.XXXXXX";
#else
  char dirName[] = "/tmp/moses.XXXXXX";
#endif // defined
  char *temp = mkdtemp(dirName);
  UTIL_THROW_IF2(temp == NULL,
                 "Couldn't create temporary directory " << dirName);

  string dirNameStr(dirName);

  string inFileName(dirNameStr + "/in");

  ofstream inFile(inFileName.c_str());

  for (size_t i = 1; i < inputSentence.GetSize() - 1; ++i) {
    inFile << inputSentence.GetWord(i);
  }
  inFile << endl;
  inFile.close();

  long translationId = inputSentence.GetTranslationId();
  string ptFileName = m_FuzzyMatchWrapper->Extract(translationId, dirNameStr);

  // populate with rules for this sentence
  PhraseDictionaryNodeMemory &rootNode = m_collection[translationId];
  FormatType format = MosesFormat;

  // data from file
  InputFileStream inStream(ptFileName);

  // copied from class LoaderStandard
  PrintUserTime("Start loading fuzzy-match phrase model");

  const StaticData &staticData = StaticData::Instance();


  string lineOrig;
  size_t count = 0;

  while(getline(inStream, lineOrig)) {
    const string *line;
    if (format == HieroFormat) { // reformat line
      UTIL_THROW(util::Exception, "Cannot be Hiero format");
      //line = ReformatHieroRule(lineOrig);
    } else {
      // do nothing to format of line
      line = &lineOrig;
    }

    vector<string> tokens;
    vector<float> scoreVector;

    TokenizeMultiCharSeparator(tokens, *line , "|||" );

    if (tokens.size() != 4 && tokens.size() != 5) {
      UTIL_THROW2("Syntax error at " << ptFileName << ":" << count);
    }

    const string &sourcePhraseString = tokens[0]
                                       , &targetPhraseString = tokens[1]
                                           , &scoreString        = tokens[2]
                                               , &alignString        = tokens[3];

    bool isLHSEmpty = (sourcePhraseString.find_first_not_of(" \t", 0) == string::npos);
    if (isLHSEmpty && !staticData.IsWordDeletionEnabled()) {
      TRACE_ERR( ptFileName << ":" << count << ": pt entry contains empty target, skipping\n");
      continue;
    }

    Tokenize<float>(scoreVector, scoreString);
    const size_t numScoreComponents = GetNumScoreComponents();
    if (scoreVector.size() != numScoreComponents) {
      UTIL_THROW2("Size of scoreVector != number (" << scoreVector.size() << "!="
                  << numScoreComponents << ") of score components on line " << count);
    }

    UTIL_THROW_IF2(scoreVector.size() != numScoreComponents,
                   "Number of scores incorrectly specified");

    // parse source & find pt node

    // constituent labels
    Word *sourceLHS;
    Word *targetLHS;

    // source
    Phrase sourcePhrase( 0);
    sourcePhrase.CreateFromString(Input, m_input, sourcePhraseString, &sourceLHS);

    // create target phrase obj
    TargetPhrase *targetPhrase = new TargetPhrase(this);
    targetPhrase->CreateFromString(Output, m_output, targetPhraseString, &targetLHS);

    // rest of target phrase
    targetPhrase->SetAlignmentInfo(alignString);
    targetPhrase->SetTargetLHS(targetLHS);
    //targetPhrase->SetDebugOutput(string("New Format pt ") + line);

    // component score, for n-best output
    std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),TransformScore);
    std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(),FloorScore);

    targetPhrase->GetScoreBreakdown().Assign(this, scoreVector);
    targetPhrase->EvaluateInIsolation(sourcePhrase, GetFeaturesToApply());

    TargetPhraseCollection::shared_ptr phraseColl
    = GetOrCreateTargetPhraseCollection(rootNode, sourcePhrase,
                                        *targetPhrase, sourceLHS);
    phraseColl->Add(targetPhrase);

    count++;

    if (format == HieroFormat) { // reformat line
      delete line;
    } else {
      // do nothing
    }

  }

  // sort and prune each target phrase collection
  SortAndPrune(rootNode);

  //removedirectoryrecursively(dirName);
}
コード例 #20
0
ファイル: transport.c プロジェクト: Carina26/git
static int rsync_transport_push(struct transport *transport,
		int refspec_nr, const char **refspec, int flags)
{
	struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
	int result = 0, i;
	struct child_process rsync = CHILD_PROCESS_INIT;
	const char *args[10];

	if (flags & TRANSPORT_PUSH_MIRROR)
		return error("rsync transport does not support mirror mode");

	/* first push the objects */

	strbuf_addstr(&buf, rsync_url(transport->url));
	strbuf_addch(&buf, '/');

	rsync.argv = args;
	rsync.stdout_to_stderr = 1;
	i = 0;
	args[i++] = "rsync";
	args[i++] = "-a";
	if (flags & TRANSPORT_PUSH_DRY_RUN)
		args[i++] = "--dry-run";
	if (transport->verbose > 1)
		args[i++] = "-v";
	args[i++] = "--ignore-existing";
	args[i++] = "--exclude";
	args[i++] = "info";
	args[i++] = get_object_directory();
	args[i++] = buf.buf;
	args[i++] = NULL;

	if (run_command(&rsync))
		return error("Could not push objects to %s",
				rsync_url(transport->url));

	/* copy the refs to the temporary directory; they could be packed. */

	strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
	if (!mkdtemp(temp_dir.buf))
		die_errno ("Could not make temporary directory");
	strbuf_addch(&temp_dir, '/');

	if (flags & TRANSPORT_PUSH_ALL) {
		if (for_each_ref(write_one_ref, &temp_dir))
			return -1;
	} else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
		return -1;

	i = 2;
	if (flags & TRANSPORT_PUSH_DRY_RUN)
		args[i++] = "--dry-run";
	if (!(flags & TRANSPORT_PUSH_FORCE))
		args[i++] = "--ignore-existing";
	args[i++] = temp_dir.buf;
	args[i++] = rsync_url(transport->url);
	args[i++] = NULL;
	if (run_command(&rsync))
		result = error("Could not push to %s",
				rsync_url(transport->url));

	if (remove_dir_recursively(&temp_dir, 0))
		warning ("Could not remove temporary directory %s.",
				temp_dir.buf);

	strbuf_release(&buf);
	strbuf_release(&temp_dir);

	return result;
}
コード例 #21
0
ファイル: root_dictionaries.cpp プロジェクト: ndawe/drillbit
void init() {
    if (not initialized) {
        mkdtemp(dictionary_tmpdir);
        atexit(cleanup_root_dictionaries);
    }
}
コード例 #22
0
ファイル: tst-dir.c プロジェクト: BackupTheBerlios/wl530g-svn
/* We expect four arguments:
   - source directory name
   - object directory
   - common object directory
   - the program name with path
*/
int
main (int argc, char *argv[])
{
    const char *srcdir;
    const char *objdir;
    const char *common_objdir;
    const char *progpath;
    struct stat64 st1;
    struct stat64 st2;
    struct stat64 st3;
    DIR *dir1;
    DIR *dir2;
    int result = 0;
    struct dirent64 *d;
    struct dirent64 direntbuf;
    char *objdir_copy1;
    char *objdir_copy2;
    char *buf;
    int fd;

    mtrace ();

    if (argc < 5)
    {
        puts ("not enough parameters");
        exit (1);
    }

    /* Make parameters available with nicer names.  */
    srcdir = argv[1];
    objdir = argv[2];
    common_objdir = argv[3];
    progpath = argv[4];

    /* First test the current source dir.  We cannot really compare the
       result of `getpwd' with the srcdir string but we have other means.  */
    if (stat64 (".", &st1) < 0)
    {
        printf ("cannot stat starting directory: %m\n");
        exit (1);
    }

    if (chdir (srcdir) < 0)
    {
        printf ("cannot change to source directory: %m\n");
        exit (1);
    }
    if (stat64 (".", &st2) < 0)
    {
        printf ("cannot stat source directory: %m\n");
        exit (1);
    }

    /* The two last stat64 calls better were for the same directory.  */
    if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
    {
        printf ("stat of source directory failed: (%lld,%lld) vs (%lld,%lld)\n",
                (long long int) st1.st_dev, (long long int) st1.st_ino,
                (long long int) st2.st_dev, (long long int) st2.st_ino);
        exit (1);
    }

    /* Change to the object directory.  */
    if (chdir (objdir) < 0)
    {
        printf ("cannot change to object directory: %m\n");
        exit (1);
    }
    if (stat64 (".", &st1) < 0)
    {
        printf ("cannot stat object directory: %m\n");
        exit (1);
    }
    /* Is this the same we get as with the full path?  */
    if (stat64 (objdir, &st2) < 0)
    {
        printf ("cannot stat object directory with full path: %m\n");
        exit (1);
    }
    if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
    {
        printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
                (long long int) st1.st_dev, (long long int) st1.st_ino,
                (long long int) st2.st_dev, (long long int) st2.st_ino);
        exit (1);
    }

    objdir_copy1 = getcwd (NULL, 0);
    if (objdir_copy1 == NULL)
    {
        printf ("cannot get current directory name for object directory: %m\n");
        result = 1;
    }

    /* First test: this directory must include our program.  */
    if (stat64 (progpath, &st2) < 0)
    {
        printf ("cannot stat program: %m\n");
        exit (1);
    }

    dir1 = opendir (".");
    if (dir1 == NULL)
    {
        printf ("cannot open object directory: %m\n");
        exit (1);
    }

    while ((d = readdir64 (dir1)) != NULL)
    {
#ifdef _DIRENT_HAVE_D_TYPE
        if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
            continue;
#endif

        if (d->d_ino == st2.st_ino)
        {
            /* Might be it.  Test the device.  We could use the st_dev
               element from st1 but what the heck, do more testing.  */
            if (stat64 (d->d_name, &st3) < 0)
            {
                printf ("cannot stat entry from readdir: %m\n");
                result = 1;
                d = NULL;
                break;
            }

            if (st3.st_dev == st2.st_dev)
                break;
        }
    }

    if (d == NULL)
    {
        puts ("haven't found program in object directory");
        result = 1;
    }

    /* We leave dir1 open.  */

    /* Stat using file descriptor.  */
    if (fstat64 (dirfd (dir1), &st2) < 0)
    {
        printf ("cannot fstat object directory: %m\n");
        result = 1;
    }
    if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
    {
        printf ("fstat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
                (long long int) st1.st_dev, (long long int) st1.st_ino,
                (long long int) st2.st_dev, (long long int) st2.st_ino);
        exit (1);
    }

    if (chdir ("..") < 0)
    {
        printf ("cannot go to common object directory with \"..\": %m\n");
        exit (1);
    }

    if (stat64 (".", &st1) < 0)
    {
        printf ("cannot stat common object directory: %m\n");
        exit (1);
    }
    /* Is this the same we get as with the full path?  */
    if (stat64 (common_objdir, &st2) < 0)
    {
        printf ("cannot stat common object directory with full path: %m\n");
        exit (1);
    }
    if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
    {
        printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
                (long long int) st1.st_dev, (long long int) st1.st_ino,
                (long long int) st2.st_dev, (long long int) st2.st_ino);
        exit (1);
    }

    /* Stat using file descriptor.  */
    if (fstat64 (dirfd (dir1), &st2) < 0)
    {
        printf ("cannot fstat object directory: %m\n");
        result = 1;
    }

    dir2 = opendir (common_objdir);
    if (dir2 == NULL)
    {
        printf ("cannot open common object directory: %m\n");
        exit (1);
    }

    while ((d = readdir64 (dir2)) != NULL)
    {
#ifdef _DIRENT_HAVE_D_TYPE
        if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
            continue;
#endif

        if (d->d_ino == st2.st_ino)
        {
            /* Might be it.  Test the device.  We could use the st_dev
               element from st1 but what the heck, do more testing.  */
            if (stat64 (d->d_name, &st3) < 0)
            {
                printf ("cannot stat entry from readdir: %m\n");
                result = 1;
                d = NULL;
                break;
            }

            if (st3.st_dev == st2.st_dev)
                break;
        }
    }

    /* This better should be the object directory again.  */
    if (fchdir (dirfd (dir1)) < 0)
    {
        printf ("cannot fchdir to object directory: %m\n");
        exit (1);
    }

    objdir_copy2 = getcwd (NULL, 0);
    if (objdir_copy2 == NULL)
    {
        printf ("cannot get current directory name for object directory: %m\n");
        result = 1;
    }
    if (strcmp (objdir_copy1, objdir_copy2) != 0)
    {
        puts ("getcwd returned a different string the second time");
        result = 1;
    }

    /* This better should be the common object directory again.  */
    if (fchdir (dirfd (dir2)) < 0)
    {
        printf ("cannot fchdir to common object directory: %m\n");
        exit (1);
    }

    if (stat64 (".", &st2) < 0)
    {
        printf ("cannot stat common object directory: %m\n");
        exit (1);
    }
    if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
    {
        printf ("stat of object directory failed: (%lld,%lld) vs (%lld,%lld)\n",
                (long long int) st1.st_dev, (long long int) st1.st_ino,
                (long long int) st2.st_dev, (long long int) st2.st_ino);
        exit (1);
    }

    buf = (char *) malloc (strlen (objdir_copy1) + 1 + sizeof "tst-dir.XXXXXX");
    if (buf == NULL)
    {
        printf ("cannot allocate buffer: %m");
        exit (1);
    }

    stpcpy (stpcpy (stpcpy (buf, objdir_copy1), "/"), "tst-dir.XXXXXX");
    if (mkdtemp (buf) == NULL)
    {
        printf ("cannot create test directory in object directory: %m\n");
        exit (1);
    }
    if (stat64 (buf, &st1) < 0)
    {
        printf ("cannot stat new directory \"%s\": %m\n", buf);
        exit (1);
    }
    if (chmod (buf, 0700) < 0)
    {
        printf ("cannot change mode of new directory: %m\n");
        exit (1);
    }

    /* Try to find the new directory.  */
    rewinddir (dir1);
    while (readdir64_r (dir1, &direntbuf, &d) == 0 && d != NULL)
    {
#ifdef _DIRENT_HAVE_D_TYPE
        if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
            continue;
#endif

        if (d->d_ino == st1.st_ino)
        {
            /* Might be it.  Test the device.  We could use the st_dev
               element from st1 but what the heck, do more testing.  */
            size_t len = strlen (objdir) + 1 + _D_EXACT_NAMLEN (d) + 1;
            char tmpbuf[len];

            stpcpy (stpcpy (stpcpy (tmpbuf, objdir), "/"), d->d_name);

            if (stat64 (tmpbuf, &st3) < 0)
            {
                printf ("cannot stat entry from readdir: %m\n");
                result = 1;
                d = NULL;
                break;
            }

            if (st3.st_dev == st2.st_dev
                    && strcmp (d->d_name, buf + strlen (buf) - 14) == 0)
                break;
        }
    }

    if (d == NULL)
    {
        printf ("haven't found new directory \"%s\"\n", buf);
        exit (1);
    }

    if (closedir (dir2) < 0)
    {
        printf ("closing dir2 failed: %m\n");
        result = 1;
    }

    if (chdir (buf) < 0)
    {
        printf ("cannot change to new directory: %m\n");
        exit (1);
    }

    dir2 = opendir (buf);
    if (dir2 == NULL)
    {
        printf ("cannot open new directory: %m\n");
        exit (1);
    }

    if (fstat64 (dirfd (dir2), &st2) < 0)
    {
        printf ("cannot fstat new directory \"%s\": %m\n", buf);
        exit (1);
    }
    if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
    {
        printf ("stat of new directory failed: (%lld,%lld) vs (%lld,%lld)\n",
                (long long int) st1.st_dev, (long long int) st1.st_ino,
                (long long int) st2.st_dev, (long long int) st2.st_ino);
        exit (1);
    }

    if (mkdir ("another-dir", 0777) < 0)
    {
        printf ("cannot create \"another-dir\": %m\n");
        exit (1);
    }
    fd = open ("and-a-file", O_RDWR | O_CREAT | O_EXCL, 0666);
    if (fd == -1)
    {
        printf ("cannot create \"and-a-file\": %m\n");
        exit (1);
    }
    close (fd);

    /* Some tests about error reporting.  */
    errno = 0;
    if (chdir ("and-a-file") >= 0)
    {
        printf ("chdir to \"and-a-file\" succeeded\n");
        exit (1);
    }
    if (errno != ENOTDIR)
    {
        printf ("chdir to \"and-a-file\" didn't set correct error\n");
        result = 1;
    }

    errno = 0;
    if (chdir ("and-a-file/..") >= 0)
    {
        printf ("chdir to \"and-a-file/..\" succeeded\n");
        exit (1);
    }
    if (errno != ENOTDIR)
    {
        printf ("chdir to \"and-a-file/..\" didn't set correct error\n");
        result = 1;
    }

    errno = 0;
    if (chdir ("another-dir/../and-a-file") >= 0)
    {
        printf ("chdir to \"another-dir/../and-a-file\" succeeded\n");
        exit (1);
    }
    if (errno != ENOTDIR)
    {
        printf ("chdir to \"another-dir/../and-a-file\" didn't set correct error\n");
        result = 1;
    }

    /* We now should have a directory and a file in the new directory.  */
    rewinddir (dir2);
    while (readdir64_r (dir2, &direntbuf, &d) == 0 && d != NULL)
    {
        if (strcmp (d->d_name, ".") == 0
                || strcmp (d->d_name, "..") == 0
                || strcmp (d->d_name, "another-dir") == 0)
        {
#ifdef _DIRENT_HAVE_D_TYPE
            if (d->d_type != DT_UNKNOWN && d->d_type != DT_DIR)
            {
                printf ("d_type for \"%s\" is wrong\n", d->d_name);
                result = 1;
            }
#endif
            if (stat64 (d->d_name, &st3) < 0)
            {
                printf ("cannot stat \"%s\" is wrong\n", d->d_name);
                result = 1;
            }
            else if (! S_ISDIR (st3.st_mode))
            {
                printf ("\"%s\" is no directory\n", d->d_name);
                result = 1;
            }
        }
        else if (strcmp (d->d_name, "and-a-file") == 0)
        {
#ifdef _DIRENT_HAVE_D_TYPE
            if (d->d_type != DT_UNKNOWN && d->d_type != DT_REG)
            {
                printf ("d_type for \"%s\" is wrong\n", d->d_name);
                result = 1;
            }
#endif
            if (stat64 (d->d_name, &st3) < 0)
            {
                printf ("cannot stat \"%s\" is wrong\n", d->d_name);
                result = 1;
            }
            else if (! S_ISREG (st3.st_mode))
            {
                printf ("\"%s\" is no regular file\n", d->d_name);
                result = 1;
            }
        }
        else
        {
            printf ("unexpected directory entry \"%s\"\n", d->d_name);
            result = 1;
        }
    }

    if (stat64 ("does-not-exist", &st1) >= 0)
    {
        puts ("stat for unexisting file did not fail");
        result = 1;
    }

    /* Free all resources.  */

    if (closedir (dir1) < 0)
    {
        printf ("closing dir1 failed: %m\n");
        result = 1;
    }
    if (closedir (dir2) < 0)
    {
        printf ("second closing dir2 failed: %m\n");
        result = 1;
    }

    if (rmdir ("another-dir") < 0)
    {
        printf ("cannot remove \"another-dir\": %m\n");
        result = 1;
    }

    if (unlink ("and-a-file") < 0)
    {
        printf ("cannot remove \"and-a-file\": %m\n");
        result = 1;
    }

    /* One more test before we leave: mkdir() is supposed to fail with
       EEXIST if the named file is a symlink.  */
    if (symlink ("a-symlink", "a-symlink") != 0)
    {
        printf ("cannot create symlink \"a-symlink\": %m\n");
        result = 1;
    }
    else
    {
        if (mkdir ("a-symlink", 0666) == 0)
        {
            puts ("can make directory \"a-symlink\"");
            result = 1;
        }
        else if (errno != EEXIST)
        {
            puts ("mkdir(\"a-symlink\") does not fail with EEXIST\n");
            result = 1;
        }
        if (unlink ("a-symlink") < 0)
        {
            printf ("cannot unlink \"a-symlink\": %m\n");
            result = 1;
        }
    }

    if (chdir (srcdir) < 0)
    {
        printf ("cannot change back to source directory: %m\n");
        exit (1);
    }

    if (rmdir (buf) < 0)
    {
        printf ("cannot remove \"%s\": %m\n", buf);
        result = 1;
    }
    free (objdir_copy1);
    free (objdir_copy2);

    if (result == 0)
        puts ("all OK");

    return result;
}