Example #1
0
Cell *gsub(Node **a, int nnn)	/* global substitute */
{
	Cell *x, *y;
	char *rptr, *sptr, *t, *pb, *q;
	char *buf;
	fa *pfa;
	int mflag, tempstat, num;
	int bufsz = recsize;

	if ((buf = (char *) malloc(bufsz)) == NULL)
		FATAL("out of memory in gsub");
	mflag = 0;	/* if mflag == 0, can replace empty string */
	num = 0;
	x = execute(a[3]);	/* target string */
	t = getsval(x);
	if (a[0] == 0)		/* 0 => a[1] is already-compiled regexpr */
		pfa = (fa *) a[1];	/* regular expression */
	else {
		y = execute(a[1]);
		pfa = makedfa(getsval(y), 1);
		tempfree(y);
	}
	y = execute(a[2]);	/* replacement string */
	if (pmatch(pfa, t)) {
		tempstat = pfa->initstat;
		pfa->initstat = 2;
		pb = buf;
		rptr = getsval(y);
		do {
			if (patlen == 0 && *patbeg != 0) {	/* matched empty string */
				if (mflag == 0) {	/* can replace empty */
					num++;
					sptr = rptr;
					while (*sptr != 0) {
						adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
						if (*sptr == '\\') {
							backsub(&pb, &sptr);
						} else if (*sptr == '&') {
							sptr++;
							adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
							for (q = patbeg; q < patbeg+patlen; )
								*pb++ = *q++;
						} else
							*pb++ = *sptr++;
					}
				}
				if (*t == 0)	/* at end */
					goto done;
				adjbuf(&buf, &bufsz, 2+pb-buf, recsize, &pb, "gsub");
				*pb++ = *t++;
				if (pb > buf + bufsz)	/* BUG: not sure of this test */
					FATAL("gsub result0 %.30s too big; can't happen", buf);
				mflag = 0;
			}
			else {	/* matched nonempty string */
				num++;
				sptr = t;
				adjbuf(&buf, &bufsz, 1+(patbeg-sptr)+pb-buf, recsize, &pb, "gsub");
				while (sptr < patbeg)
					*pb++ = *sptr++;
				sptr = rptr;
				while (*sptr != 0) {
					adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
					if (*sptr == '\\') {
						backsub(&pb, &sptr);
					} else if (*sptr == '&') {
						sptr++;
						adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
						for (q = patbeg; q < patbeg+patlen; )
							*pb++ = *q++;
					} else
						*pb++ = *sptr++;
				}
				t = patbeg + patlen;
				if (patlen == 0 || *t == 0 || *(t-1) == 0)
					goto done;
				if (pb > buf + bufsz)
					FATAL("gsub result1 %.30s too big; can't happen", buf);
				mflag = 1;
			}
		} while (pmatch(pfa,t));
		sptr = t;
		adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "gsub");
		while ((*pb++ = *sptr++) != 0)
			;
	done:	if (pb < buf + bufsz)
			*pb = '\0';
		else if (*(pb-1) != '\0')
			FATAL("gsub result2 %.30s truncated; can't happen", buf);
		setsval(x, buf);	/* BUG: should be able to avoid copy + free */
		pfa->initstat = tempstat;
	}
	tempfree(x);
	tempfree(y);
	x = gettemp();
	x->tval = NUM;
	x->fval = num;
	free(buf);
	return(x);
}
Example #2
0
STATIC int
pmatch(char *pattern, char *string, int squoted)
{
	char *p, *q;
	char c;

	p = pattern;
	q = string;
	for (;;) {
		switch (c = *p++) {
		case '\0':
			goto breakloop;
		case CTLESC:
			if (squoted && *q == CTLESC)
				q++;
			if (*q++ != *p++)
				return 0;
			break;
		case CTLQUOTEMARK:
			continue;
		case '?':
			if (squoted && *q == CTLESC)
				q++;
			if (*q++ == '\0')
				return 0;
			break;
		case '*':
			c = *p;
			while (c == CTLQUOTEMARK || c == '*')
				c = *++p;
			if (c != CTLESC &&  c != CTLQUOTEMARK &&
			    c != '?' && c != '*' && c != '[') {
				while (*q != c) {
					if (squoted && *q == CTLESC &&
					    q[1] == c)
						break;
					if (*q == '\0')
						return 0;
					if (squoted && *q == CTLESC)
						q++;
					q++;
				}
			}
			do {
				if (pmatch(p, q, squoted))
					return 1;
				if (squoted && *q == CTLESC)
					q++;
			} while (*q++ != '\0');
			return 0;
		case '[': {
			char *endp;
			int invert, found;
			char chr;

			endp = p;
			if (*endp == '!')
				endp++;
			for (;;) {
				while (*endp == CTLQUOTEMARK)
					endp++;
				if (*endp == '\0')
					goto dft;		/* no matching ] */
				if (*endp == CTLESC)
					endp++;
				if (*++endp == ']')
					break;
			}
			invert = 0;
			if (*p == '!') {
				invert++;
				p++;
			}
			found = 0;
			chr = *q++;
			if (squoted && chr == CTLESC)
				chr = *q++;
			if (chr == '\0')
				return 0;
			c = *p++;
			do {
				if (c == CTLQUOTEMARK)
					continue;
				if (c == CTLESC)
					c = *p++;
				if (*p == '-' && p[1] != ']') {
					p++;
					while (*p == CTLQUOTEMARK)
						p++;
					if (*p == CTLESC)
						p++;
					if (chr >= c && chr <= *p)
						found = 1;
					p++;
				} else {
					if (chr == c)
						found = 1;
				}
			} while ((c = *p++) != ']');
			if (found == invert)
				return 0;
			break;
		}
dft:	        default:
			if (squoted && *q == CTLESC)
				q++;
			if (*q++ != c)
				return 0;
			break;
		}
	}
breakloop:
	if (*q != '\0')
		return 0;
	return 1;
}
bool edge_match( vec2f ea, vec2f eb,
				 vec2f a, vec2f b, vec2f c )
{
	printf(" Check edge %f %f -> %f %f\n", ea.x, ea.y, eb.x, eb.y );
	// check edge AB
	if ( (pmatch(ea, a) && pmatch(eb, b)) ||
		 (pmatch(eb, a) && pmatch(ea, b)) ) {
			 printf("AB: match edge %f %f --> %f %f\n", a.x, a.y, b.x, b.y );
			 return true;
		 }

	// check edge BC
	if ( (pmatch(ea, c) && pmatch(eb, b)) ||
		(pmatch(eb, c) && pmatch(ea, b)) ) {
			printf("BC: match edge %f %f\n", b.x, b.y, c.x, c.y );
			return true;
		}

	// check edge CA
	if ( (pmatch(ea, a) && pmatch(eb, c)) ||
		(pmatch(eb, a) && pmatch(ea, c)) )  {
		printf("CA: match edge %f %f\n", a.x, a.y, c.x, c.y );
		 return true;
	}
	return false;
}
char * RegEx::pmatch(char * line, char *pattern)
/* char *line,     ..(partial) line to match.. */
/* *pattern;   ..(partial) pattern to match.. */
{
   register char   *l;        /* ..Current line pointer.. */
   register char   *p;        /* ..Current pattern pointer.. */
   register char   c;         /* ..Current character.. */
   register char   d;         /* ..Temporary character.. */
   char            *e;        /* ..End for STAR and PLUS match.. */
   int             op;        /* ..Pattern operation.. */
   int             n;         /* ..Class counter.. */
   char            *are;      /* ..Start of STAR match.. */
   
   l = line;
   p = pattern;
   while ((op = *p++) != ENDPAT)
   {
      switch(op)
      {
         case CHARAC:
         c = iflag ? tolower(*l++) : *l++;
         d = iflag ? tolower(*p++) : *p++;
         if (c != d)
         return(0);
         break;
         case BOL:
         if (l != lbuf)
         return(0);
         break;
         case EOL:
         if (*l != '\0' && *l != '\n')
         return(0);
         break;
         case ANY:
         if (*l++ == '\0')
         return(0);
         break;
         case DIGIT:
         if (!isdigit(c = *l++))
         return(0);
         break;
         case ALPHA:
         if (!isalpha(c = *l++))
         return(0);
         break;
         case NALPHA:
         if (!isalnum(c = *l++))
         return(0);
         break;
         case WHITE:
         if (!isspace(c = *l++))
         return(0);
         break;
         case CLASS:
         case NCLASS:
         c = iflag ? tolower(*l++) : *l++;
         n = *p++ & 0x00ff;
         do
         {
            if (*p == RANGE)
            {
               p += 3;
               n -= 2;
               if (c >= p[-2] && c <= p[-1])
               break;
            }
            else if (c == *p++)
            break;
         } while (--n > 1);
         if ((op == CLASS) == (n <= 1))
         return(0);
         if (op == CLASS)
         p += n - 2;
         break;
         case MINUS:
         e = pmatch(l, p);       /* ..Look for a match.. */
         while (*p++ != ENDPAT); /* ..Skip over pattern.. */
         if (e)                  /* ..Got a match?.. */
         l = e;               /* ..Yes, update string.. */
         break;                  /* ..Always succeeds.. */
         case PLUS:                 /* ..One or more.. */
         if ((l = pmatch(l, p)) == 0)
         return(0);           /* ..Gotta have a match.. */
         case STAR:                 /* ..Zero or more.. */
         are = l;                /* ..Remember line start.. */
         while (*l && (e = pmatch(l, p)))
         l = e;               /* ..Get longest match.. */
         while (*p++ != ENDPAT); /* ..Skip over pattern.. */
         while (l >= are)
         {  /* ..Try to match rest.. */
            e = pmatch(l, p);
            if (e)
            return(e);
            --l;                 /* ..Nope, try earlier.. */
         }
         return(0);              /* ..Nothing else worked.. */
         default:
         cerr << "Bad op code " << op << endl;
         cerr << "Cannot happen -- match" << endl;
      }
   }
   return(l);
}
Example #5
0
HXBOOL CMacFindFile::OS_FileMatchesPattern (const char * fname)
{
    return pmatch(m_pattern, fname);
}
Example #6
0
int CMacFindFile::pmatch(const char* pattern, const char* string)
{
    const char *p, *q;
    char c;

    p = pattern;
    q = string;
    for (;;) {
	switch (c = *p++) {
	case '\0':
	    goto breakloop;
	case '?':
	    if (*q++ == '\0')
		return 0;
	break;
	case '*':
	    c = *p;
	    if (c != '?' && c != '*' && c != '[') {
		while (*q != c) {
		    if (*q == '\0')
			return 0;
		    q++;
		}
	    }
	    do {
		if (pmatch(p, q))
		    return 1;
	    } while (*q++ != '\0');
	return 0;
	case '[': {
		const char *endp;
		int invert, found;
		char chr;

		endp = p;
		if (*endp == '!')
			endp++;
		for (;;) {
			if (*endp == '\0')
				goto dft;		/* no matching ] */
			if (*++endp == ']')
				break;
		}
		invert = 0;
		if (*p == '!') {
			invert++;
			p++;
		}
		found = 0;
		chr = *q++;
		if (chr == '\0')
			return 0;
		c = *p++;
		do {
		    if (*p == '-' && p[1] != ']') {
			p++;
#if 0
			if (   collate_range_cmp(chr, c) >= 0
			    && collate_range_cmp(chr, *p) <= 0
			   )
			    found = 1;
#endif
			p++;
		    } else {
			if (chr == c)
			    found = 1;
		    }
		} while ((c = *p++) != ']');
		if (found == invert)
		    return 0;
		break;
	    }
	    dft:
	    default:
		if (*q++ != c)
		    return 0;
	    break;
	}
    }
breakloop:
    if (*q != '\0')
	return 0;
    return 1;
}
Example #7
0
			// apply pattern against node name
			bool match(const std::string& pattern) const
			{
				return pmatch(this->name, pattern);
			}
Example #8
0
/*######################## get_full_dc_names() ##########################*/
void
get_full_dc_names(char *dc_filter, off_t *db_size)
{
   char fullname[MAX_PATH_LENGTH],
        *work_ptr;

   (void)strcpy(fullname, dc_filter);
   work_ptr = fullname + strlen(fullname);
   while ((*work_ptr != '/') && (work_ptr != fullname))
   {
      work_ptr--;
   }
   if (*work_ptr == '/')
   {
      char          filter[MAX_FILENAME_LENGTH + 1];
      struct stat   stat_buf;
      DIR           *dp;
      struct dirent *p_dir;

      work_ptr++;
      (void)strcpy(filter, work_ptr);
      *work_ptr = '\0';

      if ((dp = opendir(fullname)) == NULL)
      {
         system_log(ERROR_SIGN, __FILE__, __LINE__,
                    _("Failed to opendir() `%s' : %s"),
                    fullname, strerror(errno));
         return;
      }

      while ((p_dir = readdir(dp)) != NULL)
      {
         if (p_dir->d_name[0] == '.')
         {
            errno = 0;
            continue;
         }

         (void)strcpy(work_ptr, p_dir->d_name);
         if (stat(fullname, &stat_buf) < 0)
         {
            if (errno != ENOENT)
            {
               system_log(WARN_SIGN, __FILE__, __LINE__,
                          _("Failed to stat() `%s' : %s"),
                          fullname, strerror(errno));
            }
            errno = 0;
            continue;
         }

         /* Sure it is a normal file? */
         if (S_ISREG(stat_buf.st_mode))
         {
            if (pmatch(filter, p_dir->d_name, NULL) == 0)
            {
               int length;

               /* Check space for dc_dcl structure. */
               if ((no_of_dir_configs % DIR_CONFIG_NAME_STEP_SIZE) == 0)
               {
                  size_t new_size;

                  new_size = ((no_of_dir_configs / DIR_CONFIG_NAME_STEP_SIZE) + 1) * DIR_CONFIG_NAME_STEP_SIZE * sizeof(struct dir_config_buf);

                  if ((dc_dcl = realloc(dc_dcl, new_size)) == NULL)
                  {
                     system_log(FATAL_SIGN, __FILE__, __LINE__,
                                "Could not realloc() memory : %s",
                                strerror(errno));
                     exit(INCORRECT);
                  }
               }

               length = strlen(fullname) + 1;
               if ((dc_dcl[no_of_dir_configs].dir_config_file = malloc(length)) == NULL)
               {
                  system_log(FATAL_SIGN, __FILE__, __LINE__,
                             "Could not malloc() %d bytes : %s",
                             length, strerror(errno));
                  exit(INCORRECT);
               }
               (void)memcpy(dc_dcl[no_of_dir_configs].dir_config_file,
                            fullname, length);
               dc_dcl[no_of_dir_configs].dc_old_time = stat_buf.st_mtime;
               dc_dcl[no_of_dir_configs].is_filter = YES;
               no_of_dir_configs++;
               *db_size += stat_buf.st_size;
            }
         }
      }

      /* When using readdir() it can happen that it always returns     */
      /* NULL, due to some error. We want to know if this is the case. */
      if (errno == EBADF)
      {
         system_log(WARN_SIGN, __FILE__, __LINE__,
                    _("Failed to readdir() `%s' : %s"), fullname, strerror(errno));
      }

      /* Don't forget to close the directory. */
      if (closedir(dp) == -1)
      {
         *work_ptr = '\0';
         system_log(ERROR_SIGN, __FILE__, __LINE__,
                    _("Failed  to closedir() `%s' : %s"),
                    fullname, strerror(errno));
      }
   }

   return;
}
Example #9
0
char *CRegEx::pmatch (char *begin, char *line, char *pattern)
//*************************************************************************
//
//
//
//*************************************************************************
{
  char *l = line;           // Current line pointer
  char *p = pattern;        // Current pattern pointer
  char c;                   // Current character
  char *e;                  // End for STAR and PLUS match
  int op;                   // Pattern operation
  int n;                    // Class counter
  char *are;                // Start of STAR match

  while ((op = *p++) != ENDPAT)
  {
    c = l[0];
    switch (op)
    {
    case CHAR:
      if (c != *p++) goto notmatch;
      l++;
      break;
    case BOL:
      if (begin != l) goto notmatch;
      break;
    case EOL:
      if (c != LF) goto notmatch;
      break;
    case ANY:
      l++;
      if (c == LF) goto notmatch;
      break;
    case CLASS:
      l++;
      n = *p++;
      do
      {
        if (p[0] == RANGE)
        {
          p += 3;
          n -= 2;
          if (c >= p[-2] && c <= p[-1]) break;
        }
        else if (c == *p++) break;
      }
      while (--n > 1);
      if (n <= 1) goto notmatch;
      p += n - 2;
      break;
    case NCLASS:
      l++;
      n = *p++;
      do
      {
        if (p[0] == RANGE)
        {
          n -= 2;
          if (c >= p[1] && c <= p[2]) goto notmatch;
          p += 3;
        }
        else if (c == *p++) goto notmatch;
      }
      while (--n > 1);
      break;
    case PLUS:            // One or more ...
      l = pmatch(begin, l, p);
      if (! l) goto notmatch;    // Gotta have a match
    case STAR:            // Zero or more ...
      are = l;            // Remember line start
      while (l[0] && (e = pmatch(begin, l, p)) != NULL)
        l = e;            // Get longest match
      while (*p++ != ENDPAT);   // Skip over pattern
      while (l >= are)    // Try to match rest
      {
        e = pmatch(begin, l, p);
        if (e) return e;
        --l;              // Nope, try earlier
      }
      goto notmatch;      // Nothing else worked
    }
  }
  return l;

notmatch:
  return NULL;
}
Example #10
0
/*########################### link_files() ##############################*/
int
link_files(char                   *src_file_path,
           char                   *dest_file_path,
           int                    dest_file_path_length,
           time_t                 current_time,
#ifdef _WITH_PTHREAD
           off_t                  *file_size_pool,
           time_t                 *file_mtime_pool,
           char                   **file_name_pool,
           unsigned char          *file_length_pool,
#endif
#if defined (_MAINTAINER_LOG) && defined (SHOW_FILE_MOVING)
           char                   *caller,
           int                    line,
#endif
           struct directory_entry *p_de,
           struct instant_db      *p_db,
           unsigned int           *split_job_counter,
           int                    unique_number,
           int                    pos_in_fm,
           int                    no_of_files,
           char                   *unique_name, /* Storage to return unique name. */
           off_t                  *file_size_linked)
{
   int          files_linked = 0,
#ifdef _DISTRIBUTION_LOG
                dist_type,
#endif
                retstat;
   register int i,
                j;
   time_t       pmatch_time;
   char         *p_src,
                *p_dest = NULL,
                *p_dest_end = NULL;

   *file_size_linked = 0;
   p_src = src_file_path + strlen(src_file_path);

   for (i = 0; i < no_of_files; i++)
   {
      for (j = 0; j < p_de->fme[pos_in_fm].nfm; j++)
      {
         if (p_de->paused_dir == NULL)
         {
            pmatch_time = current_time;
         }
         else
         {
            pmatch_time = file_mtime_pool[i];
         }
         if ((retstat = pmatch(p_de->fme[pos_in_fm].file_mask[j],
                               file_name_pool[i], &pmatch_time)) == 0)
         {
            time_t diff_time = current_time - file_mtime_pool[i];

            if (diff_time < 0)
            {
               diff_time = 0;
            }
            if ((p_db->age_limit > 0) &&
                ((fsa[p_db->position].host_status & DO_NOT_DELETE_DATA) == 0) &&
                (diff_time > p_db->age_limit))
            {
#ifdef _DELETE_LOG
               size_t dl_real_size;

               (void)memcpy(dl.file_name, file_name_pool[i],
                            (size_t)(file_length_pool[i] + 1));
               (void)snprintf(dl.host_name,
                              MAX_HOSTNAME_LENGTH + 4 + 1,
                              "%-*s %03x",
                              MAX_HOSTNAME_LENGTH, p_db->host_alias, AGE_INPUT);
               *dl.file_size = file_size_pool[i];
               *dl.dir_id = p_de->dir_id;
               *dl.job_id = p_db->job_id;
               *dl.input_time = 0L;
               *dl.split_job_counter = 0;
               *dl.unique_number = 0;
               *dl.file_name_length = file_length_pool[i];
               dl_real_size = *dl.file_name_length + dl.size +
                              snprintf((dl.file_name + *dl.file_name_length + 1),
                                        MAX_FILENAME_LENGTH + 1,
# if SIZEOF_TIME_T == 4
                                       "%s%c>%ld (%s %d)",
# else
                                       "%s%c>%lld (%s %d)",
# endif
                                       DIR_CHECK, SEPARATOR_CHAR,
                                       (pri_time_t)diff_time,
                                       __FILE__, __LINE__);
               if (write(dl.fd, dl.data, dl_real_size) != dl_real_size)
               {
                  system_log(ERROR_SIGN, __FILE__, __LINE__,
                             "write() error : %s", strerror(errno));
               }
#endif
               if (p_de->flag & RENAME_ONE_JOB_ONLY)
               {
                  (void)memcpy(p_src, file_name_pool[i],
                               (size_t)(file_length_pool[i] + 1));
                  if (unlink(src_file_path) == -1)
                  {
                     system_log(WARN_SIGN, __FILE__, __LINE__,
                                "Failed to unlink() file `%s' : %s",
                                src_file_path, strerror(errno));
                  }
               }
#ifdef _DISTRIBUTION_LOG
               dist_type = AGE_LIMIT_DELETE_DIS_TYPE;
#endif
            }
            else
            {
               /* Only create a unique name and the corresponding */
               /* directory when we have found a file that is to  */
               /* be distributed.                                 */
               if (p_dest == NULL)
               {
                  if (p_db->loptions != NULL)
                  {
                     /* Create a new message name and directory. */
                     if (create_name(dest_file_path, dest_file_path_length,
                                     p_db->priority,
                                     current_time, p_db->job_id,
                                     split_job_counter, &unique_number,
                                     unique_name, MAX_FILENAME_LENGTH - 1, -1) < 0)
                     {
                        if (errno == ENOSPC)
                        {
                           system_log(ERROR_SIGN, __FILE__, __LINE__,
                                      "DISK FULL!!! Will retry in %d second interval.",
                                      DISK_FULL_RESCAN_TIME);

                           while (errno == ENOSPC)
                           {
                              (void)sleep(DISK_FULL_RESCAN_TIME);
                              errno = 0;
                              if (create_name(dest_file_path,
                                              dest_file_path_length,
                                              p_db->priority,
                                              current_time, p_db->job_id,
                                              split_job_counter, &unique_number,
                                              unique_name,
                                              MAX_FILENAME_LENGTH - 1, -1) < 0)
                              {
                                 if (errno != ENOSPC)
                                 {
                                    system_log(FATAL_SIGN, __FILE__, __LINE__,
                                               "Failed to create a unique name : %s",
                                               strerror(errno));
                                    exit(INCORRECT);
                                 }
                              }
                           }

                           system_log(INFO_SIGN, __FILE__, __LINE__,
                                      "Continuing after disk was full.");
                        }
                        else
                        {
                           system_log(FATAL_SIGN, __FILE__, __LINE__,
                                      "Failed to create a unique name : %s",
                                      strerror(errno));
                           exit(INCORRECT);
                        }
                     }
                     p_dest_end = dest_file_path + strlen(dest_file_path);
                     if (*(p_dest_end - 1) != '/')
                     {
                        *p_dest_end = '/';
                        p_dest_end++;
                     }
                     (void)strcpy(p_dest_end, unique_name);
                     p_dest = p_dest_end + strlen(unique_name);
                     *(p_dest++) = '/';
                     *p_dest = '\0';
                  }
                  else
                  {
                     int dir_no;

                     if ((dir_no = get_dir_number(dest_file_path, p_db->job_id,
                                                  NULL)) == INCORRECT)
                     {
                        if (p_dest_end != NULL)
                        {
                           *p_dest_end = '\0';
                        }
                        if (p_src != NULL)
                        {
                           *p_src = '\0';
                        }
                        return(INCORRECT);
                     }
                     p_dest_end = dest_file_path + strlen(dest_file_path);
                     if (*(p_dest_end - 1) == '/')
                     {
                        p_dest_end--;
                     }
                     (void)snprintf(unique_name, MAX_FILENAME_LENGTH - 1,
#if SIZEOF_TIME_T == 4
                                    "%x/%x/%lx_%x_%x",
#else
                                    "%x/%x/%llx_%x_%x",
#endif
                                    p_db->job_id, dir_no,
                                    (pri_time_t)current_time,
                                    unique_number, *split_job_counter);
                     p_dest = p_dest_end +
                              snprintf(p_dest_end,
                                       MAX_PATH_LENGTH - (dest_file_path - p_dest_end),
                                       "/%s/", unique_name);
                     if (mkdir(dest_file_path, DIR_MODE) == -1)
                     {
                        system_log(ERROR_SIGN, __FILE__, __LINE__,
                                   "Failed to create directory %s : %s",
                                   dest_file_path, strerror(errno));
                        if (p_dest_end != NULL)
                        {
                           *p_dest_end = '\0';
                        }
                        if (p_src != NULL)
                        {
                           *p_src = '\0';
                        }
                        return(INCORRECT);
                     }
                  }
               }

               (void)memcpy(p_src, file_name_pool[i],
                            (size_t)(file_length_pool[i] + 1));
               (void)memcpy(p_dest, file_name_pool[i],
                            (size_t)(file_length_pool[i] + 1));
#if defined (_MAINTAINER_LOG) && defined (SHOW_FILE_MOVING)
               maintainer_log(DEBUG_SIGN, NULL, 0,
                              "link_files() [%s %d]: `%s' -> `%s'",
                              caller, line, src_file_path, dest_file_path);
#endif
               /* Rename/link/copy the file. */
               if (p_de->flag & RENAME_ONE_JOB_ONLY)
               {
                  if ((retstat = rename(src_file_path, dest_file_path)) == -1)
                  {
                     int gotcha = NO;

                     /*
                      * It can happen that when we copied/renamed the file
                      * from its source directory into our internal pool
                      * directory, that we have copied/renamed the same
                      * file twice, overwritting one. But in our array
                      * file_name_pool[] we still have both listed. Search
                      * through the array and see if this is the case, then
                      * we can savely ignore this error message.
                      */
                     if (i > 0)
                     {
                        int k;

                        for (k = 0; k < i; k++)
                        {
                           if (CHECK_STRCMP(file_name_pool[i],
                                            file_name_pool[k]) == 0)
                           {
                              system_log(DEBUG_SIGN, NULL, 0,
                                         "File %s has been picked up more then once while scanning input directory %s [%s %x]\n",
                                         file_name_pool[i], p_de->dir,
                                         p_de->alias, p_de->dir_id);
                              gotcha = YES;
                              break;
                           }
                        }
                     }
                     if (gotcha == NO)
                     {
                        system_log(WARN_SIGN, __FILE__, __LINE__,
                                   "Failed to rename() file %s to %s : %s",
                                   src_file_path, dest_file_path,
                                   strerror(errno));
                        if (errno == ENOENT)
                        {
                           char whats_gone[40];

                           whats_gone[0] = '\0';
                           if (eaccess(src_file_path, R_OK) != 0)
                           {
                              (void)strcat(whats_gone, "src file");
                           }
                           if (eaccess(dest_file_path, R_OK) != 0)
                           {
                              (void)strcat(whats_gone, ", dst file");
                           }
                           *p_src = '\0';
                           *p_dest = '\0';
                           if (eaccess(src_file_path, R_OK) != 0)
                           {
                              (void)strcat(whats_gone, ", src dir");
                           }
                           if (eaccess(dest_file_path, R_OK) != 0)
                           {
                              (void)strcat(whats_gone, ", dst dir");
                           }
                           system_log(DEBUG_SIGN, NULL, 0, "%s is not there",
                                      whats_gone);
                        }
                     }
                  }
               }
#ifdef LINUX
               else if ((p_db->lfs & DO_NOT_LINK_FILES) ||
                        ((hardlinks_protected == YES) &&
                         (access(src_file_path, W_OK) != 0)))
#else
               else if (p_db->lfs & DO_NOT_LINK_FILES)
#endif
                    {
#ifdef LINUX
try_copy_file:
#endif
                       if ((retstat = copy_file(src_file_path, dest_file_path,
                                                NULL)) < 0)
                       {
                          system_log(WARN_SIGN, __FILE__, __LINE__,
                                     "Failed to copy file %s to %s",
                                     src_file_path, dest_file_path);
                       }
                    }
                    else /* Just link() the files. */
                    {
                       if ((retstat = link(src_file_path, dest_file_path)) == -1)
                       {
                          if (errno == ENOSPC)
                          {
                             system_log(ERROR_SIGN, __FILE__, __LINE__,
                                        "DISK FULL!!! Will retry in %d second interval.",
                                        DISK_FULL_RESCAN_TIME);

                             while (errno == ENOSPC)
                             {
                                (void)sleep(DISK_FULL_RESCAN_TIME);
                                errno = 0;
                                if (link(src_file_path, dest_file_path) < 0)
                                {
                                   if (errno != ENOSPC)
                                   {
                                      system_log(WARN_SIGN, __FILE__, __LINE__,
                                                 "Failed to link file %s to %s : %s",
                                                 src_file_path, dest_file_path,
                                                 strerror(errno));
                                      break;
                                   }
                                }
                             }

                             system_log(INFO_SIGN, __FILE__, __LINE__,
                                        "Continuing after disk was full.");
                          }
                          else
                          {
                             int    tmp_errno = errno;
#ifdef _DELETE_LOG
                             size_t dl_real_size;
#endif

#ifdef LINUX
                             if ((tmp_errno == EPERM) &&
                                 (hardlinks_protected == NEITHER))
                             {
                                system_log(WARN_SIGN, __FILE__, __LINE__,
                                           "Hardlinks are protected! You need to unset this in /proc/sys/fs/protected_hardlinks. Otherwise AFD must copy files!");
                                hardlinks_protected = YES;

                                goto try_copy_file;
                             }
#endif

                             system_log(ERROR_SIGN, __FILE__, __LINE__,
                                        "Failed to link file %s to %s : %s",
                                        src_file_path, dest_file_path,
                                        strerror(tmp_errno));

#ifdef _DELETE_LOG
                             (void)memcpy(dl.file_name, file_name_pool[i],
                                          (size_t)(file_length_pool[i] + 1));
                             (void)snprintf(dl.host_name,
                                            MAX_HOSTNAME_LENGTH + 4 + 1,
                                            "%-*s %03x",
                                            MAX_HOSTNAME_LENGTH,
                                            p_db->host_alias,
                                            INTERNAL_LINK_FAILED);
                             *dl.file_size = file_size_pool[i];
                             *dl.dir_id = p_de->dir_id;
                             *dl.job_id = p_db->job_id;
                             *dl.input_time = 0L;
                             *dl.split_job_counter = 0;
                             *dl.unique_number = 0;
                             *dl.file_name_length = file_length_pool[i];
                             dl_real_size = *dl.file_name_length + dl.size +
                                            snprintf((dl.file_name + *dl.file_name_length + 1),
                                                     MAX_FILENAME_LENGTH + 1,
                                                     "%s%c>%s (%s %d)",
                                                     DIR_CHECK, SEPARATOR_CHAR,
                                                     strerror(tmp_errno),
                                                     __FILE__, __LINE__);
                             if (write(dl.fd, dl.data, dl_real_size) != dl_real_size)
                             {
                                system_log(ERROR_SIGN, __FILE__, __LINE__,
                                           "write() error : %s", strerror(errno));
                             }
#endif
                          }
                       }
                    }

               if (retstat != -1)
               {
#ifndef _WITH_PTHREAD
                  if ((files_linked % FILE_NAME_STEP_SIZE) == 0)
                  {
                     size_t new_size;

                     /* Calculate new size of file name buffer. */
                     new_size = ((files_linked / FILE_NAME_STEP_SIZE) + 1) * FILE_NAME_STEP_SIZE * MAX_FILENAME_LENGTH;

                     /* Increase the space for the file name buffer. */
                     if ((file_name_buffer = realloc(file_name_buffer, new_size)) == NULL)
                     {
                        system_log(FATAL_SIGN, __FILE__, __LINE__,
                                   "Could not realloc() memory : %s",
                                   strerror(errno));
                        exit(INCORRECT);
                     }

                     /* Calculate new size of file size buffer. */
                     new_size = ((files_linked / FILE_NAME_STEP_SIZE) + 1) * FILE_NAME_STEP_SIZE * sizeof(off_t);

                     /* Increase the space for the file size buffer. */
                     if ((file_size_buffer = realloc(file_size_buffer, new_size)) == NULL)
                     {
                        system_log(FATAL_SIGN, __FILE__, __LINE__,
                                   "Could not realloc() memory : %s",
                                   strerror(errno));
                        exit(INCORRECT);
                     }
                  }
                  (void)memcpy((file_name_buffer + (files_linked * MAX_FILENAME_LENGTH)),
                               file_name_pool[i],
                               (size_t)(file_length_pool[i] + 1));
                  file_size_buffer[files_linked] = file_size_pool[i];
#endif
                  files_linked++;
                  *file_size_linked += file_size_pool[i];
#ifdef _DISTRIBUTION_LOG
                  dist_type = NORMAL_DIS_TYPE;
#endif
               }
#ifdef _DISTRIBUTION_LOG
               else
               {
                  dist_type = ERROR_DIS_TYPE;
               }
#endif
            }
#ifdef _DISTRIBUTION_LOG
            if ((dist_type < NO_OF_DISTRIBUTION_TYPES) &&
                (file_dist_pool[i][dist_type].no_of_dist < max_jobs_per_file))
            {
               file_dist_pool[i][dist_type].jid_list[file_dist_pool[i][dist_type].no_of_dist] = p_db->job_id;
               file_dist_pool[i][dist_type].proc_cycles[file_dist_pool[i][dist_type].no_of_dist] = (unsigned char)(p_db->no_of_loptions - p_db->no_of_time_entries);
               file_dist_pool[i][dist_type].no_of_dist++;
            }
#endif

            /*
             * Since the file is already in the file directory
             * no need to test any further filters.
             */
            break;
         }
         else if (retstat == 1)
              {
                 /*
                  * This file is definitely NOT wanted, no matter what the
                  * following filters say.
                  */
                 break;
              }
      } /* for (j = 0; j < p_de->fme[pos_in_fm].nfm; j++) */
Example #11
0
/*######################### search_old_files() ##########################*/
void
search_old_files(time_t current_time)
{
   int           i, j, k,
                 delete_file,
                 junk_files,
                 file_counter,
                 queued_files,
#ifdef _DELETE_LOG
                 reason,
#endif
                 ret;
   time_t        diff_time,
                 pmatch_time;
   char          *work_ptr,
                 tmp_dir[MAX_PATH_LENGTH];
   off_t         queued_size_deleted,
                 file_size;
   struct stat   stat_buf;
   DIR           *dp;
   struct dirent *p_dir;

   for (i = 0; i < no_of_local_dirs; i++)
   {
      if ((de[i].dir != NULL) &&
          ((fra[de[i].fra_pos].dir_flag & DIR_DISABLED) == 0))
      {
         (void)strcpy(tmp_dir, de[i].dir);

         if ((dp = opendir(tmp_dir)) == NULL)
         {
            if ((errno != ENOENT) && (errno != EACCES))
            {
               system_log(WARN_SIGN, __FILE__, __LINE__,
                          "Can't access directory %s : %s",
                          tmp_dir, strerror(errno));
            }
         }
         else
         {
            file_counter        = 0;
            file_size           = 0;
            junk_files          = 0;
            queued_files        = 0;
            queued_size_deleted = 0;

            work_ptr = tmp_dir + strlen(tmp_dir);
            *work_ptr++ = '/';
            *work_ptr = '\0';

            errno = 0;
            while ((p_dir = readdir(dp)) != NULL)
            {
               /* Ignore "." and "..". */
               if (((p_dir->d_name[0] == '.') && (p_dir->d_name[1] == '\0')) ||
                   ((p_dir->d_name[0] == '.') && (p_dir->d_name[1] == '.') &&
                   (p_dir->d_name[2] == '\0')))
               {
                  continue;
               }

               (void)strcpy(work_ptr, p_dir->d_name);
               if (stat(tmp_dir, &stat_buf) < 0)
               {
                  /*
                   * Since this is a very low priority function lets not
                   * always report when we fail to stat() a file. Maybe the
                   * the user wants to keep some files.
                   */
                  continue;
               }

               /* Sure it is a normal file? */
               if (S_ISREG(stat_buf.st_mode))
               {
                  /*
                   * Regardless of what the delete_files_flag is set, also
                   * delete old files that are of zero length or have a
                   * leading dot.
                   */
                  diff_time = current_time - stat_buf.st_mtime;
                  if (diff_time < 0)
                  {
                     diff_time = 0;
                  }
                  if (((p_dir->d_name[0] == '.') &&
                       (diff_time > 3600L) &&
                       ((fra[de[i].fra_pos].unknown_file_time == 0) ||
                        ((fra[de[i].fra_pos].delete_files_flag & OLD_LOCKED_FILES) &&
                         (diff_time > fra[de[i].fra_pos].locked_file_time)))) ||
                      ((diff_time > 5L) &&
                       (diff_time > fra[de[i].fra_pos].unknown_file_time)))
                  {
                     if ((fra[de[i].fra_pos].delete_files_flag & UNKNOWN_FILES) ||
                         (p_dir->d_name[0] == '.'))
                     {
                        if (p_dir->d_name[0] == '.')
                        {
                           if ((fra[de[i].fra_pos].delete_files_flag & OLD_LOCKED_FILES) &&
                                (diff_time > fra[de[i].fra_pos].locked_file_time))
                           {
                              delete_file = YES;
#ifdef _DELETE_LOG
                              reason = DEL_OLD_LOCKED_FILE;
#endif
                           }
                           else
                           {
                              delete_file = NO;
                           }
                        }
                        else
                        {
                           if (de[i].flag & ALL_FILES)
                           {
                              delete_file = NO;
                           }
                           else
                           {
                              delete_file = YES;
#ifdef _DELETE_LOG
                              reason = DEL_UNKNOWN_FILE;
#endif
                              if (de[i].paused_dir == NULL)
                              {
                                 pmatch_time = current_time;
                              }
                              else
                              {
                                 pmatch_time = stat_buf.st_mtime;
                              }
                              for (j = 0; j < de[i].nfg; j++)
                              {
                                 for (k = 0; ((j < de[i].nfg) && (k < de[i].fme[j].nfm)); k++)
                                 {
                                    if ((ret = pmatch(de[i].fme[j].file_mask[k],
                                                      p_dir->d_name,
                                                      &pmatch_time)) == 0)
                                    {
                                       delete_file = NO;
                                       j = de[i].nfg;
                                    }
                                    else if (ret == 1)
                                         {
                                            break;
                                         }
                                 }
                              }
                           }
                        }
                        if (delete_file == YES)
                        {
                           if (unlink(tmp_dir) == -1)
                           {
                              system_log(WARN_SIGN, __FILE__, __LINE__,
                                         "Failed to unlink() %s : %s",
                                         tmp_dir, strerror(errno));
                           }
                           else
                           {
#ifdef _DELETE_LOG
                              size_t dl_real_size;

                              (void)strcpy(dl.file_name, p_dir->d_name);
                              (void)snprintf(dl.host_name,
                                             MAX_HOSTNAME_LENGTH + 4 + 1,
                                             "%-*s %03x",
                                             MAX_HOSTNAME_LENGTH, "-", reason);
                              *dl.file_size = stat_buf.st_size;
                              *dl.dir_id = de[i].dir_id;
                              *dl.job_id = 0;
                              *dl.input_time = 0L;
                              *dl.split_job_counter = 0;
                              *dl.unique_number = 0;
                              *dl.file_name_length = strlen(p_dir->d_name);
                              dl_real_size = *dl.file_name_length + dl.size +
                                             snprintf((dl.file_name + *dl.file_name_length + 1),
                                                      MAX_FILENAME_LENGTH + 1,
# if SIZEOF_TIME_T == 4
                                                      "%s%c>%ld (%s %d)",
# else
                                                      "%s%c>%lld (%s %d)",
# endif
                                                      DIR_CHECK, SEPARATOR_CHAR,
                                                      (pri_time_t)diff_time,
                                                      __FILE__, __LINE__);
                              if (write(dl.fd, dl.data, dl_real_size) != dl_real_size)
                              {
                                 system_log(ERROR_SIGN, __FILE__, __LINE__,
                                            "write() error : %s",
                                            strerror(errno));
                              }
#endif
                              file_counter++;
                              file_size += stat_buf.st_size;

                              if ((fra[de[i].fra_pos].delete_files_flag & UNKNOWN_FILES) == 0)
                              {
                                 junk_files++;
                              }
                           }
                        }
                        else if (fra[de[i].fra_pos].report_unknown_files == YES)
                             {
                                if ((fra[de[i].fra_pos].dir_flag & DIR_STOPPED) == 0)
                                {
                                   file_counter++;
                                   file_size += stat_buf.st_size;
                                }
                             }
                     }
                     else if (fra[de[i].fra_pos].report_unknown_files == YES)
                          {
                             if ((fra[de[i].fra_pos].dir_flag & DIR_STOPPED) == 0)
                             {
                                file_counter++;
                                file_size += stat_buf.st_size;
                             }
                             delete_file = NO;
                          }
                          else
                          {
                             delete_file = NO;
                          }
                  }
                  else
                  {
                     delete_file = NO;
                  }
                  if ((delete_file == NO) && (p_dir->d_name[0] != '.') &&
                      (fra[de[i].fra_pos].dir_flag & DIR_STOPPED) &&
                      (fra[de[i].fra_pos].delete_files_flag & QUEUED_FILES) &&
                      (diff_time > fra[de[i].fra_pos].queued_file_time))
                  {
                     if (unlink(tmp_dir) == -1)
                     {
                        system_log(WARN_SIGN, __FILE__, __LINE__,
                                   "Failed to unlink() %s : %s",
                                   tmp_dir, strerror(errno));
                     }
                     else
                     {
#ifdef _DELETE_LOG
                        size_t dl_real_size;

                        (void)strcpy(dl.file_name, p_dir->d_name);
                        (void)snprintf(dl.host_name,
                                       MAX_HOSTNAME_LENGTH + 4 + 1,
                                       "%-*s %03x",
                                       MAX_HOSTNAME_LENGTH, "-",
                                       DEL_QUEUED_FILE);
                        *dl.file_size = stat_buf.st_size;
                        *dl.dir_id = de[i].dir_id;
                        *dl.job_id = 0;
                        *dl.input_time = 0L;
                        *dl.split_job_counter = 0;
                        *dl.unique_number = 0;
                        *dl.file_name_length = strlen(p_dir->d_name);
                        dl_real_size = *dl.file_name_length + dl.size +
                                       snprintf((dl.file_name + *dl.file_name_length + 1),
                                                MAX_FILENAME_LENGTH + 1,
# if SIZEOF_TIME_T == 4
                                                "%s%c>%ld (%s %d)",
# else
                                                "%s%c>%lld (%s %d)",
# endif
                                                DIR_CHECK, SEPARATOR_CHAR,
                                                (pri_time_t)diff_time,
                                                __FILE__, __LINE__);
                        if (write(dl.fd, dl.data, dl_real_size) != dl_real_size)
                        {
                           system_log(ERROR_SIGN, __FILE__, __LINE__,
                                      "write() error : %s",
                                      strerror(errno));
                        }
#endif
                        queued_files++;
                        queued_size_deleted += stat_buf.st_size;
                     }
                  }
               }
                    /*
                     * Search queue directories for old files!
                     */
               else if ((fra[de[i].fra_pos].delete_files_flag & QUEUED_FILES) &&
                        (p_dir->d_name[0] == '.') &&
                        (S_ISDIR(stat_buf.st_mode)))
                    {
                       int pos;

                       if (((pos = get_host_position(fsa,
                                                     &p_dir->d_name[1],
                                                     no_of_hosts)) != INCORRECT) &&
                           ((fsa[pos].host_status & DO_NOT_DELETE_DATA) == 0))
                       {
                          DIR *dp_2;

                          if ((dp_2 = opendir(tmp_dir)) == NULL)
                          {
                             system_log(WARN_SIGN, __FILE__, __LINE__,
                                        "Can't access directory %s : %s",
                                        tmp_dir, strerror(errno));
                          }
                          else
                          {
                             int   files_deleted = 0;
                             off_t file_size_deleted = 0;
                             char  *work_ptr_2;

                             work_ptr_2 = tmp_dir + strlen(tmp_dir);
                             *work_ptr_2++ = '/';
                             *work_ptr_2 = '\0';

                             errno = 0;
                             while ((p_dir = readdir(dp_2)) != NULL)
                             {
                                /* Ignore "." and "..". */
                                if (p_dir->d_name[0] == '.')
                                {
                                   continue;
                                }

                                (void)strcpy(work_ptr_2, p_dir->d_name);
                                if (stat(tmp_dir, &stat_buf) < 0)
                                {
                                   /*
                                    * Since this is a very low priority function lets not
                                    * always report when we fail to stat() a file. Maybe the
                                    * the user wants to keep some files.
                                    */
                                   continue;
                                }

                                /* Sure it is a normal file? */
                                if (S_ISREG(stat_buf.st_mode))
                                {
                                   diff_time = current_time - stat_buf.st_mtime;
                                   if (diff_time < 0)
                                   {
                                      diff_time = 0;
                                   }
                                   if (diff_time > fra[de[i].fra_pos].queued_file_time)
                                   {
                                      if (unlink(tmp_dir) == -1)
                                      {
                                         system_log(WARN_SIGN, __FILE__, __LINE__,
                                                    "Failed to unlink() %s : %s",
                                                    tmp_dir, strerror(errno));
                                      }
                                      else
                                      {
#ifdef _DELETE_LOG
                                         size_t dl_real_size;

                                         (void)strcpy(dl.file_name, p_dir->d_name);
                                         (void)snprintf(dl.host_name,
                                                        MAX_HOSTNAME_LENGTH + 4 + 1,
                                                        "%-*s %03x",
                                                        MAX_HOSTNAME_LENGTH,
                                                        fsa[pos].host_alias,
                                                        DEL_QUEUED_FILE);
                                         *dl.file_size = stat_buf.st_size;
                                         *dl.dir_id = de[i].dir_id;
                                         *dl.job_id = 0;
                                         *dl.input_time = 0L;
                                         *dl.split_job_counter = 0;
                                         *dl.unique_number = 0;
                                         *dl.file_name_length = strlen(p_dir->d_name);
                                         dl_real_size = *dl.file_name_length + dl.size +
                                                        snprintf((dl.file_name + *dl.file_name_length + 1),
                                                                 MAX_FILENAME_LENGTH + 1,
# if SIZEOF_TIME_T == 4
                                                                 "%s%c>%ld (%s %d)",
# else
                                                                 "%s%c>%lld (%s %d)",
# endif
                                                                 DIR_CHECK,
                                                                 SEPARATOR_CHAR,
                                                                 (pri_time_t)diff_time,
                                                                 __FILE__, __LINE__);
                                         if (write(dl.fd, dl.data, dl_real_size) != dl_real_size)
                                         {
                                            system_log(ERROR_SIGN, __FILE__, __LINE__,
                                                       "write() error : %s",
                                                       strerror(errno));
                                         }
#endif
                                         files_deleted++;
                                         file_size_deleted += stat_buf.st_size;
                                      }
                                   }
                                }
                                errno = 0;
                             } /* while ((p_dir = readdir(dp_2)) != NULL) */

                             if (errno)
                             {
                                system_log(ERROR_SIGN, __FILE__, __LINE__,
                                           "Could not readdir() %s : %s",
                                           tmp_dir, strerror(errno));
                             }

                             if (files_deleted > 0)
                             {
                                queued_files += files_deleted;
                                queued_size_deleted += file_size_deleted;
                                ABS_REDUCE_QUEUE(de[i].fra_pos, files_deleted,
                                                 file_size_deleted);
                             }

                             /* Don't forget to close the directory. */
                             if (closedir(dp_2) < 0)
                             {
                                system_log(ERROR_SIGN, __FILE__, __LINE__,
                                           "Could not close directory %s : %s",
                                           tmp_dir, strerror(errno));
                             }
                          }
                       }
                    }
               errno = 0;
            }

            /*
             * NOTE: The ENOENT is when it catches a file that is just
             *       being renamed (lock DOT).
             */
            if ((errno) && (errno != ENOENT))
            {
               system_log(ERROR_SIGN, __FILE__, __LINE__,
                          "Could not readdir() %s : %s",
                          tmp_dir, strerror(errno));
            }

            /* Don't forget to close the directory. */
            if (closedir(dp) < 0)
            {
               system_log(ERROR_SIGN, __FILE__, __LINE__,
                          "Could not close directory %s : %s",
                          tmp_dir, strerror(errno));
            }

            /* Remove file name from directory name. */
            *(work_ptr - 1) = '\0';

            /* Tell user there are old files in this directory. */
            if (((file_counter - junk_files) > 0) &&
                (fra[de[i].fra_pos].report_unknown_files == YES) &&
                ((fra[de[i].fra_pos].delete_files_flag & UNKNOWN_FILES) == 0))
            {
               p_fra = &fra[de[i].fra_pos];
               if (file_size >= GIGABYTE)
               {
                  receive_log(WARN_SIGN, NULL, 0, current_time,
                              "There are %d (%d GiB) old (>%dh) files in %s. @%x",
                              file_counter - junk_files,
                              (int)(file_size / 1073741824),
                              fra[de[i].fra_pos].unknown_file_time / 3600,
                              tmp_dir, de[i].dir_id);
               }
               else if (file_size >= MEGABYTE)
                    {
                       receive_log(WARN_SIGN, NULL, 0, current_time,
                                   "There are %d (%d MiB) old (>%dh) files in %s. @%x",
                                   file_counter - junk_files,
                                   (int)(file_size / 1048576),
                                   fra[de[i].fra_pos].unknown_file_time / 3600,
                                   tmp_dir, de[i].dir_id);
                    }
               else if (file_size >= KILOBYTE)
                    {
                       receive_log(WARN_SIGN, NULL, 0, current_time,
                                   "There are %d (%d KiB) old (>%dh) files in %s. @%x",
                                   file_counter - junk_files, (int)(file_size / 1024),
                                   fra[de[i].fra_pos].unknown_file_time / 3600,
                                   tmp_dir, de[i].dir_id);
                    }
                    else
                    {
                       receive_log(WARN_SIGN, NULL, 0, current_time,
#if SIZEOF_OFF_T == 4
                                   "There are %d (%ld bytes) old (>%dh) files in %s. @%x",
#else
                                   "There are %d (%lld bytes) old (>%dh) files in %s. @%x",
#endif
                                   file_counter - junk_files,
                                   (pri_off_t)file_size,
                                   fra[de[i].fra_pos].unknown_file_time / 3600,
                                   tmp_dir, de[i].dir_id);
                    }
            }
            if (junk_files > 0)
            {
               p_fra = &fra[de[i].fra_pos];
               receive_log(DEBUG_SIGN, NULL, 0, current_time,
                           "Deleted %d file(s) (>%dh) that where of length 0 or had a leading dot, in %s. @%x",
                           junk_files,
                           fra[de[i].fra_pos].unknown_file_time / 3600,
                           tmp_dir, de[i].dir_id);
            }
            if (queued_files > 0)
            {
               p_fra = &fra[de[i].fra_pos];
               if (queued_size_deleted >= GIGABYTE)
               {
                  receive_log(DEBUG_SIGN, NULL, 0, current_time,
                              "Deleted %d (%d GiB) queued file(s), in %s. @%x",
                              queued_files,
                              (int)(queued_size_deleted / 1073741824),
                              tmp_dir, de[i].dir_id);
               }
               else if (queued_size_deleted >= MEGABYTE)
                    {
                       receive_log(DEBUG_SIGN, NULL, 0, current_time,
                                   "Deleted %d (%d MiB) queued file(s), in %s. @%x",
                                   queued_files,
                                   (int)(queued_size_deleted / 1048576),
                                   tmp_dir, de[i].dir_id);
                    }
               else if (queued_size_deleted >= KILOBYTE)
                    {
                       receive_log(DEBUG_SIGN, NULL, 0, current_time,
                                   "Deleted %d (%d KiB) queued file(s), in %s. @%x",
                                   queued_files,
                                   (int)(queued_size_deleted / 1024),
                                   tmp_dir, de[i].dir_id);
                    }
                    else
                    {
                       receive_log(DEBUG_SIGN, NULL, 0, current_time,
#if SIZEOF_OFF_T == 4
                                   "Deleted %d (%ld bytes) queued file(s), in %s. @%x",
#else
                                   "Deleted %d (%lld bytes) queued file(s), in %s. @%x",
#endif
                                   queued_files, (pri_off_t)queued_size_deleted,
                                   tmp_dir, de[i].dir_id);
                    }
            }
         }
      }
   }

   return;
}
Example #12
0
SEXP attribute_hidden do_switch(SEXP call, SEXP op, SEXP args, SEXP rho)
{
    int argval, nargs = length(args);
    SEXP x, y, z, w, ans, dflt = NULL;

    if (nargs < 1) errorcall(call, _("'EXPR' is missing"));
    check1arg(args, call, "EXPR");
    PROTECT(x = eval(CAR(args), rho));
    if (!isVector(x) || length(x) != 1)
	errorcall(call, _("EXPR must be a length 1 vector"));
    if (isFactor(x))
	warningcall(call,
		    _("EXPR is a \"factor\", treated as integer.\n"
		      " Consider using '%s' instead."),
		    "switch(as.character( * ), ...)");
    if (nargs > 1) {
	/* There is a complication: if called from lapply
	   there may be a ... argument */
	PROTECT(w = expandDots(CDR(args), rho));
	if (isString(x)) {
	    for (y = w; y != R_NilValue; y = CDR(y)) {
		if (TAG(y) != R_NilValue) {
		    if (pmatch(STRING_ELT(x, 0), TAG(y), 1 /* exact */)) {
			/* Find the next non-missing argument.
			   (If there is none, return NULL.) */
			while (CAR(y) == R_MissingArg) {
			    y = CDR(y);
			    if (y == R_NilValue) break;
			    if (TAG(y) == R_NilValue) dflt = setDflt(y, dflt);
			}
			if (y == R_NilValue) {
			    R_Visible = FALSE;
			    UNPROTECT(2);
			    return R_NilValue;
			}
			/* Check for multiple defaults following y.  This loop
			   is not necessary to determine the value of the
			   switch(), but it should be fast and will detect
			   typos. */
			for (z = CDR(y); z != R_NilValue; z = CDR(z))
			    if (TAG(z) == R_NilValue) dflt = setDflt(z, dflt);

			ans =  eval(CAR(y), rho);
			UNPROTECT(2);
			return ans;
		    }
		} else
		    dflt = setDflt(y, dflt);
	    }
 	    if (dflt) {
		ans =  eval(dflt, rho);
		UNPROTECT(2);
		return ans;
	    }
	    /* fall through to error */
	} else { /* Treat as numeric */
	    argval = asInteger(x);
	    if (argval != NA_INTEGER && argval >= 1 && argval <= length(w)) {
		SEXP alt = CAR(nthcdr(w, argval - 1));
		if (alt == R_MissingArg)
		    error("empty alternative in numeric switch");
		ans =  eval(alt, rho);
		UNPROTECT(2);
		return ans;
	    }
	    /* fall through to error */
	}
	UNPROTECT(1); /* w */
    }
    /* an error */
    UNPROTECT(1); /* x */
    R_Visible = FALSE;
    return R_NilValue;
}
Example #13
0
SEXP attribute_hidden matchArgs(SEXP formals, SEXP supplied, SEXP call)
{
    int i, seendots, arg_i = 0;
    SEXP f, a, b, dots, actuals;

    actuals = R_NilValue;
    for (f = formals ; f != R_NilValue ; f = CDR(f), arg_i++) {
	/* CONS_NR is used since argument lists created here are only
	   used internally and so should not increment reference
	   counts */
	actuals = CONS_NR(R_MissingArg, actuals);
	SET_MISSING(actuals, 1);
    }
    /* We use fargused instead of ARGUSED/SET_ARGUSED on elements of
       formals to avoid modification of the formals SEXPs.  A gc can
       cause matchArgs to be called from finalizer code, resulting in
       another matchArgs call with the same formals.  In R-2.10.x, this
       corrupted the ARGUSED data of the formals and resulted in an
       incorrect "formal argument 'foo' matched by multiple actual
       arguments" error.
     */
    int fargused[arg_i ? arg_i : 1]; // avoid undefined behaviour
    memset(fargused, 0, sizeof(fargused));

    for(b = supplied; b != R_NilValue; b = CDR(b)) SET_ARGUSED(b, 0);

    PROTECT(actuals);

    /* First pass: exact matches by tag */
    /* Grab matched arguments and check */
    /* for multiple exact matches. */

    f = formals;
    a = actuals;
    arg_i = 0;
    while (f != R_NilValue) {
	if (TAG(f) != R_DotsSymbol) {
	    i = 1;
	    for (b = supplied; b != R_NilValue; b = CDR(b)) {
		if (TAG(b) != R_NilValue && pmatch(TAG(f), TAG(b), 1)) {
		    if (fargused[arg_i] == 2)
			error(_("formal argument \"%s\" matched by multiple actual arguments"),
			      CHAR(PRINTNAME(TAG(f))));
		    if (ARGUSED(b) == 2)
			error(_("argument %d matches multiple formal arguments"), i);
		    SETCAR(a, CAR(b));
		    if(CAR(b) != R_MissingArg) SET_MISSING(a, 0);
		    SET_ARGUSED(b, 2);
		    fargused[arg_i] = 2;
		}
		i++;
	    }
	}
	f = CDR(f);
	a = CDR(a);
        arg_i++;
    }

    /* Second pass: partial matches based on tags */
    /* An exact match is required after first ... */
    /* The location of the first ... is saved in "dots" */

    dots = R_NilValue;
    seendots = 0;
    f = formals;
    a = actuals;
    arg_i = 0;
    while (f != R_NilValue) {
	if (fargused[arg_i] == 0) {
	    if (TAG(f) == R_DotsSymbol && !seendots) {
		/* Record where ... value goes */
		dots = a;
		seendots = 1;
	    } else {
		i = 1;
		for (b = supplied; b != R_NilValue; b = CDR(b)) {
		    if (ARGUSED(b) != 2 && TAG(b) != R_NilValue &&
			pmatch(TAG(f), TAG(b), seendots)) {
			if (ARGUSED(b))
			    error(_("argument %d matches multiple formal arguments"), i);
			if (fargused[arg_i] == 1)
			    error(_("formal argument \"%s\" matched by multiple actual arguments"),
				  CHAR(PRINTNAME(TAG(f))));
			if (R_warn_partial_match_args) {
			    warningcall(call,
					_("partial argument match of '%s' to '%s'"),
					CHAR(PRINTNAME(TAG(b))),
					CHAR(PRINTNAME(TAG(f))) );
			}
			SETCAR(a, CAR(b));
			if (CAR(b) != R_MissingArg) SET_MISSING(a, 0);
			SET_ARGUSED(b, 1);
			fargused[arg_i] = 1;
		    }
		    i++;
		}
	    }
	}
	f = CDR(f);
	a = CDR(a);
        arg_i++;
    }

    /* Third pass: matches based on order */
    /* All args specified in tag=value form */
    /* have now been matched.  If we find ... */
    /* we gobble up all the remaining args. */
    /* Otherwise we bind untagged values in */
    /* order to any unmatched formals. */

    f = formals;
    a = actuals;
    b = supplied;
    seendots = 0;

    while (f != R_NilValue && b != R_NilValue && !seendots) {
	if (TAG(f) == R_DotsSymbol) {
	    /* Skip ... matching until all tags done */
	    seendots = 1;
	    f = CDR(f);
	    a = CDR(a);
	} else if (CAR(a) != R_MissingArg) {
	    /* Already matched by tag */
	    /* skip to next formal */
	    f = CDR(f);
	    a = CDR(a);
	} else if (ARGUSED(b) || TAG(b) != R_NilValue) {
	    /* This value used or tagged , skip to next value */
	    /* The second test above is needed because we */
	    /* shouldn't consider tagged values for positional */
	    /* matches. */
	    /* The formal being considered remains the same */
	    b = CDR(b);
	} else {
	    /* We have a positional match */
	    SETCAR(a, CAR(b));
	    if(CAR(b) != R_MissingArg) SET_MISSING(a, 0);
	    SET_ARGUSED(b, 1);
	    b = CDR(b);
	    f = CDR(f);
	    a = CDR(a);
	}
    }

    if (dots != R_NilValue) {
	/* Gobble up all unused actuals */
	SET_MISSING(dots, 0);
	i = 0;
	for(a = supplied; a != R_NilValue ; a = CDR(a)) if(!ARGUSED(a)) i++;

	if (i) {
	    a = allocList(i);
	    SET_TYPEOF(a, DOTSXP);
	    f = a;
	    for(b = supplied; b != R_NilValue; b = CDR(b))
		if(!ARGUSED(b)) {
		    SETCAR(f, CAR(b));
		    SET_TAG(f, TAG(b));
		    f = CDR(f);
		}
	    SETCAR(dots, a);
	}
    } else {
	/* Check that all arguments are used */
	SEXP unused = R_NilValue, last = R_NilValue;
	for (b = supplied; b != R_NilValue; b = CDR(b))
	    if (!ARGUSED(b)) {
		if(last == R_NilValue) {
		    PROTECT(unused = CONS(CAR(b), R_NilValue));
		    SET_TAG(unused, TAG(b));
		    last = unused;
		} else {
		    SETCDR(last, CONS(CAR(b), R_NilValue));
		    last = CDR(last);
		    SET_TAG(last, TAG(b));
		}
	    }

	if(last != R_NilValue) {
            /* show bad arguments in call without evaluating them */
            SEXP unusedForError = R_NilValue, last = R_NilValue;

            for(b = unused ; b != R_NilValue ; b = CDR(b)) {
                SEXP tagB = TAG(b), carB = CAR(b) ;
                if (TYPEOF(carB) == PROMSXP) carB = PREXPR(carB) ;
                if (last == R_NilValue) {
                    PROTECT(last = CONS(carB, R_NilValue));
                    SET_TAG(last, tagB);
                    unusedForError = last;
                } else {
                    SETCDR(last, CONS(carB, R_NilValue));
                    last = CDR(last);
                    SET_TAG(last, tagB);
                }
            }
	    errorcall(call /* R_GlobalContext->call */,
		      ngettext("unused argument %s",
			       "unused arguments %s",
			       (unsigned long) length(unusedForError)),
		      CHAR(STRING_ELT(deparse1line(unusedForError, 0), 0)) + 4);
                      /* '+ 4' is to remove 'list' from 'list(badTag1,...)' */
	}
    }
    UNPROTECT(1);
    return(actuals);
}
Example #14
0
/*
 * Look in the "data" file for more info.  Called if the user typed in the
 * whole name (user_typed_name == TRUE), or we've found a possible match
 * with a character/glyph.
 */
static void checkfile(const char *inp, struct permonst *pm, boolean user_typed_name,
		      boolean without_asking)
{
    dlb *fp;
    char buf[BUFSZ], newstr[BUFSZ];
    char *ep, *dbase_str;
    long txt_offset;
    int chk_skip;
    boolean found_in_file = FALSE, skipping_entry = FALSE;

    fp = dlb_fopen(DATAFILE, "r");
    if (!fp) {
	pline("Cannot open data file!");
	return;
    }

    /* To prevent the need for entries in data.base like *ngel to account
     * for Angel and angel, make the lookup string the same for both
     * user_typed_name and picked name.
     */
    if (pm != NULL && !user_typed_name)
	dbase_str = strcpy(newstr, pm->mname);
    else dbase_str = strcpy(newstr, inp);
    lcase(dbase_str);

    if (!strncmp(dbase_str, "interior of ", 12))
	dbase_str += 12;
    if (!strncmp(dbase_str, "a ", 2))
	dbase_str += 2;
    else if (!strncmp(dbase_str, "an ", 3))
	dbase_str += 3;
    else if (!strncmp(dbase_str, "the ", 4))
	dbase_str += 4;
    if (!strncmp(dbase_str, "tame ", 5))
	dbase_str += 5;
    else if (!strncmp(dbase_str, "peaceful ", 9))
	dbase_str += 9;
    if (!strncmp(dbase_str, "invisible ", 10))
	dbase_str += 10;
    if (!strncmp(dbase_str, "statue of ", 10))
	dbase_str[6] = '\0';
    else if (!strncmp(dbase_str, "figurine of ", 12))
	dbase_str[8] = '\0';

    /* Make sure the name is non-empty. */
    if (*dbase_str) {
	/* adjust the input to remove " [seen" and "named " and convert to lower case */
	char *alt = 0;	/* alternate description */

	if ((ep = strstri(dbase_str, " [seen")) != 0)
	    *ep = '\0';

	if ((ep = strstri(dbase_str, " named ")) != 0)
	    alt = ep + 7;
	else
	    ep = strstri(dbase_str, " called ");
	if (!ep) ep = strstri(dbase_str, ", ");
	if (ep && ep > dbase_str) *ep = '\0';

	/*
	 * If the object is named, then the name is the alternate description;
	 * otherwise, the result of makesingular() applied to the name is. This
	 * isn't strictly optimal, but named objects of interest to the user
	 * will usually be found under their name, rather than under their
	 * object type, so looking for a singular form is pointless.
	 */

	if (!alt)
	    alt = makesingular(dbase_str);
	else
	    if (user_typed_name)
		lcase(alt);

	/* skip first record; read second */
	txt_offset = 0L;
	if (!dlb_fgets(buf, BUFSZ, fp) || !dlb_fgets(buf, BUFSZ, fp)) {
	    impossible("can't read 'data' file");
	    dlb_fclose(fp);
	    return;
	} else if (sscanf(buf, "%8lx\n", &txt_offset) < 1 || txt_offset <= 0)
	    goto bad_data_file;

	/* look for the appropriate entry */
	while (dlb_fgets(buf,BUFSZ,fp)) {
	    if (*buf == '.') break;  /* we passed last entry without success */

	    if (digit(*buf)) {
		/* a number indicates the end of current entry */
		skipping_entry = FALSE;
	    } else if (!skipping_entry) {
		if (!(ep = strchr(buf, '\n'))) goto bad_data_file;
		*ep = 0;
		/* if we match a key that begins with "~", skip this entry */
		chk_skip = (*buf == '~') ? 1 : 0;
		if (pmatch(&buf[chk_skip], dbase_str) ||
			(alt && pmatch(&buf[chk_skip], alt))) {
		    if (chk_skip) {
			skipping_entry = TRUE;
			continue;
		    } else {
			found_in_file = TRUE;
			break;
		    }
		}
	    }
	}
    }

    if (found_in_file) {
	long entry_offset;
	int  entry_count;
	int  i;

	/* skip over other possible matches for the info */
	do {
	    if (!dlb_fgets(buf, BUFSZ, fp))
		goto bad_data_file;
	} while (!digit(*buf));
	
	if (sscanf(buf, "%ld,%d\n", &entry_offset, &entry_count) < 2) {
bad_data_file:	impossible("'data' file in wrong format");
		dlb_fclose(fp);
		return;
	}

	if (user_typed_name || without_asking || yn("More info?") == 'y') {
	    struct menulist menu;

	    if (dlb_fseek(fp, txt_offset + entry_offset, SEEK_SET) < 0) {
		pline("? Seek error on 'data' file!");
		dlb_fclose(fp);
		return;
	    }
	    
	    init_menulist(&menu);
	    for (i = 0; i < entry_count; i++) {
		if (!dlb_fgets(buf, BUFSZ, fp))
		    goto bad_data_file;
		if ((ep = strchr(buf, '\n')) != 0)
		    *ep = 0;
		if (strchr(buf+1, '\t') != 0)
		    tabexpand(buf+1);
		add_menutext(&menu, buf+1);
	    }
	    
	    display_menu(menu.items, menu.icount, NULL, FALSE, NULL);
	    free(menu.items);
	}
    } else if (user_typed_name)
	pline("I don't have any information on those things.");

    dlb_fclose(fp);
}
Example #15
0
Cell *gsub(Node **a, int nnn)	/* global substitute */
{
	register Cell *x, *y;
	register uchar *rptr, *sptr, *t, *pb;
	uchar buf[SUBSIZE];
	register fa *pfa;
	int mflag, tempstat, num;

	mflag = 0;	/* if mflag == 0, can replace empty string */
	num = 0;
	x = execute(a[3]);	/* target string */
	t = getsval(x);
	if (a[0] == 0)		/* 0 => a[1] is already-compiled regexpr */
		pfa = (fa *) a[1];	/* regular expression */
	else {
		y = execute(a[1]);
		pfa = makedfa(getsval(y), 1);
		tempfree(y);
	}
	y = execute(a[2]);	/* replacement string */
	if (pmatch(pfa, t)) {
		tempstat = pfa->initstat;
		pfa->initstat = 2;
		pb = buf;
		rptr = getsval(y);
		do {
			/*
			uchar *p;
			int i;
			printf("target string: %s, *patbeg = %o, patlen = %d\n",
				t, *patbeg, patlen);
			printf("	match found: ");
			p=patbeg;
			for (i=0; i<patlen; i++)
				printf("%c", *p++);
			printf("\n");
			*/
			if (patlen == 0 && *patbeg != 0) {	/* matched empty string */
				if (mflag == 0) {	/* can replace empty */
					num++;
					sptr = rptr;
					while (*sptr != 0 && pb < buf + SUBSIZE-1)
						if (*sptr == '\\' && *(sptr+1) == '&') {
							sptr++;
							*pb++ = *sptr++;
						} else if (*sptr == '&') {
							uchar *q;
							sptr++;
							for (q = patbeg; q < patbeg+patlen; )
								*pb++ = *q++;
						} else
							*pb++ = *sptr++;
				}
				if (*t == 0)	/* at end */
					goto done;
				*pb++ = *t++;
				if (pb >= buf + SUBSIZE-1)
					ERROR "gsub() result %.30s too big", buf FATAL;
				mflag = 0;
			}
			else {	/* matched nonempty string */
				num++;
				sptr = t;
				while (sptr < patbeg && pb < buf + SUBSIZE-1)
					*pb++ = *sptr++;
				sptr = rptr;
				while (*sptr != 0 && pb < buf + SUBSIZE-1)
					if (*sptr == '\\' && *(sptr+1) == '&') {
						sptr++;
						*pb++ = *sptr++;
					} else if (*sptr == '&') {
						uchar *q;
						sptr++;
						for (q = patbeg; q < patbeg+patlen; )
							*pb++ = *q++;
					} else
						*pb++ = *sptr++;
				t = patbeg + patlen;
				if ((*(t-1) == 0) || (*t == 0))
					goto done;
				if (pb >= buf + SUBSIZE-1)
					ERROR "gsub() result %.30s too big", buf FATAL;
				mflag = 1;
			}
		} while (pmatch(pfa,t));
		sptr = t;
		while (*pb++ = *sptr++)
			;
	done:	if (pb >= buf + SUBSIZE-1)
			ERROR "gsub() result %.30s too big", buf FATAL;
		*pb = '\0';
		setsval(x, buf);
		pfa->initstat = tempstat;
	}
	tempfree(x);
	tempfree(y);
	x = gettemp();
	x->tval = NUM;
	x->fval = num;
	return(x);
}
Example #16
0
// Function to load the runInfo structure with all runID's on local disk
void US_XpnRunAuc::load_runs( void )
{
   impdir         = US_Settings::importDir();  // Imports directory
   impdir.replace( "\\", "/" );                // Possible Windows issue

   if ( impdir.right( 1 ) != "/" )
      impdir         = impdir + "/";           // Insure trailing slash

   // Set up to load either from a raw DB file or from openAUC files
   QStringList efilt( "*.auc" );

   QStringList runids;
   QStringList rdirs = QDir( impdir ).entryList(
         QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name );
qDebug() << "LdDk:  rdirs count" << rdirs.count() << "impdir" << impdir
 << "efilt" << efilt;

   // Get the list of all Run IDs with data in their work directories
   for ( int ii = 0; ii < rdirs.count(); ii++ )
   {
      QString runID  = rdirs[ ii ];
      QString wdir   = impdir + runID + "/";
      QStringList efiles = QDir( wdir ).entryList( efilt, QDir::Files,
                                                   QDir::Name );
      int nfiles     = efiles.count();
qDebug() << "LdDk:   ii" << ii << "run" << rdirs[ii]
 << "count" << nfiles;

      if ( nfiles < 1 )              // Definitely not Optima
         continue;

      QString rfn    = wdir + efiles[ 0 ];
      QString date   = US_Util::toUTCDatetimeText(
                          QFileInfo( rfn ).lastModified().toUTC()
                          .toString( Qt::ISODate ), true )
                          .section( " ", 0, 0 ).simplified();

      // Look for TMST definition file and test import origin
      QString dfname = runID + ".time_state.xml";
      QString dpath  = wdir + dfname;
      QFile dfile( dpath );

      if ( ! dfile.exists()  ||
           ! dfile.open( QIODevice::ReadOnly ) )
         continue;  // Skip if TMST def file does not exist or can't be opened
qDebug() << "LdDk:    dfname -- exists/opened";

      QTextStream fsi( &dfile );
      QString pmatch( "import_type=\"Optima\"" );
      QString pmatch2( "import_type=\"XPN\"" );
      QString xmli   = fsi.readAll();
      dfile.close();
qDebug() << "LdDk:     pmatch" << pmatch;

      if ( ! xmli.contains( pmatch )  &&
           ! xmli.contains( pmatch2 ) )
         continue;  // Skip if TMST def has no import_type="Optima"


      // Add an eligible run directory to the list
//qDebug() << "LdDk:   ii" << ii << "  rfn" << rfn;
      RunInfo rr;
      rr.runID       = runID;
      rr.date        = date;
      rr.ntriple     = nfiles;
//qDebug() << "LdDk:   ii" << ii << "     runID date count"
// << rr.runID << rr.date << rr.nfiles;

      runInfo << rr;
   }

   if ( runInfo.size() < 1 )
   {
      QMessageBox::information( this,
             tr( "Error" ),
             tr( "There are no US3 runs on the local Disk to load.\n" ) );
   }

   return;
}
Example #17
0
DESCR__S_M3
/*
 ** NAME
 **   remove_files - removes the given files in the specified directory
 **
 ** SYNOPSIS
 **   int remove_files(char *dirname, char *filter)
 **
 ** DESCRIPTION
 **   Deletes 'filter' files in the directory 'dirname'. It will
 **   fail to delete any directories.
 **
 ** RETURN VALUES
 **   Returns INCORRECT when it fails to delete any files matching
 **   the filter. Otherwise it returns the number of files that where
 **   deleted, even 0 when no files are deleted.
 **
 ** AUTHOR
 **   H.Kiehl
 **
 ** HISTORY
 **   07.04.2006 H.Kiehl Created
 **
 */
DESCR__E_M3

#include <stdio.h>
#include <string.h>             /* strcpy(), strlen()                    */
#include <unistd.h>             /* rmdir(), unlink()                     */
#include <sys/types.h>
#include <dirent.h>             /* opendir(), readdir(), closedir()      */
#include <errno.h>


/*############################ remove_files() ###########################*/
int
remove_files(char *dirname, char *filter)
{
   int           addchar = NO,
                 count = 0,
                 ret = SUCCESS;
   char          *ptr;
   struct dirent *dirp;
   DIR           *dp;

   ptr = dirname + strlen(dirname);
   if ((dp = opendir(dirname)) == NULL)
   {
      system_log(ERROR_SIGN, __FILE__, __LINE__,
                 _("Failed to opendir() `%s' : %s"), dirname, strerror(errno));
      return(INCORRECT);
   }
   if (*(ptr - 1) != '/')
   {
      *(ptr++) = '/';
      addchar = YES;
   }

   errno = 0;
   while ((dirp = readdir(dp)) != NULL)
   {
      if ((dirp->d_name[0] == '.') && ((dirp->d_name[1] == '\0') ||
          ((dirp->d_name[1] == '.') && (dirp->d_name[2] == '\0'))))
      {
         continue;
      }
      if (pmatch(filter, dirp->d_name, NULL) == 0)
      {
         (void)strcpy(ptr, dirp->d_name);
         if (unlink(dirname) == -1)
         {
            if (errno != ENOENT)
            {
               system_log(ERROR_SIGN, __FILE__, __LINE__,
                          _("Failed to delete `%s' : %s"),
                          dirname, strerror(errno));
               ret = INCORRECT;
            }
         }
         else
         {
            count++;
         }
      }
      errno = 0;
   }
   if (addchar == YES)
   {
      ptr[-1] = 0;
   }
   else
   {
      *ptr = '\0';
   }
   if (errno != 0)
   {
      system_log(ERROR_SIGN, __FILE__, __LINE__,
                 _("Failed to readdir() `%s' : %s"), dirname, strerror(errno));
      ret = INCORRECT;
   }
   if (closedir(dp) == -1)
   {
      system_log(ERROR_SIGN, __FILE__, __LINE__,
                 _("Failed to closedir() `%s' : %s"), dirname, strerror(errno));
      ret = INCORRECT;
   }
   if (ret != INCORRECT)
   {
      ret = count;
   }

   return(ret);
}