Example #1
0
void copydupe(uint16_t c,osFile oldfh,osFile newfh)
{
   char buf[300];
   uint16_t size;

   osSeek(oldfh,dupebuf[c].offset,OFFSET_BEGINNING);

   if(osRead(oldfh,&size,sizeof(uint16_t))!=sizeof(uint16_t))
      return;

   if(osRead(oldfh,buf,size) != size)
      return;

   if(!osWrite(newfh,&size,sizeof(uint16_t)))
   {
		uint32_t err=osError();
      LogWrite(1,SYSTEMERR,"Failed to write to temporary dupe file");
      LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err));
      return;
   }

   if(!osWrite(newfh,buf,size))
   {
		uint32_t err=osError();
      LogWrite(1,SYSTEMERR,"Failed to write to temporary dupe file");
      LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err));
      return;
   }
}
Example #2
0
bool CheckDupe(struct MemMessage *mm)
{
   uint32_t c,crc32,*crc32p;
   uint16_t size,dsize;
   char dbuf[300],buf[300];

   if(mm->MSGID[0] == 0)
      return(FALSE); /* No dupechecking for messages without MSGID */

   crc32=calcstringcrc(mm->MSGID);

   crc32p=(uint32_t *)dbuf;
   *crc32p=crc32;
   dsize=4;

   strcpy(&dbuf[dsize],mm->MSGID);
   dsize += strlen(mm->MSGID) + 1;

   strcpy(&dbuf[dsize],mm->Area);
   dsize += strlen(mm->Area) + 1;

   for(c=0;c<dupeentrymax;c++)
      if(dupebuf[c].crc32 == crc32)
      {
         /* CRC matches, check rest */

         osSeek(dupefh,dupebuf[c].offset,OFFSET_BEGINNING);

         if(osRead(dupefh,&size,sizeof(uint16_t))==sizeof(uint16_t))
            if(size == dsize)
               if(osRead(dupefh,buf,(uint32_t)size) == size)
                  if(dupecomp(buf,dbuf,dsize) == 0)
                     return(TRUE); /* Dupe */
      }


   AddDupeBuf(dbuf,dsize);

   return(FALSE);
}
// a simple test for OS layer
void osTest() {
    SqlVFS *os = osGetVFS("unix");
    SqlFile *file = osGetFileHandle(os);
    printf("%d\n", osOpen(os, "data", file, O_RDWR | O_CREAT));
    printf("%d\n", osWrite(file, "123", 3, 0));
    printf("%d\n", osWrite(file, "abcd", 4, 1));
    printf("%d\n", osTruncate(file, 2));

    char buf[20];
    int ret = osRead(file, buf, 20, 0);
    printf("%d\n", ret);
    buf[ret] = 0;
    printf("%s\n", buf);
}
Example #4
0
int main(int argc, char **argv)
{
   osFile fh;
   uint32_t total,areas,totaldupes;
   time_t firsttime,t;
   uint32_t DayStatsWritten;
   char buf[200],date[30],date2[30];
   struct DiskAreaStats dastat;
   struct DiskNodeStats dnstat;
   struct StatsNode *sn;
   struct NodeStatsNode *nsn;
   struct jbList StatsList;
   struct jbList NodesList;
   uint32_t c,num,tot;
   uint16_t total8days[8];
   char sortmode;
   struct tm *tp;
   char *monthnames[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","???"};

   signal(SIGINT,breakfunc);
         
   if(!osInit())
      exit(OS_EXIT_ERROR);

   if(argc > 1 &&
	  (strcmp(argv[1],"?")==0      ||
		strcmp(argv[1],"-h")==0     ||
		strcmp(argv[1],"--help")==0 ||
		strcmp(argv[1],"help")==0 ||
		strcmp(argv[1],"/h")==0     ||
		strcmp(argv[1],"/?")==0 ))
   {
      printargs(args);
      osEnd();
      exit(OS_EXIT_OK);
   }

   if(!parseargs(args,argc,argv))
   {
      osEnd();
      exit(OS_EXIT_ERROR);
   }

   sortmode='a';
   
   if(args[ARG_SORT].data)
      sortmode=tolower(((char *)args[ARG_SORT].data)[0]);
      
   if(!strchr("amtdlu",sortmode))
   {
      printf("Unknown sort mode %c\n",sortmode);
      osEnd();
      exit(OS_EXIT_ERROR);
   }

   if(args[ARG_NOAREAS].data && args[ARG_NONODES].data)
   {
      printf("Nothing to do\n");
      osEnd();
      exit(OS_EXIT_ERROR);
   }

   printf("CrashStats "VERSION" © "  COPYRIGHT " Johan Billing\n");

   if(!(fh=osOpen(args[ARG_FILE].data,MODE_OLDFILE)))
   {
		uint32_t err=osError();
      printf("Error opening %s\n",(char *)args[ARG_FILE].data);
		printf("Error: %s\n",osErrorMsg(err));		
      osEnd();
      exit(OS_EXIT_ERROR);
   }

   osRead(fh,buf,4);
   buf[4]=0;

   if(strcmp(buf,STATS_IDENTIFIER)!=0)
   {
      printf("Unknown format of stats file\n");
      osClose(fh);
      osEnd();
      exit(OS_EXIT_ERROR);
   }

   osRead(fh,&DayStatsWritten,sizeof(uint32_t));

   total=0;
   totaldupes=0;
   firsttime=0;
   areas=0;

   for(c=0;c<8;c++)
   	total8days[c]=0;

   jbNewList(&StatsList);
   jbNewList(&NodesList);

   osRead(fh,&num,sizeof(uint32_t));
   c=0;

   if(!args[ARG_NOAREAS].data)
   {
      while(c<num && osRead(fh,&dastat,sizeof(struct DiskAreaStats))==sizeof(struct DiskAreaStats))
      {
         if(!args[ARG_GROUP].data || CheckFlags(dastat.Group,args[ARG_GROUP].data))
         {
            if(!(sn=osAlloc(sizeof(struct StatsNode))))
            {
               printf("Out of memory\n");
               jbFreeList(&StatsList);
               osClose(fh);
               osEnd();
               exit(OS_EXIT_ERROR);
            }

            jbAddNode(&StatsList,(struct jbNode *)sn);

            strcpy(sn->Tagname,dastat.Tagname);
            sn->Dupes=dastat.Dupes;
            sn->Total=dastat.TotalTexts;
            sn->FirstTime=dastat.FirstTime;
            sn->LastTime=dastat.LastTime;
            memcpy(&sn->Last8Days[0],&dastat.Last8Days[0],8*sizeof(uint16_t));

            sn->Average=CalculateAverage(&dastat.Last8Days[0],dastat.TotalTexts,DayStatsWritten,sn->FirstTime / (24*60*60));
         }

         if(dastat.FirstTime!=0)
            if(firsttime==0 || firsttime > dastat.FirstTime)
               firsttime=dastat.FirstTime;

         c++;
      }
   }
   else
   {
      while(c<num && osRead(fh,&dastat,sizeof(struct DiskAreaStats))==sizeof(struct DiskAreaStats))
         c++;
   }

   osRead(fh,&num,sizeof(uint32_t));
   c=0;

   if(!args[ARG_NONODES].data)
   {
      while(c<num && osRead(fh,&dnstat,sizeof(struct DiskNodeStats))==sizeof(struct DiskNodeStats))
      {
         if(!(nsn=osAlloc(sizeof(struct NodeStatsNode))))
         {
            printf("Out of memory\n");
            jbFreeList(&NodesList);
            jbFreeList(&StatsList);
            osClose(fh);
            osEnd();
            exit(OS_EXIT_ERROR);
         }

         jbAddNode(&NodesList,(struct jbNode *)nsn);

         Copy4D(&nsn->Node,&dnstat.Node);

         nsn->GotNetmails=dnstat.GotNetmails;
         nsn->GotNetmailBytes=dnstat.GotNetmailBytes;
         nsn->SentNetmails=dnstat.SentNetmails;
         nsn->SentNetmailBytes=dnstat.SentNetmailBytes;
         nsn->GotEchomails=dnstat.GotEchomails;
         nsn->GotEchomailBytes=dnstat.GotEchomailBytes;
         nsn->SentEchomails=dnstat.SentEchomails;
         nsn->SentEchomailBytes=dnstat.SentEchomailBytes;
         nsn->Dupes=dnstat.Dupes;

         nsn->Days=DayStatsWritten-dnstat.FirstTime % (24*60*60);
         if(nsn->Days==0) nsn->Days=1;

         nsn->FirstTime=dnstat.FirstTime;

         if(dnstat.FirstTime!=0)
            if(firsttime==0 || firsttime > dnstat.FirstTime)
               firsttime=dnstat.FirstTime;

         c++;
      }
   }
   else
   {
      while(c<num && osRead(fh,&dnstat,sizeof(struct DiskNodeStats))==sizeof(struct DiskNodeStats))
         c++;
   }

   osClose(fh);

   t=(time_t)DayStatsWritten * 24*60*60;

   tp=localtime(&firsttime);
   sprintf(date,"%02d-%s-%02d",tp->tm_mday,monthnames[tp->tm_mon],tp->tm_year%100);
   
   tp=localtime(&t);
   sprintf(date2,"%02d-%s-%02d",tp->tm_mday,monthnames[tp->tm_mon],tp->tm_year%100);

   printf("\nStatistics from %s to %s\n",date,date2);
   
   if(!ctrlc && !args[ARG_NOAREAS].data)
   {
      Sort(&StatsList,'a');
      Sort(&StatsList,sortmode);
      printf("\n");

      if(args[ARG_LAST7].data)
      {
         printf("Area                             ");

         for(c=1;c<8;c++)
         {
            t=(DayStatsWritten-c)*24*60*60;
            tp=localtime(&t);
            printf("   %02d",tp->tm_mday);
         }

         printf("   Total\n============================================================================\n");

         if(!ctrlc)
         {
            for(sn=(struct StatsNode *)StatsList.First;sn && !ctrlc;sn=sn->Next)
            {
               tot=0;

               for(c=1;c<8;c++)
                  tot+=sn->Last8Days[c];

               printf("%-33.33s %4d %4d %4d %4d %4d %4d %4d : %5d\n",
                  sn->Tagname,
                  sn->Last8Days[1],
                  sn->Last8Days[2],
                  sn->Last8Days[3],
                  sn->Last8Days[4],
                  sn->Last8Days[5],
                  sn->Last8Days[6],
                  sn->Last8Days[7],
                  tot);

               for(c=1;c<8;c++)
                  total8days[c]+=sn->Last8Days[c];

               areas++;
            }

            if(!ctrlc)
            {
               tot=0;

               for(c=1;c<8;c++)
                  tot+=total8days[c];

               printf("=============================================================================\n");
               sprintf(buf,"Totally in all %u areas",areas);

               printf("%-33.33s %4d %4d %4d %4d %4d %4d %4d : %5d\n",
                  buf,
                  total8days[1],
                  total8days[2],
                  total8days[3],
                  total8days[4],
                  total8days[5],
                  total8days[6],
                  total8days[7],
                  tot);
            }
         }
      }
      else
      {
         printf("Area                           First     Last         Msgs  Msgs/day   Dupes\n");
         printf("============================================================================\n");

         if(!ctrlc)
         {
            for(sn=(struct StatsNode *)StatsList.First;sn && !ctrlc;sn=sn->Next)
            {
               if(sn->LastTime==0)
               {
                  strcpy(date2,"<Never>");
               }
               else
               {
                  tp=localtime(&sn->LastTime);
                  sprintf(date2,"%02d-%s-%02d",tp->tm_mday,monthnames[tp->tm_mon],tp->tm_year%100);
               }

               if(sn->FirstTime==0)
               {
                  strcpy(date,"<Never>");
               }
               else
               {
                  tp=localtime(&sn->FirstTime);
                  sprintf(date,"%02d-%s-%02d",tp->tm_mday,monthnames[tp->tm_mon],tp->tm_year%100);
               }

               for(c=0;c<8;c++)
                  total8days[c]+=sn->Last8Days[c];

               total+=sn->Total;
               totaldupes+=sn->Dupes;
               areas++;

               printf("%-29.30s %-9.9s %-9.9s %7d   %7d %7d\n",sn->Tagname,date,date2,sn->Total,sn->Average,sn->Dupes);
            }
         }

         if(!ctrlc)
         {
            printf("============================================================================\n");
            sprintf(buf,"Totally in all %u areas",areas);
            printf("%-42s         %7d   %7d %7d\n",
               buf,
               total,
               CalculateAverage(&total8days[0],total,DayStatsWritten,firsttime / (24*60*60)),
               totaldupes);
         }
      }
   }

   if(!ctrlc && !args[ARG_NONODES].data)
   {
      SortNodes(&NodesList);

      printf("\n");
      printf("Nodes statistics\n");
      printf("================\n");

      for(nsn=(struct NodeStatsNode *)NodesList.First;nsn && !ctrlc;nsn=nsn->Next)
      {
         if(nsn->FirstTime==0)
         {
            strcpy(date,"<Never>");
         }
         else
         {
            tp=localtime(&nsn->FirstTime);
            sprintf(date,"%0d-%s-%0d",tp->tm_mday,monthnames[tp->tm_mon],tp->tm_year%100);
         }

         sprintf(buf,"%u:%u/%u.%u",nsn->Node.Zone,nsn->Node.Net,nsn->Node.Node,nsn->Node.Point);

			printf("%-30.40s Statistics since: %s\n\n",buf,date);
			printf("                                  Sent netmails: %u/%s\n",nsn->SentNetmails,unit(nsn->SentNetmailBytes));
			printf("                              Received netmails: %u/%s\n",nsn->GotNetmails,unit(nsn->GotNetmailBytes));
			printf("                                 Sent echomails: %u/%s\n",nsn->SentEchomails,unit(nsn->SentEchomailBytes));
			printf("                             Received echomails: %u/%s\n",nsn->GotEchomails,unit(nsn->GotEchomailBytes));
			printf("                                          Dupes: %u\n",nsn->Dupes);
         printf("\n");
      }
   }

   if(ctrlc)
   {
      printf("*** Break\n");
   }
   else
   {
      printf("\n");
   }

   jbFreeList(&StatsList);
   jbFreeList(&NodesList);

   osEnd();
   
   exit(OS_EXIT_OK);
}
Example #5
0
bool OpenDupeDB(void)
{
   char buf[300];
   uint32_t offset,crc32,*crc32p;
   uint16_t size,res;

   if(!(dupebuf=osAlloc(config.cfg_DupeSize*sizeof(struct dupeentry))))
   {
      LogWrite(1,SYSTEMERR,"Not enough memory for dupe-check buffer\n");
      return(FALSE);
   }

   dupeentrynum=0;
   dupeentrymax=0;

   dupechanged=FALSE;

   if(!(dupefh=osOpen(config.cfg_DupeFile,MODE_READWRITE)))
   {
		uint32_t err=osError();
      LogWrite(1,SYSTEMERR,"Failed to open dupe file %s in read/write mode",config.cfg_DupeFile);
      LogWrite(1,SYSTEMERR,"Error: %s",osErrorMsg(err));
      return(FALSE);
   }

   res=osRead(dupefh,buf,4);
   buf[4]=0;

   if(res == 0)
   {
      /* New file */

		LogWrite(3,TOSSINGINFO,"Creating new dupe file %s",config.cfg_DupeFile);

      strcpy(buf,DUPES_IDENTIFIER);
      osWrite(dupefh,buf,4);
   }
   else if(res != 4 || strcmp(buf,DUPES_IDENTIFIER)!=0)
   {
      LogWrite(1,SYSTEMERR,"Invalid format of dupe file %s, exiting...",config.cfg_DupeFile);
      osClose(dupefh);
      return(FALSE);
   }

   offset=4;

   while(osRead(dupefh,&size,sizeof(uint16_t))==sizeof(uint16_t))
   {
      if(size == 0 || size > 300) /* Unreasonably big */
      {
         LogWrite(1,SYSTEMERR,"Error in dupe file %s, exiting...",config.cfg_DupeFile);
         osClose(dupefh);
         return(FALSE);
      }

      if(osRead(dupefh,buf,(uint32_t)size) != size)
      {
         LogWrite(1,SYSTEMERR,"Error in dupe file %s, exiting...",config.cfg_DupeFile);
         osClose(dupefh);
         return(FALSE);
      }

      crc32p=(uint32_t *)buf;
      crc32=*crc32p;

      adddupeindex(offset,crc32);

      offset += size+2;
   }

   dupechanged=FALSE;

   return(TRUE);
}
Example #6
0
int main(int argc, const char* argv[])
{
	/* Commandline options */

	std::string sWorkingDir(argv[1]);
	std::cout << " Working directory: " << sWorkingDir << std::endl;
	
	std::string sInReadsFileName(argv[2]);//"parsed");

	std::cout << " Reads file: " << sInReadsFileName << std::endl;

	/* Code stars here */

	std::chrono::time_point<std::chrono::system_clock> t_start,t_end;
	t_start = std::chrono::system_clock::now();

	std::ifstream  fInReadsFile(sWorkingDir+"/"+sInReadsFileName);
	std::string  sOutFileName(sWorkingDir+"/"+sInReadsFileName+"-uSatellites");//

	fInReadsFile.seekg(0,fInReadsFile.end);
	long int iSize = fInReadsFile.tellg();
	fInReadsFile.seekg(0,fInReadsFile.beg);

	std::ofstream osRead(sOutFileName);//

	int iSatelliteCount(0);
	int j(0);

	std::string sMicroSatelliteTemplate("TTT");
	std::map<std::string,int> msStalliteCounts;
	std::string sMicroSatelliteSearch("");

	while(fInReadsFile.good())
	{
		//char cRead = fInReadsFile.get();
		//sMicroSatelliteSearch.push_back(cRead);
		
		fInReadsFile.seekg(j,fInReadsFile.beg);

		for(int i=0;i<sMicroSatelliteTemplate.size();i++)
		{
			char cRead = fInReadsFile.get();
			sMicroSatelliteSearch.push_back(cRead);
		}
		
		j++;

		std::cout << sMicroSatelliteSearch << "\t" << iSatelliteCount << std::endl;
		
		if(sMicroSatelliteTemplate == sMicroSatelliteSearch)
		{
			iSatelliteCount++;	
		}

		sMicroSatelliteSearch.clear();
	}

	fInReadsFile.close();

	t_end = std::chrono::system_clock::now();

	
	std::cout << "Searching " << iSatelliteCount << " ocurrences, took me about\n\t" << std::chrono::duration_cast<std::chrono::milliseconds> (t_end-t_start).count() << " ms" << std::endl;
	std::cout << "\t" << std::chrono::duration_cast<std::chrono::seconds> (t_end-t_start).count() << " s" << std::endl;
	std::cout << "\t" << std::chrono::duration_cast<std::chrono::minutes> (t_end-t_start).count() << " m" << std::endl;
	return 0;
}