Exemple #1
0
void getusers_handler(int argc,char**argv,int bloblen,void*blob)
{
        dbResult*res;
        int i,l;
        char rsp[64];
        struct dynbuf_s*buf;
        if(!checkGlobalACL(ACL_ADMUSER)){
                bc_hdl->sockwriter(bc_hdl,E_PERM,strlen(E_PERM));
                return;
        }
        res=query(bc_con,SQL_GETUSERS);
        if(!res){
                bc_hdl->sockwriter(bc_hdl,E_DATABASE,strlen(E_DATABASE));
                return;
        }
        buf=dynbuf_new();
        if(!buf){
                bc_hdl->sockwriter(bc_hdl,E_ALLOCATION,strlen(E_ALLOCATION));
                dbFree(res);
                return;
        }
        l=dbNumRows(res);
        for(i=0;i<l;i++){
                dynbuf_addstr(buf,dbGetStringByname(res,i,"usrname"));
                dynbuf_addstr(buf,"\n");
        }
        dbFree(res);
        sprintf(rsp,"+%i Users:\n",buf->len);
        bc_hdl->sockwriter(bc_hdl,rsp,strlen(rsp));
        bc_hdl->sockwriter(bc_hdl,buf->buffer,buf->len);
        dynbuf_free(buf);
}
Exemple #2
0
void getuseracl_handler(int argc,char**argv,int bloblen,void*blob)
{
        dbResult*res;
        char*acl;
        struct dynbuf_s*buf;
        if(!checkGlobalACL(ACL_ADMUSER)){
                bc_hdl->sockwriter(bc_hdl,E_PERM,strlen(E_PERM));
                return;
        }
        /*invalid argc*/
        if(argc!=2){
                bc_hdl->sockwriter(bc_hdl,E_SYNTAX,strlen(E_SYNTAX));
                return;
        }
        res=query(bc_con,SQL_GETMAINACL,argv[1]);
        if(!res){
                bc_hdl->sockwriter(bc_hdl,E_DATABASE,strlen(E_DATABASE));
                return;
        }
        if(dbNumRows(res)!=1){
                bc_hdl->sockwriter(bc_hdl,E_NOUSER,strlen(E_NOUSER));
                dbFree(res);
                return;
        }
        buf=dynbuf_new();
        if(!buf){
                bc_hdl->sockwriter(bc_hdl,E_ALLOCATION,strlen(E_ALLOCATION));
                dbFree(res);
                return;
        }
        acl=dbGetString(res,0,0);
        if(!acl)acl="";

        if(strchr(acl,ACL_ADMIN))dynbuf_addstr(buf," admin");
        if(strchr(acl,ACL_READ))dynbuf_addstr(buf," read");
        if(strchr(acl,ACL_WRITE))dynbuf_addstr(buf," write");
        if(strchr(acl,ACL_CONFIG))dynbuf_addstr(buf," config");
        if(strchr(acl,ACL_ADMUSER))dynbuf_addstr(buf," crateUsers");
        if(strchr(acl,ACL_ADMPROJECT))dynbuf_addstr(buf,"createProjects");

        dbFree(res);

        dynbuf_addstr(buf,"\n");

        bc_hdl->sockwriter(bc_hdl,"+0",2);
        bc_hdl->sockwriter(bc_hdl,buf->buffer,buf->len);
        dynbuf_free(buf);
}
Exemple #3
0
/*
 * NAME:	free_ea_wmap(inode)
 *
 * FUNCTION:	free uncommitted extended attributes from working map
 *
 */
static inline void free_ea_wmap(struct inode *inode)
{
    dxd_t *ea = &JFS_IP(inode)->ea;

    if (ea->flag & DXD_EXTENT) {
        /* free EA pages from cache */
        invalidate_dxd_metapages(inode, *ea);
        dbFree(inode, addressDXD(ea), lengthDXD(ea));
    }
    ea->flag = 0;
}
Exemple #4
0
void dbFree(Node db){
  if (db != NULL){
    if(db->next == NULL){
      free(db->key);
      free(db->value);
      free(db);
    }   
    dbFree(db->next);
    free(db->key);
    free(db->value);
    free(db);
  }
}
Exemple #5
0
int main()
{
	FILE *fp;
	char dict[][50] = {"ChineseFamilyNames","CountryNames","FemaleFirstNames",
			"FuncWord","MaleFirstNames","Months","PlaceNames","PublisherNames","CityNames"};
	char valdict[50] = "LastNames";
	char fileName[1024];
	char data[1024];
	char name[1024];
	double val;
	dbConnect();
	
	dbLock();
	dbCreateTable();
	int i;
	for(i=0;i<9;i++)
	{
		sprintf(fileName,"res/%s",dict[i]);
		fp = fopen(fileName,"r");
		while(fgets(data,1024,fp))
		{
			dbAddNameDict(dict[i],removeCR(data));
			//printf("[%s]\n",removeCR(data));
		}
		fclose(fp);
	}
	sprintf(fileName,"res/%s",valdict);
	fp = fopen(fileName,"r");
	while(fgets(data,1024,fp))
	{
		spilitVal(name,&val,data);
		dbAddValDict(valdict,name,val);
		//printf("[%s]|[%f]\n",name,val);
	}
	dbUnlock();
	fclose(fp);
	
	//////////////////////////////////////////////////////////////
	// boolean
	printf("CityNames,abidjan:%d\n",dbGetNameLikeDict("CityNames","abigail"));
	printf("FemaleFirstNames,abigail:%d\n",dbGetNameDict("FemaleFirstNames","abigail"));
	printf("FemaleFirstNames,Abigail:%d\n",dbGetNameLikeDict("FemaleFirstNames","Abigail"));
	// double
	printf("LastNames,wilson:%f\n",dbGetValDict("LastNames","wilson"));
	printf("LastNames,Wilson:%f\n",dbGetValLikeDict("LastNames","Wilson"));
	//////////////////////////////////////////////////////////////
	
	dbFree();
	
	return 0;
}
Exemple #6
0
int checkGlobalACL(char cap)
{
        dbResult *res;
        char *acl;

        res=query(bc_con,SQL_GETMAINACL,username);

        if(!res)return 0;
        if(dbNumRows(res)!=1){
                dbFree(res);
                return 0;
        }

        acl=dbGetString(res,0,0);
        if(!acl)acl="";

        if(strchr(acl,cap)||strchr(acl,'A')){
                dbFree(res);
                return 1;
        }else{
                dbFree(res);
                return 0;
        }
}
char *fetchHelp(int getThis, int getType, struct threadInfo * ti) {
	int (*dbConnect)(struct threadStorageInfo *);
	void (*dbDisconnect)(struct threadStorageInfo *);
	void (*dbEscape)(struct threadStorageInfo *, char *, char *);
	void (*dbPush)(struct threadStorageInfo *, char *);
	char *(*dbPull)(struct threadStorageInfo *, char *);
	void (*dbFree)(struct threadStorageInfo *);

	dbConnect = ti->dbInfo.connect;
	dbDisconnect = ti->dbInfo.disconnect;
	dbEscape = ti->dbInfo.escape;
	dbPush = ti->dbInfo.push;
	dbPull = ti->dbInfo.pull;
	dbFree = ti->dbInfo.free;

	ti->commandInfo.esc1Buffer = NULL;
	ti->commandInfo.esc2Buffer = NULL;
	ti->commandInfo.esc3Buffer = NULL;
	ti->commandInfo.esc4Buffer = NULL;
	ti->commandInfo.esc5Buffer = NULL;

	// Allocate memory for buffers
	ti->commandInfo.s =
		(ti->handlerArrays[HANDLER_ARRAY_UID].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 8) +
		(ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 8) +
		CONFIG_SPACE_SIZE;

	if((ti->commandInfo.statBuffer = malloc(ti->commandInfo.s + 1)) == NULL) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

		return(ti->dataBuffer);
	}

	// Escape parameters received from the client
	if(ti->handlerArrays[HANDLER_ARRAY_UID].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_UID].buffer[0] != 0) {
		if((ti->commandInfo.esc1Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc1Buffer, ti->handlerArrays[HANDLER_ARRAY_UID].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer[0] != 0) {
		if((ti->commandInfo.esc2Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc2Buffer, ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer[0] != 0) {
		if((ti->commandInfo.esc3Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc3Buffer, ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] != 0) {
		if((ti->commandInfo.esc4Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc4Buffer, ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer[0] != 0) {
		if((ti->commandInfo.esc5Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);
			free(ti->commandInfo.esc4Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc5Buffer, ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer);
	}

	// Connect to database
	if(dbConnect(&ti->storageInfo) != 0) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_CONNECTFAILED, ERROR_MESS_DATABASE_CONNECTFAILED, ti);
	}
	else {
		// Construct SQL statement for requested item
		switch(getThis) {
			case 1:
				// List all help pages
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_HELP_HELP " FROM " TABLE_HELP " ORDER BY " TABLEORD_HELP_HELP "%c",
					0
				);

				break;
			case 2:
				// Get requested help page
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_HELP_HELP " FROM " TABLE_HELP " WHERE " TABLECOL_HELP_NAME " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 3:
				// Search from help pages
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_HELP_HELP " FROM " TABLE_HELP " WHERE " TABLECOL_HELP_TITLE " LIKE '%%%s%%' OR " TABLECOL_HELP_HELP " LIKE '%%%s%%' OR " TABLECOL_HELP_DESCR " LIKE '%%%s%%' OR " TABLECOL_HELP_NOTE " LIKE '%%%s%%' ORDER BY " TABLECOL_HELP_TITLE "%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		// Pull data out from database
		switch(getType) {
			case QUERY_TYPE_PULL:
				if(dbPull(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			case QUERY_TYPE_PUSH:
				dbPush(&ti->storageInfo, ti->commandInfo.statBuffer);

				if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer == NULL || ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] == 0 || ti->handlerArrays[HANDLER_ARRAY_PARAM].size == 0) {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Help \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}
				else {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Help \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}

				logMessage(ti->logSpace);

				replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		dbDisconnect(&ti->storageInfo);
	}

	// Free allocated resources and reply to the client
	if(ti->commandInfo.esc1Buffer != NULL) {
		free(ti->commandInfo.esc1Buffer);
	}

	if(ti->commandInfo.esc2Buffer != NULL) {
		free(ti->commandInfo.esc2Buffer);
	}

	if(ti->commandInfo.esc3Buffer != NULL) {
		free(ti->commandInfo.esc3Buffer);
	}

	if(ti->commandInfo.esc4Buffer != NULL) {
		free(ti->commandInfo.esc4Buffer);
	}

	if(ti->commandInfo.esc5Buffer != NULL) {
		free(ti->commandInfo.esc5Buffer);
	}

	free(ti->commandInfo.statBuffer);

	return(ti->dataBuffer);
}
char *fetchDevice(int getThis, int getType, struct threadInfo * ti) {
	int (*dbConnect)(struct threadStorageInfo *);
	void (*dbDisconnect)(struct threadStorageInfo *);
	void (*dbEscape)(struct threadStorageInfo *, char *, char *);
	void (*dbPush)(struct threadStorageInfo *, char *);
	char *(*dbPull)(struct threadStorageInfo *, char *);
	char *(*dbRound)(struct threadStorageInfo *, char *);
	void (*dbFree)(struct threadStorageInfo *);

	dbConnect = ti->dbInfo.connect;
	dbDisconnect = ti->dbInfo.disconnect;
	dbEscape = ti->dbInfo.escape;
	dbPush = ti->dbInfo.push;
	dbPull = ti->dbInfo.pull;
	dbRound = ti->dbInfo.round;
	dbFree = ti->dbInfo.free;

	ti->commandInfo.esc1Buffer = NULL;
	ti->commandInfo.esc2Buffer = NULL;
	ti->commandInfo.esc3Buffer = NULL;
	ti->commandInfo.esc4Buffer = NULL;
	ti->commandInfo.esc5Buffer = NULL;

	// Allocate memory for buffers
	ti->commandInfo.s =
		(ti->handlerArrays[HANDLER_ARRAY_UID].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 18) +
		(ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 18) +
		CONFIG_SPACE_SIZE;

	if((ti->commandInfo.statBuffer = malloc(ti->commandInfo.s + 1)) == NULL) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

		return(ti->dataBuffer);
	}

	// Escape parameters received from the client
	if(ti->handlerArrays[HANDLER_ARRAY_UID].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_UID].buffer[0] != 0) {
		if((ti->commandInfo.esc1Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc1Buffer, ti->handlerArrays[HANDLER_ARRAY_UID].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer[0] != 0) {
		if((ti->commandInfo.esc2Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc2Buffer, ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer[0] != 0) {
		if((ti->commandInfo.esc3Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc3Buffer, ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] != 0) {
		if((ti->commandInfo.esc4Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc4Buffer, ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer[0] != 0) {
		if((ti->commandInfo.esc5Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);
			free(ti->commandInfo.esc4Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc5Buffer, ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer);
	}

	// Connect to database
	if(dbConnect(&ti->storageInfo) != 0) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_CONNECTFAILED, ERROR_MESS_DATABASE_CONNECTFAILED, ti);
	}
	else {
		// Construct SQL statement for requested machine
		switch(getThis) {
			case 1:
				// List all devices
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '0' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 2:
				// List requested devices by category
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '0' AND " TABLECOL_DEVICE_CATEGORY " = '%s' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 3:
				// Get requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_DEVICE_DATA " FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 4:
				// Search from devices
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLEKEY_DEVICE_SEARCH " FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_MODEL " LIKE '%%%s%%' OR " TABLECOL_DEVICE_CATEGORY " LIKE '%%%s%%' OR " TABLECOL_DEVICE_MANUFACTURER " LIKE '%%%s%%' OR " TABLECOL_DEVICE_SIZE " LIKE '%%%s%%' OR " TABLECOL_DEVICE_IPADDRESS " LIKE '%%%s%%' OR " TABLECOL_DEVICE_SERNUM " LIKE '%%%s%%' OR " TABLECOL_DEVICE_SECNUM " LIKE '%%%s%%' OR " TABLECOL_DEVICE_LOCATION " LIKE '%%%s%%' OR " TABLECOL_DEVICE_MOSD " LIKE '%%%s%%' ORDER BY " TABLEORD_DEVICE_NAME "%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 5:
				// Create new device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE " (" TABLEKEY_DEVICE_NEW ") VALUES('%s', '%s', '%s', '%s')%c",
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 6:
				// Modify existing device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET %s = '%s' WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc5Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 7:
				// Modify existing device timestamp
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET " TABLECOL_DEVICE_MODIFIED " = NOW(), " TABLECOL_DEVICE_MODIFIER " = '%s' WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 8:
				// Delete existing device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET " TABLECOL_DEVICE_DISPOSED " = " TABLECOL_DEVICE_DISPOSED " + 1 WHERE " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 9:
				// Restore disposed device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_DEVICE " SET " TABLECOL_DEVICE_DISPOSED " = '0' WHERE " TABLECOL_DEVICE_DISPOSED " = '1' AND " TABLECOL_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 10:
				// Attach device to requested machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_MACHINE_ID ") VALUES('%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 11:
				// Remove device from requested machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_MACHINE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 12:
				// Get devices attached in machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_DEVICE_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_MACHINE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 13:
				// Get machines attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_MACHINE_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 14:
				// List all disposed devices
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '1' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 15:
				// List requested disposed devices by type
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_ID ",%s FROM " TABLE_DEVICE " WHERE " TABLECOL_DEVICE_DISPOSED " = '1' AND " TABLECOL_DEVICE_CATEGORY " = '%s' ORDER BY %s%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 16:
				// Attach device to requested domain
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_MACHINE_ID ") (SELECT '%s', " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DOMAIN " = '%s' AND " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 17:
				// Attach device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_MACHINE_ID ") (SELECT '%s', " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 18:
				// Remove device to requested domain
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_MACHINE_ID " IN (SELECT " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DOMAIN " = '%s' AND " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 19:
				// Remove device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_MACHINE_ID " IN (SELECT " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 20:
				// Attach device to requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_DEVICES_ID ") VALUES('%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 21:
				// Attach device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICES_ID ", " TABLECOL_DEVICE_MAP_DEVICE_ID ") (SELECT '%s', " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 22:
				// Get devices attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_DEVICES_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 23:
				// Get peripherals attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 24:
				// Remove device from requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_DEVICES_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 25:
				// Remove device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICES_ID " = '%s' AND " TABLECOL_DEVICE_MAP_DEVICE_ID " IN (SELECT " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 26:
				// Attach device to requested peripheral
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICE_ID ", " TABLECOL_DEVICE_MAP_PERIPHERALS_ID ") VALUES('%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 27:
				// Attach device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_PERIPHERALS_ID ", " TABLECOL_DEVICE_MAP_DEVICE_ID ") (SELECT '%s', " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 28:
				// Get devices attached in peripheral
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 29:
				// Get peripherals attached in device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_DEVICE_MAP_DEVICES_ID " FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 30:
				// Remove device from requested peripheral
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_DEVICE_ID " = '%s' AND " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 31:
				// Remove device globally
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_PERIPHERALS_ID " = '%s' AND " TABLECOL_DEVICE_MAP_DEVICE_ID " IN (SELECT " TABLECOL_DEVICE_ID " FROM " TABLE_DEVICE ")%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 32:
				// Clone device attachments for machine
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_DEVICE_MAP " (" TABLECOL_DEVICE_MAP_DEVICES ") SELECT " TABLECOL_DEVICE_MAP_DEVICE_ID ",'%s','0','0' FROM " TABLE_DEVICE_MAP " WHERE " TABLECOL_DEVICE_MAP_MACHINE_ID " IN (SELECT " TABLECOL_MACHINE_NID " FROM " TABLECOL_MACHINE_NODE " WHERE " TABLECOL_MACHINE_NODE " = '%s' AND " TABLECOL_MACHINE_DOMAIN " = '%s' AND " TABLECOL_MACHINE_DISPOSED " = '0')%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc3Buffer,
					0
				);

				break;
			case 33:
				// Add note to requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_NOTES " (" TABLEKEY_NOTES_DEVICE ") VALUES('%s', '%s', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 34:
				// Pull notes from requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT " TABLEKEY_NOTES_DATA " FROM " TABLE_NOTES " WHERE " TABLECOL_NOTES_DEVICE " = '%s' ORDER BY " TABLEORD_NOTES_CREATED "%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 35:
				// Update note for requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_NOTES " SET " TABLECOL_NOTES_MODIFIED " = NOW(), " TABLECOL_NOTES_MODIFIER " = '%s', " TABLECOL_NOTES_NOTE " = '%s' WHERE " TABLECOL_NOTES_ID " = '%s' AND " TABLECOL_NOTES_CREATOR " = '%s'%c",
					ti->commandInfo.esc1Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 36:
				// Delete note for requested device
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_NOTES " WHERE " TABLECOL_NOTES_ID " = '%s' AND " TABLECOL_NOTES_CREATOR " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 37:
				// Pull note from requested device by id
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT " TABLEKEY_NOTES_DATA " FROM " TABLE_NOTES " WHERE " TABLECOL_NOTES_DEVICE " = '%s' AND " TABLECOL_NOTES_ID " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		// Pull data out from database
		switch(getType) {
			case QUERY_TYPE_PULL:
				if(dbPull(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			case QUERY_TYPE_PUSH:
				dbPush(&ti->storageInfo, ti->commandInfo.statBuffer);

				if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer == NULL || ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] == 0 || ti->handlerArrays[HANDLER_ARRAY_PARAM].size == 0) {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Device \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}
				else {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Device \"%s\" modified by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);
				}

				logMessage(ti->logSpace);

				replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

				break;
			case QUERY_TYPE_ROUND:
				if(dbRound(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					snprintf(
						ti->logSpace,
						sizeof(ti->logSpace),
						"Device \"%s\" created by \"%s\" using command \"%s\"%c",
						ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer,
						ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
						ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
						0
					);

					logMessage(ti->logSpace);

					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		dbDisconnect(&ti->storageInfo);
	}

	// Free allocated resources and reply to the client
	if(ti->commandInfo.esc1Buffer != NULL) {
		free(ti->commandInfo.esc1Buffer);
	}

	if(ti->commandInfo.esc2Buffer != NULL) {
		free(ti->commandInfo.esc2Buffer);
	}

	if(ti->commandInfo.esc3Buffer != NULL) {
		free(ti->commandInfo.esc3Buffer);
	}

	if(ti->commandInfo.esc4Buffer != NULL) {
		free(ti->commandInfo.esc4Buffer);
	}

	if(ti->commandInfo.esc5Buffer != NULL) {
		free(ti->commandInfo.esc5Buffer);
	}

	free(ti->commandInfo.statBuffer);

	return(ti->dataBuffer);
}
Exemple #9
0
/*
 * NAME:        jfs_defragfs()
 *
 * FUNCTION:    relocate specified extent for defragfs()
 *              optionally commiting the operation.
 */
int32 jfs_defragfs(
	char	*pData,		/* pointer to buffer containing plist */ 
	uint32	lenData,	/* length of buffer */
	uint16	*pbufsize)	/* pointer of buffer length */ 
{
	int32	rc = 0;
	defragfs_t	pList, *p = &pList;
	uint32	xtype;
	int64	offset, xoff, oxaddr, nxaddr;
	int32	xlen;
	inode_t	*ipmnt, *ipimap, *ipbmap;
	inode_t	*ip = NULL;
	xad_t	oxad;
	pxd_t	opxd;
        mode_t	imode;
        int32	tid;
        inode_t	*iplist[1];
	struct vfs *vfsp;

	if (rc = KernCopyIn(&pList, pData, lenData))
		return rc;

	/* get the 'mount' inode */
        for (vfsp = vfs_anchor; vfsp != NULL; vfsp = vfsp->vfs_next)
		if (vfsp->vfs_vpfsi->vpi_drive == pList.dev)
			break;
	if (vfsp == NULL)
		return EINVAL;

	xtype = pList.flag;

	/* sync at start of defragfs ? */
	if (xtype & DEFRAGFS_SYNC)
	{
jEVENT(0,("jfs_defragfs: DEFRAGFS_SYNC\n"));

		if ((vfsp->vfs_flag & VFS_READONLY) ||
		    (vfsp->vfs_flag & VFS_ACCEPT))
			return 0;

		ipimap = (inode_t *)vfsp->vfs_data;
		ipmnt = ipimap->i_ipmnt;
		ipbmap = ipmnt->i_ipbmap;

		/* sync the file system */
		iSyncFS(vfsp);

		/* write dirty pages of imap */
		diSync(ipimap);

		/* write dirty pages of bmap */
		dbSync(ipbmap);

		return 0;
	}
	else if (!(xtype & DEFRAGFS_RELOCATE))
		return EINVAL;

	if (vfsp->vfs_flag & VFS_READONLY)
		return EROFS;

	if (vfsp->vfs_flag & VFS_ACCEPT)
		return EINVAL;

	/* get the relocation parameter */
        xoff = pList.xoff;
        oxaddr = pList.old_xaddr;
        nxaddr = pList.new_xaddr;
        xlen = pList.xlen;

jEVENT(0,("jfs_defragfs: i:%d xtype:0x%08x xoff:%lld xlen:%d xaddr:%lld:%lld\n",
	pList.ino, xtype, xoff, xlen, oxaddr, nxaddr));

	/* get the object inode if it exist */
	ICACHE_LOCK();
	rc = iget(vfsp, pList.ino, &ip, 0);
	ICACHE_UNLOCK();
	if(rc)
        {
jEVENT(0,("jfs_defragfs: stale target object.\n"));
		rc = ESTALE;  /* stale object ENOENT */
		goto out1;
        }

	IWRITE_LOCK(ip);
	
	/* validate inode */
	if (ip->i_nlink == 0  
	|| ip->i_gen != pList.gen
	|| ip->i_fileset != pList.fileset
	|| ip->i_inostamp != pList.inostamp)
	{
jEVENT(0,("jfs_defragfs: stale target object.\n"));
		rc = ESTALE;  /* stale object ENOENT */
		goto out1;
	}
	
	/* validate object type: regular file or directory */
	imode = ip->i_mode & IFMT;
	switch(imode)
	{
	case IFREG:
	case IFDIR:
		break;
	default:
		rc = ESTALE;	/* invalid object type ENOENT */
		goto out1;
	}

	/*
	 * try to allocate new destination extent
	 */
	if (rc = dbAllocExact(ip, nxaddr, xlen))
        {
jEVENT(0,("jfs_defragfs: stale destination extent.\n"));
		rc = ENOSPC;
		goto out1;
        }

	iBindCache(ip);

	/*
	 *	regular file: 
         */
	if (imode == IFREG)
	{
		/*
		 * automatic commit before and after each relocation 
		 * may be skipped after more experience;
		 */
		/*
		 * commit any update before relocation
		 */
		if (ip->i_flag & IUPD)
		{
			ip->i_flag |= IFSYNC;

			txBegin(ip->i_ipmnt, &tid, 0);
			iplist[0] = ip;
			rc = txCommit(tid, 1, &iplist[0], 0);
			if (rc)
				goto out2;
			txEnd(tid);
		}

		/*
		 *	relocate either xtpage or data extent
		 */
		txBegin(ip->i_ipmnt, &tid, 0);

		/* source extent xad */
		XADoffset(&oxad, xoff);
		XADaddress(&oxad, oxaddr);
		XADlength(&oxad, xlen);

		rc = xtRelocate(tid, ip, &oxad, nxaddr, xtype);
		if (rc)
			goto out2;

		iplist[0] = ip;
		rc = txCommit(tid, 1, &iplist[0], 0);
		if (rc)
			goto out2;

		txEnd(tid);
		goto out1;
	}
	/*
	 *	directory:
	 */
	else  /* IFDIR */
	{
		/*
		 *	relocate dtpage
		 */
		txBegin(ip->i_ipmnt, &tid, 0);

		/* source extent pxd */
		PXDaddress(&opxd, oxaddr);
		PXDlength(&opxd, xlen);
	
		rc = dtRelocate(tid, ip, xoff, &opxd, nxaddr); 
		if (rc)
			goto out2;

		iplist[0] = ip;
		rc = txCommit(tid, 1, &iplist[0], 0);
		if (rc)
			goto out2;

		txEnd(tid);
		goto out1;
	}

out2:
	dbFree(ip, nxaddr, xlen) ;

out1:
	if (ip)
	{
		IWRITE_UNLOCK(ip);

		ICACHE_LOCK();
		iput(ip, NULL);
		ICACHE_UNLOCK();
	}

jEVENT(0,("jfs_defragfs: rc=%d\n", rc));
	return (rc);
}
Exemple #10
0
void config_handler(int argc,char**argv,int bloblen,void*blob)
{
        dbResult*res;
        int i,l,l2,n;
        char*blob2;
        /*check ACL*/
        if(!checkGlobalACL(ACL_CONFIG)){
                bc_hdl->sockwriter(bc_hdl,E_PERM,strlen(E_PERM));
                return;
        }
        /*invalid argc*/
        if(argc>3){
                bc_hdl->sockwriter(bc_hdl,E_SYNTAX,strlen(E_SYNTAX));
                return;
        }
        /*query*/
        if(argc==1){
                /*query*/
                res=query(bc_con,SQL_GETOPTIONS,servername);
                /*create blob*/
                if(!res){
                        bc_hdl->sockwriter(bc_hdl,E_DATABASE,strlen(E_DATABASE));
                        return;
                }
                l=dbNumRows(res);
                n=dbGetFieldIndex(res,"okey");
                for(l2=i=0;i<l;i++){
                        l2+=strlen(dbGetString(res,i,n))+1;
                }
                blob2=malloc(l2+64);/*64 should be enough for the text*/
                if(!blob2){
                        dbFree(res);
                        bc_hdl->sockwriter(bc_hdl,E_ALLOCATION,strlen(E_ALLOCATION));
                        return;
                }
                *blob2=0;
                sprintf(blob2,"+%i Options:\n",l2);
                for(i=0;i<l;i++)
                        strcat(strcat(blob2,strchr(dbGetString(res,i,n),':')+1),"\n");
                dbFree(res);
                /*send*/
                bc_hdl->sockwriter(bc_hdl,blob2,strlen(blob2));
                /*free*/
                free(blob2);
                return;
        }
        if(argc==2){
                res=query(bc_con,SQL_GETOPTION,servername,argv[1]);
                if(!res){
                        bc_hdl->sockwriter(bc_hdl,E_DATABASE,strlen(E_DATABASE));
                        return;
                }
                if(dbNumRows(res)){
                        char buf[64];
                        sprintf(buf,"+0 %s\n",dbGetStringByname(res,0,"oval"));
                        bc_hdl->sockwriter(bc_hdl,buf,strlen(buf));
                }else{
                        bc_hdl->sockwriter(bc_hdl,E_NOOPT,strlen(E_NOOPT));
                }
                dbFree(res);
                return;
        }
        /*set*/
        if(argc==3){
                /*FIXME: this is not transaction safe, unfortunately I know no safe way*/
                /*try update*/
                res=query(bc_con,SQL_UPDATEOPTION,argv[2],servername,argv[1]);
                if(!res){
                        bc_hdl->sockwriter(bc_hdl,E_DATABASE,strlen(E_DATABASE));
                        return;
                }
                if(dbAffectedRows(res)<=0){
                        dbFree(res);
                        /*try insert*/
                        res=query(bc_con,SQL_INSERTOPTION,argv[2],servername,argv[1]);
                }
                if(!dbUpdateOK(res)){
                        bc_hdl->sockwriter(bc_hdl,E_DATABASE,strlen(E_DATABASE));
                }else{
                        bc_hdl->sockwriter(bc_hdl,E_OK,strlen(E_OK));
                }
                if(res)dbFree(res);
                return;
        }
}
char *fetchAccess(int getThis, int getType, struct threadInfo * ti) {
	int (*dbConnect)(struct threadStorageInfo *);
	void (*dbDisconnect)(struct threadStorageInfo *);
	void (*dbEscape)(struct threadStorageInfo *, char *, char *);
	void (*dbPush)(struct threadStorageInfo *, char *);
	char *(*dbPull)(struct threadStorageInfo *, char *);
	void (*dbFree)(struct threadStorageInfo *);

	dbConnect = ti->dbInfo.connect;
	dbDisconnect = ti->dbInfo.disconnect;
	dbEscape = ti->dbInfo.escape;
	dbPush = ti->dbInfo.push;
	dbPull = ti->dbInfo.pull;
	dbFree = ti->dbInfo.free;

	ti->commandInfo.esc1Buffer = NULL;
	ti->commandInfo.esc2Buffer = NULL;
	ti->commandInfo.esc3Buffer = NULL;
	ti->commandInfo.esc4Buffer = NULL;
	ti->commandInfo.esc5Buffer = NULL;

	// Allocate memory for buffers
	ti->commandInfo.s =
		(ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) +
		(ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) +
		CONFIG_SPACE_SIZE;

	if((ti->commandInfo.statBuffer = malloc(ti->commandInfo.s + 1)) == NULL) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

		return(ti->dataBuffer);
	}

	// Escape parameters received from the client
	if(ti->handlerArrays[HANDLER_ARRAY_UID].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_UID].buffer[0] != 0) {
		if((ti->commandInfo.esc1Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_UID].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc1Buffer, ti->handlerArrays[HANDLER_ARRAY_UID].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer[0] != 0) {
		if((ti->commandInfo.esc2Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_ITEM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc2Buffer, ti->handlerArrays[HANDLER_ARRAY_ITEM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer[0] != 0) {
		if((ti->commandInfo.esc3Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_DOMAIN].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc3Buffer, ti->handlerArrays[HANDLER_ARRAY_DOMAIN].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer[0] != 0) {
		if((ti->commandInfo.esc4Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_PARAM].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc4Buffer, ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer);
	}

	if(ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer != NULL && ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer[0] != 0) {
		if((ti->commandInfo.esc5Buffer = malloc((ti->handlerArrays[HANDLER_ARRAY_OPTION].size * 2) + 1)) == NULL) {
			free(ti->commandInfo.statBuffer);
			free(ti->commandInfo.esc1Buffer);
			free(ti->commandInfo.esc2Buffer);
			free(ti->commandInfo.esc3Buffer);
			free(ti->commandInfo.esc4Buffer);

			replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_OUTOFMEMORY, ERROR_MESS_SERVER_OUTOFMEMORY, ti);

			return(ti->dataBuffer);
		}

		dbEscape(&ti->storageInfo, ti->commandInfo.esc5Buffer, ti->handlerArrays[HANDLER_ARRAY_OPTION].buffer);
	}

	// Connect to database
	if(dbConnect(&ti->storageInfo) != 0) {
		replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_CONNECTFAILED, ERROR_MESS_DATABASE_CONNECTFAILED, ti);
	}
	else {
		// Construct SQL statement for requested item
		switch(getThis) {
			case 1:
				// Change own password
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_USERS " SET " TABLECOL_USER_PWD " = " TABLE_PWD_HASH "('%s') WHERE " TABLECOL_USER_UID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 2:
				// Change user's password
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_USERS " SET " TABLECOL_USER_PWD " = " TABLE_PWD_HASH "('%s') WHERE " TABLECOL_USER_UID " = '%s'%c",
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 3:
				// Check if requestor is admin or not
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_USER_PERM " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '' AND " TABLECOL_USER_GRP " = '' AND " TABLECOL_USER_PERM " = '" PRIVILEGE_LEVEL_ADMIN_S "'%c",
					ti->commandInfo.esc1Buffer,
					0
				);

				break;
			case 4:
				// Get user's default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT MAX(" TABLECOL_USER_PERM ") AS " TABLECOL_USER_PERM " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '' AND " TABLECOL_USER_GRP " = ''%c",
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 5:
				// Create new user privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_PERM_NODES " (" TABLEKEY_USER_PERM ") VALUES((SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s'), '', '%s', '', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 6:
				// Create new user default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"INSERT INTO " TABLE_PERM_NODES " (" TABLEKEY_USER_PERM ") VALUES((SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s'), '', '', '', '%s')%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 7:
				// Modify existing user privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_PERM_NODES " SET " TABLECOL_USER_PERM " = '%s' WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '%s'%c",
					ti->commandInfo.esc5Buffer,
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 8:
				// Modify existing user default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"UPDATE " TABLE_PERM_NODES " SET " TABLECOL_USER_PERM " = '%s' WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = ''%c",
					ti->commandInfo.esc5Buffer,
					ti->commandInfo.esc2Buffer,
					0
				);

				break;
			case 9:
				// Check user default privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT DISTINCT " TABLECOL_USER_USID " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_PERM " = '%s' AND " TABLECOL_USER_DOMAIN " = ''%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc5Buffer,
					0
				);

				break;
			case 10:
				// Delete existing user privilege level
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"DELETE FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			case 11:
				// Get user's privilege level for domain
				snprintf(
					ti->commandInfo.statBuffer,
					ti->commandInfo.s,
					"SELECT MAX(" TABLECOL_USER_PERM ") AS " TABLECOL_USER_PERM " FROM " TABLE_PERM_NODES " WHERE " TABLECOL_USER_USID " IN (SELECT DISTINCT " TABLECOL_USER_ID " FROM " TABLE_USERS " WHERE " TABLECOL_USER_UID " = '%s') AND " TABLECOL_USER_DOMAIN " = '%s'%c",
					ti->commandInfo.esc2Buffer,
					ti->commandInfo.esc4Buffer,
					0
				);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		// Pull data out from database
		switch(getType) {
			case QUERY_TYPE_PULL:
				if(dbPull(&ti->storageInfo, ti->commandInfo.statBuffer) == NULL) {
					replyPrepare(ERROR_SLIGHT, ERROR_CLASS_DATABASE, ERROR_CODE_DATABASE_PULLFAILED, ERROR_MESS_DATABASE_PULLFAILED, ti);
				}
				else {
					replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

					dbFree(&ti->storageInfo);
				}

				break;
			case QUERY_TYPE_PUSH:
				dbPush(&ti->storageInfo, ti->commandInfo.statBuffer);

				snprintf(
					ti->logSpace,
					sizeof(ti->logSpace),
					"User \"%s\" modified by \"%s\" using command \"%s\"%c",
					ti->handlerArrays[HANDLER_ARRAY_PARAM].buffer,
					ti->handlerArrays[HANDLER_ARRAY_UID].buffer,
					ti->handlerArrays[HANDLER_ARRAY_COMMAND].buffer,
					0
				);

				logMessage(ti->logSpace);

				replyPrepare(ERROR_NOERROR, 0, 0, ti->storageInfo.replyBuffer, ti);

				break;
			default:
				replyPrepare(ERROR_SLIGHT, ERROR_CLASS_SERVER, ERROR_CODE_SERVER_INTERNALERROR, ERROR_MESS_SERVER_INTERNALERROR, ti);

				break;
		}

		dbDisconnect(&ti->storageInfo);
	}

	// Free allocated resources and reply to the client
	if(ti->commandInfo.esc1Buffer != NULL) {
		free(ti->commandInfo.esc1Buffer);
	}

	if(ti->commandInfo.esc2Buffer != NULL) {
		free(ti->commandInfo.esc2Buffer);
	}

	if(ti->commandInfo.esc3Buffer != NULL) {
		free(ti->commandInfo.esc3Buffer);
	}

	if(ti->commandInfo.esc4Buffer != NULL) {
		free(ti->commandInfo.esc4Buffer);
	}

	if(ti->commandInfo.esc5Buffer != NULL) {
		free(ti->commandInfo.esc5Buffer);
	}

	free(ti->commandInfo.statBuffer);

	return(ti->dataBuffer);
}