<commit_before>/* (C)opyright MMIV-MMVI <NAME> <<EMAIL>>
 * See LICENSE file for license details.
 */
#include "ixp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

int
ixp_client_do_fcall(IXPClient *c) {
	static uchar msg[IXP_MAX_MSG];
	uint msize = ixp_fcall2msg(msg, &c->ifcall, IXP_MAX_MSG);

	c->errstr = 0;
	if(ixp_send_message(c->fd, msg, msize, &c->errstr) != msize)
		return -1;
	if(!ixp_recv_message(c->fd, msg, IXP_MAX_MSG, &c->errstr))
		return -1;
	if(!(msize = ixp_msg2fcall(&c->ofcall, msg, IXP_MAX_MSG))) {
		c->errstr = "received bad message";
		return -1;
	}
	if(c->ofcall.type == RERROR) {
		c->errstr = c->ofcall.ename;
		return -1;
	}
	return 0;
}

int
ixp_client_dial(IXPClient *c, char *sockfile, uint rootfid) {
	if((c->fd = ixp_connect_sock(sockfile)) < 0) {
		c->errstr = "cannot connect server";
		return -1;
	}
	c->ifcall.type = TVERSION;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.msize = IXP_MAX_MSG;
	c->ifcall.version = IXP_VERSION;
	if(ixp_client_do_fcall(c) == -1) {
		fprintf(stderr, "error: %s\n", c->errstr);
		ixp_client_hangup(c);
		return -1;
	}
	if(strncmp(c->ofcall.version, IXP_VERSION, strlen(IXP_VERSION))) {
		fprintf(stderr, "error: %s\n", c->errstr);
		c->errstr = "9P versions differ";
		ixp_client_hangup(c);
		return -1;	/* we cannot handle this version */
	}
	free(c->ofcall.version);
	c->root_fid = rootfid;
	c->ifcall.type = TATTACH;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = c->root_fid;
	c->ifcall.afid = IXP_NOFID;
	c->ifcall.uname = getenv("USER");
	c->ifcall.aname = "";
	if(ixp_client_do_fcall(c) == -1) {
		fprintf(stderr, "error: %s\n", c->errstr);
		ixp_client_hangup(c);
		return -1;
	}
	c->root_qid = c->ofcall.qid;
	return 0;
}

int
ixp_client_remove(IXPClient *c, uint newfid, char *filepath) {
	if(ixp_client_walk(c, newfid, filepath) == -1)
		return -1;
	c->ifcall.type = TREMOVE;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = newfid;
	return ixp_client_do_fcall(c);
}

int
ixp_client_create(IXPClient *c, uint dirfid, char *name,
		uint perm, uchar mode)
{
	c->ifcall.type = TCREATE;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = dirfid;
	c->ifcall.name = name;
	c->ifcall.perm = perm;
	c->ifcall.mode = mode;
	return ixp_client_do_fcall(c);
}

int
ixp_client_walk(IXPClient *c, uint newfid, char *filepath) {
	uint i;
	char *wname[IXP_MAX_WELEM], *fp;

	c->ifcall.type = TWALK;
	c->ifcall.fid = c->root_fid;
	c->ifcall.newfid = newfid;
	if(filepath) {
		// c->ifcall.name = filepath; // tcreate overlaps with twalk !!!
		fp = ixp_estrdup(filepath);
		c->ifcall.nwname = ixp_tokenize(wname, IXP_MAX_WELEM, fp, '/'); // was "c->ifcall.name"
		for(i = 0; i < c->ifcall.nwname; i++)
			c->ifcall.wname[i] = wname[i];
		free(fp);
	}
	return ixp_client_do_fcall(c);
}

int
ixp_client_stat(IXPClient *c, uint newfid, char *filepath) {
	if(ixp_client_walk(c, newfid, filepath) == -1)
		return -1;
	c->ifcall.type = TSTAT;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = newfid;
	return ixp_client_do_fcall(c);
}

int
ixp_client_open(IXPClient *c, uint newfid, uchar mode) {
	c->ifcall.type = TOPEN;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = newfid;
	c->ifcall.mode = mode;
	return ixp_client_do_fcall(c);
}

int<commit_msg>Free data after you use it, not before.<commit_after>/* (C)opyright MMIV-MMVI <NAME> <<EMAIL>>
 * See LICENSE file for license details.
 */
#include "ixp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

int
ixp_client_do_fcall(IXPClient *c) {
	static uchar msg[IXP_MAX_MSG];
	uint msize = ixp_fcall2msg(msg, &c->ifcall, IXP_MAX_MSG);

	c->errstr = 0;
	if(ixp_send_message(c->fd, msg, msize, &c->errstr) != msize)
		return -1;
	if(!ixp_recv_message(c->fd, msg, IXP_MAX_MSG, &c->errstr))
		return -1;
	if(!(msize = ixp_msg2fcall(&c->ofcall, msg, IXP_MAX_MSG))) {
		c->errstr = "received bad message";
		return -1;
	}
	if(c->ofcall.type == RERROR) {
		c->errstr = c->ofcall.ename;
		return -1;
	}
	return 0;
}

int
ixp_client_dial(IXPClient *c, char *sockfile, uint rootfid) {
	if((c->fd = ixp_connect_sock(sockfile)) < 0) {
		c->errstr = "cannot connect server";
		return -1;
	}
	c->ifcall.type = TVERSION;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.msize = IXP_MAX_MSG;
	c->ifcall.version = IXP_VERSION;
	if(ixp_client_do_fcall(c) == -1) {
		fprintf(stderr, "error: %s\n", c->errstr);
		ixp_client_hangup(c);
		return -1;
	}
	if(strncmp(c->ofcall.version, IXP_VERSION, strlen(IXP_VERSION))) {
		fprintf(stderr, "error: %s\n", c->errstr);
		c->errstr = "9P versions differ";
		ixp_client_hangup(c);
		return -1;	/* we cannot handle this version */
	}
	free(c->ofcall.version);
	c->root_fid = rootfid;
	c->ifcall.type = TATTACH;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = c->root_fid;
	c->ifcall.afid = IXP_NOFID;
	c->ifcall.uname = getenv("USER");
	c->ifcall.aname = "";
	if(ixp_client_do_fcall(c) == -1) {
		fprintf(stderr, "error: %s\n", c->errstr);
		ixp_client_hangup(c);
		return -1;
	}
	c->root_qid = c->ofcall.qid;
	return 0;
}

int
ixp_client_remove(IXPClient *c, uint newfid, char *filepath) {
	if(ixp_client_walk(c, newfid, filepath) == -1)
		return -1;
	c->ifcall.type = TREMOVE;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = newfid;
	return ixp_client_do_fcall(c);
}

int
ixp_client_create(IXPClient *c, uint dirfid, char *name,
		uint perm, uchar mode)
{
	c->ifcall.type = TCREATE;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = dirfid;
	c->ifcall.name = name;
	c->ifcall.perm = perm;
	c->ifcall.mode = mode;
	return ixp_client_do_fcall(c);
}

int
ixp_client_walk(IXPClient *c, uint newfid, char *filepath) {
	int i;
	char *wname[IXP_MAX_WELEM], *fp;

	c->ifcall.type = TWALK;
	c->ifcall.fid = c->root_fid;
	c->ifcall.newfid = newfid;
	if(filepath) {
		// c->ifcall.name = filepath; // tcreate overlaps with twalk !!!
		fp = ixp_estrdup(filepath);
		c->ifcall.nwname = ixp_tokenize(wname, IXP_MAX_WELEM, fp, '/'); // was "c->ifcall.name"
		for(i = 0; i < c->ifcall.nwname; i++)
			c->ifcall.wname[i] = wname[i];
	}
	i = ixp_client_do_fcall(c);
	free(fp);
	return i;
}

int
ixp_client_stat(IXPClient *c, uint newfid, char *filepath) {
	if(ixp_client_walk(c, newfid, filepath) == -1)
		return -1;
	c->ifcall.type = TSTAT;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = newfid;
	return ixp_client_do_fcall(c);
}

int
ixp_client_open(IXPClient *c, uint newfid, uchar mode) {
	c->ifcall.type = TOPEN;
	c->ifcall.tag = IXP_NOTAG;
	c->ifcall.fid = newfid;
	c->ifcall.mode = mode;
	return ixp_client_do_fcall(c);
}

int
{'fraction_non_alphanumeric': 0.17835365853658536, 'fraction_numerical': 0.006707317073170732, 'mean_word_length': 4.158018867924528, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 137, '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': '839904', 'n_tokens_mistral': 3150, 'n_tokens_neox': 3072, 'n_words': 564}