/* * @brief 轮询目录和文件 * @return 0表示成功,可继续休眠 -1表示轮询过程中出错 */ int polling_files() { DIR *dir; char dirbuffer[FULL_PATH_LEN]; int flag; while (1) { get_ini(foldername, filename); //memcpy(tmpfolder, foldername, FOLDER_NAME_LEN); sprintf(dirbuffer, "%s/%s", data_store_path, foldername); dir = opendir((const char *)dirbuffer); if (dir == NULL) { write_log("Open dir <%s> failed!\n", dirbuffer); return 0; //return and have a rest may be directory have not been constructed } flag = processdir(dir, foldername, filename); if (-1 == flag) { closedir(dir); return -1; //have a rest } //when completly processed this dir we want to get into next folder //but next folder may be have not constructed yet, but if collect module running, //at most 60 secondes elapsed,next folder must be exist! sleep(60); //update_folder(tmpfolder, REALLY_UPDATE_FOLDER); update_folder(foldername, REALLY_UPDATE_FOLDER); closedir(dir); } return 0; }
/* * @brief 轮询目录和文件 * @return 0表示成功,可继续休眠 -1表示轮询过程中出错 */ int polling_files() { DIR *dir; char dirbuffer[FULL_PATH_LEN]; while (1) { sprintf(dirbuffer, "%s", data_store_path); dir = opendir((const char *)dirbuffer); if (dir == NULL) { write_log("Open dir <%s> failed!\n", dirbuffer); return 0; //return and have a rest may be directory have not been constructed } processdir(dir); sleep(60); closedir(dir); } return 0; }
int main(int argc, char *argv[]) { char *ptr; char option; struct stat st; char *conffile = NULL; const char *const myname = "rnews"; ln_log_open(myname); if (!initvars(argv[0], 0)) init_failed(myname); while ((option = getopt(argc, argv, GLOBALOPTS "")) != -1) { if (parseopt(myname, option, optarg, &conffile)) continue; switch(option) { default: usage(); exit(EXIT_FAILURE); } } if (readconfig(conffile) != 0) { ln_log(LNLOG_SERR, LNLOG_CTOP, "Reading configuration failed: %m.\n"); exit(2); } if (conffile) free(conffile); if (filterfile && !readfilter(filterfile)) { ln_log(LNLOG_SERR, LNLOG_CTOP, "%s: Cannot read filterfile %s, aborting.", argv[0], filterfile); log_unlink(lockfile, 0); exit(EXIT_FAILURE); } if (!init_post()) init_failed(myname); umask((mode_t) 077); if (attempt_lock(LOCKWAIT)) { exit(EXIT_FAILURE); } rereadactive(); readlocalgroups(); if (!argv[optind]) fprocessfile(stdin); /* process stdin */ while ((ptr = argv[optind++])) { if (stat(ptr, &st) == 0) { if (S_ISDIR(st.st_mode)) processdir(ptr); else if (S_ISREG(st.st_mode)) processfile(ptr); else ln_log(LNLOG_SERR, LNLOG_CTOP, "%s: cannot open %s\n", argv[0], ptr); } else ln_log(LNLOG_SERR, LNLOG_CTOP, "%s: cannot stat %s\n", argv[0], ptr); } writeactive(); /* write groupinfo file */ freeallfilter(filter); log_unlink(lockfile, 0); exit(0); }
int main(int argc, char *argv[]) { int c; char *dir = "."; char *outf = NULL; char *volname = NULL; int verbose = 0; char buf[256]; struct filenode *root; struct stat sb; int lastoff; int i; char *p; struct aligns *pa, *pa2; struct excludes *pe, *pe2; FILE *f; while((c = getopt(argc, argv, "V:vd:f:ha:A:x:")) != EOF) { switch(c) { case 'd': dir = optarg; break; case 'f': outf = optarg; break; case 'V': volname = optarg; break; case 'v': verbose = 1; break; case 'h': showhelp(argv[0]); exit(0); case 'a': align = strtoul(optarg, NULL, 0); if(align < 16 || (align & (align - 1))) { fprintf(stderr, "Align has to be at least 16 bytes and a power of two\n"); exit(1); } break; case 'A': i = strtoul(optarg, &p, 0); if(i < 16 || (i & (i - 1))) { fprintf(stderr, "Align has to be at least 16 bytes and a power of two\n"); exit(1); } if(*p != ',' || !p[1]) { fprintf(stderr, "-A takes N,PATTERN format of argument, where N is a number\n"); exit(1); } /* strlen(p+1) + 1 eq strlen(p) */ pa = (struct aligns *)malloc(sizeof(*pa) + strlen(p)); pa->align = i; pa->next = NULL; strcpy(pa->pattern, p + 1); if(!alignlist) alignlist = pa; else { for(pa2 = alignlist; pa2->next; pa2 = pa2->next) ; pa2->next = pa; } break; case 'x': pe = (struct excludes *)malloc(sizeof(*pe) + strlen(optarg) + 1); pe->next = NULL; strcpy(pe->pattern, optarg); if(!excludelist) excludelist = pe; else { for(pe2 = excludelist; pe2->next; pe2 = pe2->next) ; pe2->next = pe; } break; default: exit(1); } } if(!volname) { sprintf(buf, "rom %08lx", time(NULL)); volname = buf; } if(!outf) { fprintf(stderr, "%s: you must specify the destination file\n", argv[0]); fprintf(stderr, "Try `%s -h' for more information\n", argv[0]); exit(1); } if(strcmp(outf, "-") == 0) { f = fdopen(1, "wb"); } else f = fopen(outf, "wb"); if(!f) { perror(outf); exit(1); } realbase = strlen(dir); root = newnode(dir, volname, 0); root->parent = root; lastoff = processdir(1, dir, dir, &sb, root, root, spaceneeded(root)); if(lastoff < 0) { fprintf(stderr, "Error while processing directory.\n"); return 1; } if(verbose) shownode(0, root, stderr); if(dumpall(root, lastoff, f)) { fprintf(stderr, "Error while dumping!\n"); return 1; } return 0; }
int processdir(int level, const char *base, const char *dirname, struct stat *sb, struct filenode *dir, struct filenode *root, int curroffset) { DIR *dirfd; struct dirent *dp; struct filenode *n, *link; struct excludes *pe; if(level <= 1) { /* Ok, to make sure . and .. are handled correctly * we add them first. Note also that we alloc them * first to get to know the real name */ link = newnode(base, ".", curroffset); if(!lstat(link->realname, sb)) { setnode(link, sb->st_dev, sb->st_ino, sb->st_mode); append(&dir->dirlist, link); /* special case for root node - '..'s in subdirs should link to * '.' of root node, not root node itself. */ dir->dirlist.owner = link; curroffset = alignnode(link, curroffset, 0) + spaceneeded(link); n = newnode(base, "..", curroffset); if(!lstat(n->realname, sb)) { setnode(n, sb->st_dev, sb->st_ino, sb->st_mode); append(&dir->dirlist, n); n->orig_link = link; curroffset = alignnode(n, curroffset, 0) + spaceneeded(n); } } } dirfd = opendir(dir->realname); while((dp = readdir(dirfd))) { /* don't process main . and .. twice */ if(level <= 1 && (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)) continue; n = newnode(base, dp->d_name, curroffset); /* Process exclude list. */ for(pe = excludelist; pe; pe = pe->next) { if(!nodematch(pe->pattern, n)) { freenode(n); break; } } if(pe) continue; if(lstat(n->realname, sb)) { fprintf(stderr, "ignoring '%s' (lstat failed)\n", n->realname); freenode(n); continue; } /* Handle special names */ if(n->name[0] == '@') { if(S_ISLNK(sb->st_mode)) { /* this is a link to follow at build time */ n->name = n->name + 1; /* strip off the leading @ */ memset(bigbuf, 0, sizeof(bigbuf)); if(readlink(n->realname, bigbuf, sizeof(bigbuf))) { return -1; } n->realname = strdup(bigbuf); if(lstat(n->realname, sb)) { fprintf(stderr, "ignoring '%s' (lstat failed)\n", n->realname); freenode(n); continue; } } else if(S_ISREG(sb->st_mode) && sb->st_size == 0) { /* * special file @name,[bcp..],major,minor */ char devname[32]; char type; int major; int minor; if(sscanf(n->name, "@%[a-zA-Z0-9],%c,%d,%d", devname, &type, &major, &minor) == 4) { strcpy(n->name, devname); sb->st_rdev = makedev(major, minor); sb->st_mode &= ~S_IFMT; switch(type) { case 'c': case 'u': sb->st_mode |= S_IFCHR; break; case 'b': sb->st_mode |= S_IFBLK; break; case 'p': sb->st_mode |= S_IFIFO; break; default: fprintf(stderr, "Invalid special device type '%c' " "for file %s\n", type, n->realname); freenode(n); continue; } } } } setnode(n, sb->st_dev, sb->st_ino, sb->st_mode); /* Skip unreadable files/dirs */ if(!S_ISLNK(n->modes) && access(n->realname, R_OK)) { fprintf(stderr, "ignoring '%s' (access failed)\n", n->realname); freenode(n); continue; } /* Look up old links */ if(strcmp(n->name, ".") == 0) { append(&dir->dirlist, n); link = n->parent; } else if(strcmp(n->name, "..") == 0) { append(&dir->dirlist, n); link = n->parent->parent; } else { link = findnode(root, n->ondev, n->onino); append(&dir->dirlist, n); } if(link) { n->orig_link = link; curroffset = alignnode(n, curroffset, 0) + spaceneeded(n); continue; } if(S_ISREG(sb->st_mode)) { curroffset = alignnode(n, curroffset, spaceneeded(n)); n->size = sb->st_size; } else curroffset = alignnode(n, curroffset, 0); if(S_ISLNK(sb->st_mode)) { n->size = sb->st_size; } curroffset += spaceneeded(n); if(S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) { n->devnode = sb->st_rdev; } if(S_ISDIR(sb->st_mode)) { if(!strcmp(n->name, "..")) { curroffset = processdir(level + 1, dir->realname, dp->d_name, sb, dir, root, curroffset); } else { curroffset = processdir(level + 1, n->realname, dp->d_name, sb, n, root, curroffset); } if(curroffset < 0) return -1; } } closedir(dirfd); return curroffset; }