예제 #1
0
static void make_file_index(void)
{
   #define MAX_CREATION_DATE 15
   FILE *fp_ou;
   DIR *dirp;
   struct dirent *direntp;
   char wdir[MAXLEN];
   char data[80];
   char tbytes[20];
   char media[20];
   char ftime[128];
   char day[6], mon[8], year[40], hour[10];
   int iyear, imonth, iday, ihour, iminute, isecond, idst;
   int nsort;
   int nallocated;
   int order;
   int i;
   int tuser;
   struct getwordstruct gwarea;
   struct sortstruct
   {
      int year, month, day, sortnum;
      char creationdate[MAX_CREATION_DATE];
      char *dirname;
      char date[60];
   } **sortlist, *item, **tempsort;

   sprintf(wdir,"%sindex.html",outdir);

   order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1;

   if ((dirp = opendir(outdir)) == NULL) {
      debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
      exit(EXIT_FAILURE);
   }

   nsort=0;
   nallocated=0;
   sortlist=NULL;
   while ((direntp = readdir( dirp )) != NULL) {
      if (strchr(direntp->d_name,'-') == 0) continue;
      item=malloc(sizeof(*item));
      if (!item) {
         debuga(_("not enough memory to sort the index\n"));
         exit(EXIT_FAILURE);
      }
      if(strcmp(df,"u") == 0) {
         item->year=atoi(direntp->d_name);
         item->month=conv_month(direntp->d_name+4);
         item->day=atoi(direntp->d_name+7);
      } else {
         item->year=atoi(direntp->d_name+5);
         item->month=conv_month(direntp->d_name+2);
         item->day=atoi(direntp->d_name);
      }
      item->sortnum=(item->year*16+item->month)*32+item->day;
      obtdate(outdir,direntp->d_name,data);
      if (sscanf(data,"%d-%d-%d %d:%d:%d %d",&iyear,&imonth,&iday,&ihour,&iminute,&isecond,&idst)==7) {
         formatdate(data,sizeof(data),iyear,imonth,iday,ihour,iminute,isecond,idst);
         snprintf(item->creationdate,sizeof(item->creationdate),"%04d%02d%02d%02d%02d%02d",iyear,imonth,iday,ihour,iminute,isecond);
      } else {
         /*
         Old code to parse a date stored by sarg before 2.2.6.1 in the sarg-date file of each report directory.
         */
         getword_start(&gwarea,data);
         if (getword_skip(16,&gwarea,' ')<0) {
            debuga(_("Maybe you have a broken week day in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
            exit(EXIT_FAILURE);
         }
         if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) {
            debuga(_("Maybe you have a broken month in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
            exit(EXIT_FAILURE);
         }
         if (getword_multisep(day,sizeof(day),&gwarea,' ')<0) {
            debuga(_("Maybe you have a broken day in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
            exit(EXIT_FAILURE);
         }
         if (getword_multisep(hour,sizeof(hour),&gwarea,' ')<0) {
            debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
            exit(EXIT_FAILURE);
         }
         do {
            if (getword_multisep(year,sizeof(year),&gwarea,' ')<0) {
               debuga(_("Maybe you have a broken year in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
               exit(EXIT_FAILURE);
            }
         } while (year[0] && !isdigit(year[0])); //skip time zone information with spaces until the year is found
         if (sscanf(hour,"%d:%d:%d",&ihour,&iminute,&isecond)!=3) {
            debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name);
            exit(EXIT_FAILURE);
         }
         buildymd(day,mon,year,ftime);
         snprintf(item->creationdate,sizeof(item->creationdate),"%s%02d%02d%02d",ftime, ihour, iminute, isecond);
      }
      item->dirname=strdup(direntp->d_name);
      if (!item->dirname) {
         debuga(_("Not enough memory to store the directory name \"%s\" in the index\n"),direntp->d_name);
         exit(EXIT_FAILURE);
      }
      strncpy(item->date,data,sizeof(item->date));
      if (nsort+1>nallocated) {
         nallocated+=10;
         tempsort=realloc(sortlist,nallocated*sizeof(*item));
         if (!tempsort) {
            debuga(_("not enough memory to sort the index\n"));
            exit(EXIT_FAILURE);
         }
         sortlist=tempsort;
      }
      for (i=nsort ; i>0 ; i--) {
         if (item->sortnum>sortlist[i-1]->sortnum) break;
         if (item->sortnum==sortlist[i-1]->sortnum) {
            if (strcmp(item->creationdate,sortlist[i-1]->creationdate)>=0) break;
         }
         sortlist[i]=sortlist[i-1];
      }
      sortlist[i]=item;
      nsort++;
   }

   closedir( dirp );

   if((fp_ou=fopen(wdir,"w"))==NULL) {
      debuga(_("(index) Cannot open file %s\n"),wdir);
      exit(EXIT_FAILURE);
   }
   write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort));
   close_html_header(fp_ou);
   fputs("<div class=\"index\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",fp_ou);
   fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("FILE/PERIOD"),_("CREATION DATE"),_("USERS"),_("BYTES"),_("AVERAGE"));
   for (i=0 ; i<nsort ; i++) {
      if (order>0)
         item=sortlist[i];
      else
         item=sortlist[nsort-i-1];
      tuser=obtuser(outdir,item->dirname);
      obttotal(outdir,item->dirname,tbytes,tuser,media);
      fprintf(fp_ou,"<tr><td class=\"data2\"><a href='%s/%s'>%s</a></td><td class=\"data2\">%s</td><td class=\"data\">%d</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",item->dirname,ReplaceIndex,item->dirname,item->date,tuser,tbytes,media);
   }
   fputs("</table></div>\n",fp_ou);
   if (write_html_trailer(fp_ou)<0)
      debuga(_("Write error in the index %s\n"),wdir);
   if (fclose(fp_ou)==EOF)
      debuga(_("Failed to close the index file %s - %s\n"),wdir,strerror(errno));

   if (sortlist) {
      for (i=0 ; i<nsort ; i++) {
         free(sortlist[i]->dirname);
         free(sortlist[i]);
      }
      free(sortlist);
   }
}
예제 #2
0
 void info(char* pathName) {
	struct stat entryInfo;
	char time[36];
	char type[40];
	char mode[] = "---------";

	if( stat( pathName, &entryInfo ) == 0 ) {
		if( S_ISDIR( entryInfo.st_mode ) ) {            
			DIR *dir = opendir( pathName );
			char deepPathName[PATH_MAX + 1];
			int retval = 0;
			if( dir == NULL ) {
        		printf( "Error opening %s: %s", pathName, strerror( errno ) );
        		return ;
    		}

    		struct dirent entry;
    		struct dirent *entryPtr = NULL;

    		retval = readdir_r( dir, &entry, &entryPtr );
    		while( entryPtr != NULL ) {
        		struct stat entryInfo;

		        if( ( strncmp( entry.d_name, ".", PATH_MAX ) == 0 ) ||
		            ( strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) ) {
		            /* Short-circuit the . and .. entries. */
		            retval = readdir_r( dir, &entry, &entryPtr );
		            continue;
        		}

        		(void)strncpy( deepPathName, pathName, PATH_MAX );
      			(void)strncat( deepPathName, "/", PATH_MAX );
        		(void)strncat( deepPathName, entry.d_name, PATH_MAX );

        		info(deepPathName);

        		retval = readdir_r( dir, &entry, &entryPtr );

        	}
        } else {
        	printf("%s\n", pathName);
        	printf("User: %s\n", getpwuid(entryInfo.st_uid)->pw_name);
        	//printf("%d\n", entryInfo.st_atime);
        	
        	printf("Access: %s\n", formatdate(time, entryInfo.st_atime));

        	if (S_ISBLK(entryInfo.st_mode))
        		strcpy(type , "block device");
        	else if (S_ISCHR(entryInfo.st_mode))
        		strcpy(type ,"character device");
        	else if (S_ISDIR(entryInfo.st_mode))
        		strcpy(type,"directory");
        	else if (S_ISFIFO(entryInfo.st_mode))
        		strcpy(type , "FIFO");
        	else if (S_ISLNK(entryInfo.st_mode))
        		strcpy(type , "link");
        	else if(S_ISREG(entryInfo.st_mode))
        		strcpy(type , "regular file");

        	printf("Type: %s\n", type);


        	if ( entryInfo.st_mode & S_IRUSR ) mode[1] = 'r';    /* 3 bits for user  */
    		if ( entryInfo.st_mode & S_IWUSR ) mode[2] = 'w';
    		if ( entryInfo.st_mode & S_IXUSR ) mode[3] = 'x';

		    if ( entryInfo.st_mode & S_IRGRP ) mode[4] = 'r';    /* 3 bits for group */
		    if ( entryInfo.st_mode & S_IWGRP ) mode[5] = 'w';
		    if ( entryInfo.st_mode & S_IXGRP ) mode[6] = 'x';

		    if ( entryInfo.st_mode & S_IROTH ) mode[7] = 'r';    /* 3 bits for other */
		    if ( entryInfo.st_mode & S_IWOTH ) mode[8] = 'w';
		    if ( entryInfo.st_mode & S_IXOTH ) mode[9] = 'x';

		    printf("Permissions: %s\n", mode);
		    printf("\n");
        }
	} 
}