Help making a ruby module

I am working on making a ruby module to wrapper the xsan quota fctl
calls.
I have C code that will get the quota information, but using the picaxe
book i dont quite understand how to make a wrapper.
Essecally i need 2 functions, a GetQuota function that would return a
Ruby struct with the information and a SetQuota function
the GetQuota would take only a name(“mcgoverp”) and a type (“U”), the
SetQuota function would take a hard limit soft limit and time out as
well as a name and a type.

This is my current trial

#include “ruby.h”
#include “./extapi.h”
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>

static VALUE cXsanQuota;
static void free_quota(void *p)
{

}

static VALUE quota_alloc(VALUE klass){

}

static VALUE quota_initialize(VALUE self)
{
return self;
}
static VALUE get_quota(VALUE self,VALUE user,VALUE system)
{
const char* in_file = “/Home/”;
int in_fd = open(in_file, O_RDONLY); //open existing file
GetQuotaReqReply_t test;
GetQuotaReq_t request;
request.gq_type=‘U’;
strcpy(request.gq_quotaname,t.pw_name);
test.req=request;
int res=fcntl(in_fd,F_GETQUOTA,&test);
//test.response has struct
//how do i return that?
}
static VALUE set_quota(VALUE self,VALUE user, VALUE hl, VALUE sl, VALUE
to)
{
SetQuotaReq_t setquota;
char *str;
str=RSTRING(user)->ptr;
strcpy(setquota.sq_quotaname,str);
setquota.sq_type=“U”;
setquota.sq_timelimit=FIX2INT(to);
setquota.sq_hardlimit=NUM2ULONG(hl);
setquota.sq_softlimit=NUM2ULONG(sl);
res=fcntl(in_fd,F_SETQUOTA,&setquota);
return res;
}
void Init_XsanQuota()
{
cXsanQuota=rb_define_class(“XsanQuota”,rb_cObject);
rb_define_alloc_func(cXsanQuota,quota_alloc);
rb_define_method(cXsanQuota,“initialize”, quota_initialize,1);
rb_define_method(cXsanQuota,“getQuota”, quota_initialize,3);
rb_define_method(cXsanQuota,“setQuota”, quota_initialize,5);
}

“R” == Ryan McGovern [email protected] writes:

R> int res=fcntl(in_fd,F_GETQUOTA,&test);
R> //test.response has struct
R> //how do i return that?

See ext/etc/etc.c in the ruby distribution to see how create and return
a
ruby struct.

R> }
R> static VALUE set_quota(VALUE self,VALUE user, VALUE hl, VALUE sl,
VALUE to)
R> {
R> SetQuotaReq_t setquota;
R> char *str;
R> str=RSTRING(user)->ptr;

Never do this : what if `user’ is a Float ?

Use StringValuePtr()

R> strcpy(setquota.sq_quotaname,str);

R> void Init_XsanQuota()

best to call it `Init_xsanquota’

Guy Decoux

Hi,

At Sat, 15 Jul 2006 08:01:11 +0900,
Ryan McGovern wrote in [ruby-talk:202039]:

Essecally i need 2 functions, a GetQuota function that would return a
Ruby struct with the information and a SetQuota function
the GetQuota would take only a name(“mcgoverp”) and a type (“U”), the
SetQuota function would take a hard limit soft limit and time out as
well as a name and a type.

This is my current trial

Seems module functions better.

void Init_XsanQuota()
{
cXsanQuota=rb_define_module(“XsanQuota”);
rb_define_module_function(cXsanQuota,“getQuota”,
quota_initialize,3);
rb_define_module_function(cXsanQuota,“setQuota”,
quota_initialize,5);
}

free_quota(), quota_alloc() and quota_initialize() aren’t needed.

[email protected] wrote:

Seems module functions better.

Thanks for the help, i also figured it out using SWIG, though it’s just
a temporary solution

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs