<commit_before>#include "gb-include.h"

#include "Msg1f.h"

static void handleRequest ( UdpSlot *slot , long netnice );
#define  LOG_WINDOW 2048



bool Msg1f::init() {
        if ( ! g_udpServer.registerHandler ( 0x1f, handleRequest )) {
		log(LOG_WARN, "db: logview initialization failed"); 
		return false;
	}
	return true;
}


bool Msg1f::getLog(long hostId, 
		   long numBytes, 
		   void *callbackState, 
		   void ( *callback) (void *state, UdpSlot* slot)) {
	
	char* sendBuf = (char*)mmalloc(sizeof(long), "Msg1fA");
	char* p = sendBuf;
	*(long*)p = numBytes;
	p += sizeof(long);
	UdpSlot *slot;
	g_udpServer.sendRequest(sendBuf,
				p - sendBuf,
				0x1f,
				g_hostdb.m_hostPtrs[hostId]->m_ip,
				g_hostdb.m_hostPtrs[hostId]->m_port,
				g_hostdb.m_hostPtrs[hostId]->m_hostId,
				&slot,
				callbackState,
				callback,
				5);

	return false;
}


void handleRequest ( UdpSlot *slot , long netnice ) {
	char *p = slot->m_readBuf;

	long numBytes = *(long*)p;
	p += sizeof(long);


	char *filename = g_hostdb.m_logFilename;

	// running just ./gb will log to stderr...
	if ( strcmp(filename ,"/dev/stderr") == 0 )
		return;

	long fd = open ( filename , O_RDONLY,
			 S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP| S_IROTH );
	if ( ! fd ) {
		log(LOG_DEBUG, "logviewer: Failed to open %s for reading: ",
		    filename);
		g_errno = EBADFILE;
		g_udpServer.sendErrorReply ( slot, g_errno ); 
		return;
	}

	char stackSpace[LOG_WINDOW];
	char *buf = stackSpace;
	char *allocBuf = NULL;
	long allocBufSize = 0;

	if(numBytes > LOG_WINDOW) {
		buf = (char*)mmalloc(numBytes, "Msg1fA");
		if(!buf) {
			log(LOG_INFO, 
			    "admin: malloc of %li bytes failed "
			    "for logview,"
			    " falling back on stack buffer.",
			    numBytes);
			buf = stackSpace;
			numBytes = LOG_WINDOW;
		}
		else {
			allocBuf     = buf;
			allocBufSize = numBytes;
		}
	}

	lseek(fd, -1 * numBytes, SEEK_END);
	if(errno == EINVAL) {
		//oops! we seeked to before the begining of the file
		//log(LOG_WARN, "bad seek!");
		lseek(fd, 0, SEEK_SET);
	}

	long numRead = read(fd, buf, numBytes-1);
	close(fd);
	if(numRead > 0)	buf[numRead-1] = '\0';
	else          {  
		buf[0] = '\0'; 
		numRead = 0;
		if(allocBuf) mfree(allocBuf,	allocBufSize, "Msg1fA");
		allocBufSize = 0;
		allocBuf = NULL;
		g_udpServer.sendErrorReply ( slot, EBADFILE ); 
		return;
	}
	//log(LOG_DEBUG, "bytes read! %li ", numRead);

	g_udpServer.sendReply_ass (buf, numRead, allocBuf,allocBufSize, slot); //send
}
<commit_msg>if logging to stderr then return err when trying to fetch logs.<commit_after>#include "gb-include.h"

#include "Msg1f.h"

static void handleRequest ( UdpSlot *slot , long netnice );
#define  LOG_WINDOW 2048



bool Msg1f::init() {
        if ( ! g_udpServer.registerHandler ( 0x1f, handleRequest )) {
		log(LOG_WARN, "db: logview initialization failed"); 
		return false;
	}
	return true;
}


bool Msg1f::getLog(long hostId, 
		   long numBytes, 
		   void *callbackState, 
		   void ( *callback) (void *state, UdpSlot* slot)) {
	
	char* sendBuf = (char*)mmalloc(sizeof(long), "Msg1fA");
	char* p = sendBuf;
	*(long*)p = numBytes;
	p += sizeof(long);
	UdpSlot *slot;
	g_udpServer.sendRequest(sendBuf,
				p - sendBuf,
				0x1f,
				g_hostdb.m_hostPtrs[hostId]->m_ip,
				g_hostdb.m_hostPtrs[hostId]->m_port,
				g_hostdb.m_hostPtrs[hostId]->m_hostId,
				&slot,
				callbackState,
				callback,
				5);

	return false;
}


void handleRequest ( UdpSlot *slot , long netnice ) {
	char *p = slot->m_readBuf;

	long numBytes = *(long*)p;
	p += sizeof(long);


	char *filename = g_hostdb.m_logFilename;

	// running just ./gb will log to stderr...
	if ( strcmp(filename ,"/dev/stderr") == 0 ) {
		g_errno = EBADFILE;
		g_udpServer.sendErrorReply ( slot, g_errno ); 
		return;
	}

	long fd = open ( filename , O_RDONLY,
			 S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP| S_IROTH );
	if ( ! fd ) {
		log(LOG_DEBUG, "logviewer: Failed to open %s for reading: ",
		    filename);
		g_errno = EBADFILE;
		g_udpServer.sendErrorReply ( slot, g_errno ); 
		return;
	}

	char stackSpace[LOG_WINDOW];
	char *buf = stackSpace;
	char *allocBuf = NULL;
	long allocBufSize = 0;

	if(numBytes > LOG_WINDOW) {
		buf = (char*)mmalloc(numBytes, "Msg1fA");
		if(!buf) {
			log(LOG_INFO, 
			    "admin: malloc of %li bytes failed "
			    "for logview,"
			    " falling back on stack buffer.",
			    numBytes);
			buf = stackSpace;
			numBytes = LOG_WINDOW;
		}
		else {
			allocBuf     = buf;
			allocBufSize = numBytes;
		}
	}

	lseek(fd, -1 * numBytes, SEEK_END);
	if(errno == EINVAL) {
		//oops! we seeked to before the begining of the file
		//log(LOG_WARN, "bad seek!");
		lseek(fd, 0, SEEK_SET);
	}

	long numRead = read(fd, buf, numBytes-1);
	close(fd);
	if(numRead > 0)	buf[numRead-1] = '\0';
	else          {  
		buf[0] = '\0'; 
		numRead = 0;
		if(allocBuf) mfree(allocBuf,	allocBufSize, "Msg1fA");
		allocBufSize = 0;
		allocBuf = NULL;
		g_udpServer.sendErrorReply ( slot, EBADFILE ); 
		return;
	}
	//log(LOG_DEBUG, "bytes read! %li ", numRead);

	g_udpServer.sendReply_ass (buf, numRead, allocBuf,allocBufSize, slot); //send
}

{'fraction_non_alphanumeric': 0.1417910447761194, 'fraction_numerical': 0.010604870384917517, 'mean_word_length': 3.287037037037037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4272350', 'n_tokens_mistral': 2244, 'n_tokens_neox': 1998, 'n_words': 466}