Skip to content
Closed
48 changes: 42 additions & 6 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Test LinuxStat with Ruby 2.3
- name: Test LibmagicRb with Ruby 2.3
uses: ruby/[email protected]
with:
ruby-version: 2.3
Expand All @@ -26,7 +26,7 @@ jobs:
run: rake test

- uses: actions/checkout@v2
- name: Test LinuxStat with Ruby 2.5
- name: Test LibmagicRb with Ruby 2.5
uses: ruby/[email protected]
with:
ruby-version: 2.5
Expand All @@ -38,7 +38,7 @@ jobs:
run: rake test

- uses: actions/checkout@v2
- name: Test LinuxStat with Ruby 2.7
- name: Test LibmagicRb with Ruby 2.7
uses: ruby/[email protected]
with:
ruby-version: 2.7
Expand All @@ -49,9 +49,9 @@ jobs:
- name: Run tests
run: rake test

- uses: actions/checkout@v2
- name: Test LinuxStat with Ruby 3.0
uses: ruby/setup-ruby@v1.59.1
- uses: actions/checkout@v4
- name: Test LibmagicRb with Ruby 3.0
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
- name: Install dependencies
Expand All @@ -60,3 +60,39 @@ jobs:
run: rake compile
- name: Run tests
run: rake test

- uses: actions/checkout@v4
- name: Test LibmagicRb with Ruby 3.1
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
- name: Install dependencies
run: bundle install
- name: Run rake compile
run: rake compile
- name: Run tests
run: rake test

- uses: actions/checkout@v4
- name: Test LibmagicRb with Ruby 3.2
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2
- name: Install dependencies
run: bundle install
- name: Run rake compile
run: rake compile
- name: Run tests
run: rake test

- uses: actions/checkout@v4
- name: Test LibmagicRb with Ruby 3.3
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3
- name: Install dependencies
run: bundle install
- name: Run rake compile
run: rake compile
- name: Run tests
run: rake test
6 changes: 3 additions & 3 deletions ext/libmagic/definitions.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define RB_UNWRAP(cookie) \
magic_t *cookie ; \
TypedData_Get_Struct(self, magic_t, &fileType, cookie) ; \
if(!*cookie) rb_raise(rb_eFileClosedError, "Magic cookie already closed") ;
magic_t *cookie; \
TypedData_Get_Struct(self, magic_t, &fileType, cookie); \
if(!*cookie) rb_raise(rb_eFileClosedError, "Magic cookie already closed");
150 changes: 79 additions & 71 deletions ext/libmagic/func.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
Returns self.
*/

VALUE _closeGlobal_(volatile VALUE self) {
RB_UNWRAP(cookie) ;
VALUE _closeGlobal_(VALUE self) {
RB_UNWRAP(cookie);

magic_close(*cookie) ;
*cookie = NULL ;
rb_ivar_set(self, rb_intern("@closed"), Qtrue) ;
return self ;
magic_close(*cookie);
*cookie = NULL;
rb_ivar_set(self, rb_intern("@closed"), Qtrue);
return self;
}

/*
Expand All @@ -36,24 +36,26 @@ VALUE _closeGlobal_(volatile VALUE self) {
Returns self.
*/

VALUE _loadGlobal_(volatile VALUE self, volatile VALUE dbPath) {
char *databasePath = NULL ;
VALUE _loadGlobal_(VALUE self, VALUE dbPath) {
char *databasePath = NULL;

if (RB_TYPE_P(dbPath, T_STRING)) {
databasePath = StringValuePtr(dbPath) ;
rb_iv_set(self, "@db", dbPath) ;
} else if(RB_TYPE_P(dbPath, T_STRING)) {
rb_iv_set(self, "@db", Qnil) ;
databasePath = StringValuePtr(dbPath);
rb_iv_set(self, "@db", dbPath);
}

// Check if the database is a valid file or not
// Raises ruby error which will return.
RB_UNWRAP(cookie) ;
RB_UNWRAP(cookie);

if(databasePath) magic_validate_db(*cookie, databasePath) ;
magic_load(*cookie, databasePath) ;
if(databasePath) magic_validate_db(*cookie, databasePath);

return self ;
if (magic_load(*cookie, databasePath) == -1) {
rb_raise(rb_eRuntimeError, "Failed to load magic database: %s", magic_error(*cookie));
}


return self;
}

/*
Expand All @@ -70,28 +72,31 @@ VALUE _loadGlobal_(volatile VALUE self, volatile VALUE dbPath) {
Returns String or nil.
*/

VALUE _checkGlobal_(volatile VALUE self) {
RB_UNWRAP(cookie) ;
VALUE _checkGlobal_(VALUE self) {
RB_UNWRAP(cookie);

// Database path
VALUE db = rb_iv_get(self, "@db") ;
VALUE db = rb_iv_get(self, "@db");

char *database = NULL ;
char *database = NULL;
if(RB_TYPE_P(db, T_STRING)) {
database = StringValuePtr(db) ;
database = StringValuePtr(db);
}

// File path
VALUE f = rb_iv_get(self, "@file") ;
char *file = StringValuePtr(f) ;
VALUE f = rb_iv_get(self, "@file");
char *file = StringValuePtr(f);

if(database) magic_validate_db(*cookie, database) ;
magic_load(*cookie, database) ;
if(database) magic_validate_db(*cookie, database);

fileReadable(file) ;
const char *mt = magic_file(*cookie, file) ;
if (magic_load(*cookie, database) == -1) {
rb_raise(rb_eRuntimeError, "Failed to load magic database: %s", magic_error(*cookie));
}

return mt ? rb_str_new_cstr(mt) : Qnil ;
fileReadable(file);
const char *mt = magic_file(*cookie, file);

return mt ? rb_str_new_cstr(mt) : Qnil;
}

/*
Expand All @@ -106,18 +111,18 @@ VALUE _checkGlobal_(volatile VALUE self) {
# => #<LibmagicRb:0x00005581018f35e0 @closed=true, @db=nil, @file="/usr/share/dict/words", @mode=1106>
*/

VALUE _getParamGlobal_(volatile VALUE self, volatile VALUE param) {
VALUE _getParamGlobal_(VALUE self, VALUE param) {
#if MAGIC_VERSION > 525
RB_UNWRAP(cookie) ;
RB_UNWRAP(cookie);

unsigned int _param = NUM2UINT(param) ;
unsigned long value ;
unsigned int _param = NUM2UINT(param);
unsigned long value;

int status = magic_getparam(*cookie, _param, &value) ;
if (status) return Qnil ;
return ULONG2NUM(value) ;
int status = magic_getparam(*cookie, _param, &value);
if (status) return Qnil;
return ULONG2NUM(value);
#else
return Qnil ;
return Qnil;
#endif
}

Expand All @@ -142,22 +147,22 @@ VALUE _getParamGlobal_(volatile VALUE self, volatile VALUE param) {
Returns Integer or nil on failure.
*/

VALUE _setParamGlobal_(volatile VALUE self, volatile VALUE param, volatile VALUE paramVal) {
VALUE _setParamGlobal_(VALUE self, VALUE param, VALUE paramVal) {
#if MAGIC_VERSION > 525
unsigned int _param = NUM2UINT(param) ;
unsigned long _paramVal = NUM2ULONG(paramVal) ;
unsigned int _param = NUM2UINT(param);
unsigned long _paramVal = NUM2ULONG(paramVal);

RB_UNWRAP(cookie) ;
RB_UNWRAP(cookie);

unsigned long value ;
magic_setparam(*cookie, _param, &_paramVal) ;
unsigned long value;
magic_setparam(*cookie, _param, &_paramVal);

int status = magic_getparam(*cookie, _param, &value) ;
if (status) return Qnil ;
int status = magic_getparam(*cookie, _param, &value);
if (status) return Qnil;

return ULONG2NUM((int)value) ;
return ULONG2NUM((int)value);
#else
return Qnil ;
return Qnil;
#endif
}

Expand All @@ -180,23 +185,26 @@ VALUE _setParamGlobal_(volatile VALUE self, volatile VALUE param, volatile VALUE
Returns either String or nil.
*/

VALUE _bufferGlobal_(volatile VALUE self, volatile VALUE string) {
RB_UNWRAP(cookie) ;
VALUE _bufferGlobal_(VALUE self, VALUE string) {
RB_UNWRAP(cookie);

VALUE db = rb_iv_get(self, "@db") ;
VALUE db = rb_iv_get(self, "@db");

char *database = NULL ;
char *database = NULL;
if(RB_TYPE_P(db, T_STRING)) {
database = StringValuePtr(db) ;
database = StringValuePtr(db);
}

if(database) magic_validate_db(*cookie, database) ;
magic_load(*cookie, database) ;
if(database) magic_validate_db(*cookie, database);

if (magic_load(*cookie, database) == -1) {
rb_raise(rb_eRuntimeError, "Failed to load magic database: %s", magic_error(*cookie));
}

char *buffer = StringValuePtr(string) ;
const char *buf = magic_buffer(*cookie, buffer, strlen(buffer)) ;
char *buffer = StringValuePtr(string);
const char *buf = magic_buffer(*cookie, buffer, strlen(buffer));

return buf ? rb_str_new_cstr(buf) : Qnil ;
return buf ? rb_str_new_cstr(buf) : Qnil;
}

/*
Expand Down Expand Up @@ -229,20 +237,20 @@ VALUE _bufferGlobal_(volatile VALUE self, volatile VALUE string) {
=> #<LibmagicRb:0x000055cf280a9b30 @closed=true, @db=nil, @file=".", @mode=1106>
*/

VALUE _listGlobal_(volatile VALUE self) {
RB_UNWRAP(cookie) ;
VALUE _listGlobal_(VALUE self) {
RB_UNWRAP(cookie);

VALUE db = rb_iv_get(self, "@db") ;
VALUE db = rb_iv_get(self, "@db");

char *database = NULL ;
char *database = NULL;
if (RB_TYPE_P(db, T_STRING)) {
database = StringValuePtr(db) ;
database = StringValuePtr(db);
}

if(database) magic_validate_db(*cookie, database) ;
int status = magic_list(*cookie, database) ;
if(database) magic_validate_db(*cookie, database);
int status = magic_list(*cookie, database);

return INT2FIX(status) ;
return INT2FIX(status);
}

/*
Expand Down Expand Up @@ -276,16 +284,16 @@ VALUE _listGlobal_(volatile VALUE self) {
# => #<LibmagicRb:0x00005583732e1070 @closed=true, @db=nil, @file=".", @mode=256>
*/

VALUE _setflagsGlobal_(volatile VALUE self, volatile VALUE flags) {
unsigned int flag = NUM2UINT(flags) ;
VALUE _setflagsGlobal_(VALUE self, VALUE flags) {
unsigned int flag = NUM2UINT(flags);

RB_UNWRAP(cookie) ;
int status = magic_setflags(*cookie, flag) ;
RB_UNWRAP(cookie);
int status = magic_setflags(*cookie, flag);

if (status) {
return Qnil ;
return Qnil;
} else {
rb_ivar_set(self, rb_intern("@mode"), flags) ;
return flags ;
rb_ivar_set(self, rb_intern("@mode"), flags);
return flags;
}
}
Loading