pax_global_header00006660000000000000000000000064150712147620014517gustar00rootroot0000000000000052 comment=d699f2d403cbac38e91c36331cdc5053675e899d Tatakinov-ninix-fmo-a6cef3f/000077500000000000000000000000001507121476200161405ustar00rootroot00000000000000Tatakinov-ninix-fmo-a6cef3f/Gemfile000066400000000000000000000003001507121476200174240ustar00rootroot00000000000000# frozen_string_literal: true source "https://rubygems.org" # Specify your gem's dependencies in ninix-fmo.gemspec gemspec gem "bundler", "~> 1.5" gem "rake", "~> 13.0" gem "rake-compiler" Tatakinov-ninix-fmo-a6cef3f/LICENSE.txt000066400000000000000000000020641507121476200177650ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2024 Tatakinov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Tatakinov-ninix-fmo-a6cef3f/README.md000066400000000000000000000005711507121476200174220ustar00rootroot00000000000000# NinixFMO A gem for using FileMappingObject in ninix-kagari. ## Requirements ``` apt install build-essential rake rake-compiler ruby-dev ``` ## How to Install ``` git clone https://github.com/Tatakinov/ninix-fmo.git cd ninix-fmo rake install ``` ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). Tatakinov-ninix-fmo-a6cef3f/Rakefile000066400000000000000000000002371507121476200176070ustar00rootroot00000000000000require "bundler/gem_tasks" require 'rake/extensiontask' Rake::ExtensionTask.new 'ninix-fmo' do |ext| ext.lib_dir = 'lib/ninix-fmo' end task default: %i[] Tatakinov-ninix-fmo-a6cef3f/debian/000077500000000000000000000000001507121476200173625ustar00rootroot00000000000000Tatakinov-ninix-fmo-a6cef3f/debian/control000066400000000000000000000005221507121476200207640ustar00rootroot00000000000000Package: ninix-fmo Version: @version Architecture: amd64 Maintainer: Tatakinov Installed-Size: @installed_size Depends: ruby | ruby-interpreter Recommends: ninix-kagari Section: games Priority: optional Homepage: https://github.com/Tatakinov/ninix-fmo Description: A gem for using FileMappingObject in ninix-kagari. Tatakinov-ninix-fmo-a6cef3f/ext/000077500000000000000000000000001507121476200167405ustar00rootroot00000000000000Tatakinov-ninix-fmo-a6cef3f/ext/ninix-fmo/000077500000000000000000000000001507121476200206445ustar00rootroot00000000000000Tatakinov-ninix-fmo-a6cef3f/ext/ninix-fmo/extconf.rb000066400000000000000000000007561507121476200226470ustar00rootroot00000000000000require 'mkmf' func = [] if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ func = ['CreateMutex', 'OpenMutex', 'CreateFileMapping', 'OpenFileMapping', 'WaitForSingleObject', 'MapViewOfFile', 'UnmapViewOfFile', 'CloseHandle'] else func = ['strndup', 'shm_open', 'ftruncate', 'mmap', 'sem_init', 'sem_wait', 'sem_post', 'munmap', 'shm_unlink', 'close'] end for f in func abort 'missing ' + f unless have_func f end create_makefile 'ninix-fmo/ninix_fmo' Tatakinov-ninix-fmo-a6cef3f/ext/ninix-fmo/ninix-fmo.c000066400000000000000000000225711507121476200227230ustar00rootroot00000000000000#include #include #include #include #ifdef WIN32 #include #else #include #include #include #include #include #include #endif // WIN32 static const int OPENED = 1; static const int CLOSED = 0; static const int ERR = -1; static const int F_RDONLY = 0x01; static const int F_RDWR = 0x02; static const int F_CREAT = 0x04; static const int F_EXCL = 0x08; static const int F_TRUNC = 0x10; struct shm_t { uint32_t size; #ifdef WIN32 char buf[MAX_PATH]; #else sem_t sem; char buf[PATH_MAX]; #endif // WIN32 }; struct ninix_fmo { int status; struct shm_t *memory; #ifdef WIN32 HANDLE mutex; HANDLE fmo; #else int is_owner; int fd; char *name; #endif // WIN32 }; #ifdef WIN32 static char *strndup_(char *s, size_t n) { char *p = malloc(sizeof(char) * (n + 1)); if (p == NULL) { return NULL; } memcpy(p, s, n); p[n] = '\0'; return p; } #endif // WIN32 static void ninix_fmo_free(void *ptr) { struct ninix_fmo *p = ptr; if (p->status > CLOSED) { p->status = CLOSED; #ifdef WIN32 UnmapViewOfFile(p->memory); CloseHandle(p->fmo); CloseHandle(p->mutex); #else if (p->is_owner) { sem_destroy(&p->memory->sem); munmap(p->memory, sizeof(struct shm_t)); shm_unlink(p->name); } free(p->name); #endif // WIN32 } } static VALUE ninix_fmo_alloc(VALUE klass) { VALUE obj; struct ninix_fmo *p; obj = Data_Make_Struct(klass, struct ninix_fmo, NULL, ninix_fmo_free, p); p->status = CLOSED; #ifdef WIN32 #else p->is_owner = 0; #endif return obj; } static VALUE ninix_fmo_init(VALUE self, VALUE name, VALUE flag) { struct ninix_fmo *p; Data_Get_Struct(self, struct ninix_fmo, p); Check_Type(name, T_STRING); Check_Type(flag, T_FIXNUM); int i = NUM2INT(flag); #ifdef WIN32 DWORD create_flag = 0; DWORD open_flag = 0; BOOL is_owner = FALSE; if (i & F_RDONLY) { create_flag |= PAGE_READONLY; open_flag |= FILE_MAP_READ; } if (i & F_RDWR) { create_flag |= PAGE_READWRITE; open_flag |= FILE_MAP_ALL_ACCESS; } if (i & F_CREAT) { is_owner = TRUE; } int len = RSTRING_LENINT(name); if (len + 6 >= MAX_PATH) { goto error_too_long; } char *n = strndup_(RSTRING_PTR(name), len); if (n == NULL) { goto error_strndup; } char *n_for_mutex = (char *) malloc(sizeof(char) * (len + 6 + 1)); if (n_for_mutex == NULL) { goto error_malloc; } memcpy(n_for_mutex, n, len); memcpy(&n_for_mutex[len], "_mutex", 6); n_for_mutex[len + 6] = '\0'; if (is_owner) { p->fmo = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, create_flag, 0, sizeof(struct shm_t), n); } else { p->fmo = OpenFileMapping(open_flag, FALSE, n); } if (p->fmo == NULL) { goto error_create_file_mapping; } p->memory = MapViewOfFile(p->fmo, open_flag, 0, 0, 0); if (p->memory == NULL) { goto error_map_view_of_file; } if (is_owner) { p->memory->size = 0; p->mutex = CreateMutex(NULL, FALSE, n_for_mutex); } else { p->mutex = OpenMutex(SYNCHRONIZE, FALSE, n_for_mutex); } if (p->mutex == NULL) { goto error_create_mutex; } free(n_for_mutex); free(n); p->status = OPENED; return self; if (0) { error_create_mutex: rb_raise(rb_eSystemCallError, "failed to Create/OpenMutex"); } UnmapViewOfFile(p->memory); if (0) { error_map_view_of_file: rb_raise(rb_eSystemCallError, "failed to map_view_of_file"); } CloseHandle(p->fmo); if (0) { error_create_file_mapping: rb_raise(rb_eSystemCallError, "failed to Create/OpenFileMapping"); } free(n_for_mutex); if (0) { error_malloc: rb_raise(rb_eSystemCallError, "failed to malloc"); } free(n); if (0) { error_strndup: rb_raise(rb_eSystemCallError, "failed to strndup"); } if (0) { error_too_long: rb_raise(rb_eArgError, "name is too long"); } p->status = ERR; return self; #else int f = 0; if (i & F_RDONLY) { f |= O_RDONLY; } if (i & F_RDWR) { f |= O_RDWR; } if (i & F_CREAT) { f |= O_CREAT; p->is_owner = 1; } if (i & F_EXCL) { f |= O_EXCL; } if (i & F_TRUNC) { f |= O_TRUNC; } int len = RSTRING_LENINT(name); if (len >= NAME_MAX) { goto error_too_long; } p->name = strndup(RSTRING_PTR(name), len); if (p->name == NULL) { goto error_strndup; } p->fd = shm_open(p->name, f, S_IRUSR | S_IWUSR); if (p->fd == -1) { goto error_shm_open; } if (ftruncate(p->fd, sizeof(struct shm_t)) == -1) { goto error_ftruncate; } p->memory = mmap(NULL, sizeof(struct shm_t), PROT_READ | PROT_WRITE, MAP_SHARED, p->fd, 0); if (p->memory == MAP_FAILED) { goto error_mmap; } close(p->fd); if (p->is_owner) { p->memory->size = 0; if (sem_init(&p->memory->sem, 1, 1) == -1) { goto error_sem_init; } } p->status = OPENED; return self; if (0) { error_sem_init: rb_raise(rb_eSystemCallError, "%s: %s", "failed to init semaphore", strerror(errno)); } munmap(p->memory, sizeof(struct shm_t)); if (0) { error_mmap: rb_raise(rb_eSystemCallError, "%s: %s", "failed to mmap", strerror(errno)); } if (0) { error_ftruncate: rb_raise(rb_eSystemCallError, "%s: %s", "failed to truncate", strerror(errno)); } if (p->is_owner) { shm_unlink(p->name); } if (0) { error_shm_open: rb_raise(rb_eSystemCallError, "%s: %s", "failed to shm_open", strerror(errno)); } free(p->name); if (0) { error_strndup: rb_raise(rb_eSystemCallError, "%s: %s", "failed to strndup", strerror(errno)); } if (0) { error_too_long: rb_raise(rb_eArgError, "name is too long"); } p->status = ERR; return self; #endif // WIN32 } static VALUE ninix_fmo_read(VALUE self) { struct ninix_fmo *p; Data_Get_Struct(self, struct ninix_fmo, p); if (p->status <= CLOSED) { return Qfalse; } #ifdef WIN32 if (WaitForSingleObject(p->mutex, INFINITE) != WAIT_OBJECT_0) { goto error_mutex; } VALUE ret = rb_str_new(p->memory->buf, p->memory->size); ReleaseMutex(p->mutex); return ret; error_mutex: CloseHandle(p->mutex); UnmapViewOfFile(p->memory); CloseHandle(p->fmo); p->status = ERR; rb_raise(rb_eSystemCallError, "failed to wait/release mutex"); return Qfalse; #else if (sem_wait(&p->memory->sem) == -1) { goto error_sem; } VALUE ret = rb_str_new(p->memory->buf, p->memory->size); if (sem_post(&p->memory->sem) == -1) { goto error_sem; } return ret; error_sem: if (p->is_owner) { sem_destroy(&p->memory->sem); munmap(p->memory, sizeof(struct shm_t)); shm_unlink(p->name); } p->status = ERR; rb_raise(rb_eSystemCallError, "%s: %s", "failed to wait/post semaphore", strerror(errno)); return Qfalse; #endif // WIN32 } static VALUE ninix_fmo_write(VALUE self, VALUE data) { struct ninix_fmo *p; Check_Type(data, T_STRING); Data_Get_Struct(self, struct ninix_fmo, p); if (p->status <= CLOSED) { return Qfalse; } char *d = RSTRING_PTR(data); #ifdef WIN32 if (RSTRING_LEN(data) >= MAX_PATH) { rb_raise(rb_eArgError, "data is too long"); return Qfalse; } if (WaitForSingleObject(p->mutex, INFINITE) != WAIT_OBJECT_0) { goto error_mutex; } p->memory->size = RSTRING_LEN(data); memcpy(p->memory->buf, d, p->memory->size); p->memory->buf[p->memory->size] = '\0'; if (ReleaseMutex(p->mutex) == FALSE) { goto error_mutex; } return Qtrue; error_mutex: CloseHandle(p->mutex); UnmapViewOfFile(p->memory); CloseHandle(p->fmo); p->status = ERR; rb_raise(rb_eSystemCallError, "failed to wait/release mutex"); return Qfalse; #else if (RSTRING_LEN(data) >= PATH_MAX) { rb_raise(rb_eArgError, "data is too long"); return Qfalse; } if (sem_wait(&p->memory->sem) == -1) { goto error_sem; } p->memory->size = RSTRING_LEN(data); memcpy(p->memory->buf, d, p->memory->size); p->memory->buf[p->memory->size] = '\0'; if (sem_post(&p->memory->sem) == -1) { goto error_sem; } return Qtrue; error_sem: sem_destroy(&p->memory->sem); munmap(p->memory, sizeof(struct shm_t)); shm_unlink(p->name); p->status = ERR; rb_raise(rb_eSystemCallError, "%s: %s", "failed to wait/post semaphore", strerror(errno)); return Qfalse; #endif // WIN32 } static VALUE ninix_fmo_close(VALUE self) { struct ninix_fmo *p; Data_Get_Struct(self, struct ninix_fmo, p); ninix_fmo_free(p); return Qtrue; } void Init_ninix_fmo() { VALUE mNinixFMO = rb_define_module("NinixFMO"); VALUE cNinixFMO = rb_define_class_under(mNinixFMO, "NinixFMO", rb_cObject); rb_define_alloc_func(cNinixFMO, ninix_fmo_alloc); rb_define_method(cNinixFMO, "initialize", ninix_fmo_init, 2); rb_define_method(cNinixFMO, "read", ninix_fmo_read, 0); rb_define_method(cNinixFMO, "write", ninix_fmo_write, 1); rb_define_method(cNinixFMO, "close", ninix_fmo_close, 0); } Tatakinov-ninix-fmo-a6cef3f/lib/000077500000000000000000000000001507121476200167065ustar00rootroot00000000000000Tatakinov-ninix-fmo-a6cef3f/lib/ninix-fmo.rb000066400000000000000000000003041507121476200211340ustar00rootroot00000000000000require_relative 'ninix-fmo/version' module NinixFMO O_RDONLY = 0x01 O_RDWR = 0x02 O_CREAT = 0x04 O_EXCL = 0x08 O_TRUNC = 0x10 class NinixFMO end end require 'ninix-fmo/ninix_fmo' Tatakinov-ninix-fmo-a6cef3f/lib/ninix-fmo/000077500000000000000000000000001507121476200206125ustar00rootroot00000000000000Tatakinov-ninix-fmo-a6cef3f/lib/ninix-fmo/version.rb000066400000000000000000000000501507121476200226170ustar00rootroot00000000000000module NinixFMO VERSION = '1.0.2' end Tatakinov-ninix-fmo-a6cef3f/ninix-fmo.gemspec000066400000000000000000000023061507121476200214120ustar00rootroot00000000000000# frozen_string_literal: true Gem::Specification.new do |spec| spec.name = "ninix-fmo" spec.version = "1.0.1" spec.authors = ["Tatakinov"] spec.email = ["tatakinov@gmail.com"] spec.summary = "A gem for using FileMappingObject in ninix-kagari." spec.homepage = "https://tatakinov.github.io/" spec.license = "MIT" spec.required_ruby_version = ">= 3.1.0" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = "https://github.com/Tatakinov/ninix-fmo" # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path(__dir__)) do `git ls-files -z`.split("\x0").reject do |f| (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features|utils)/|\.(?:git|travis|circleci)|appveyor|debian)}) end end spec.bindir = "exe" spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.extensions = %w{ext/ninix-fmo/extconf.rb} spec.add_development_dependency "bundler", "~> 1.5" spec.add_development_dependency "rake" spec.add_development_dependency "rake-compiler" end Tatakinov-ninix-fmo-a6cef3f/utils/000077500000000000000000000000001507121476200173005ustar00rootroot00000000000000Tatakinov-ninix-fmo-a6cef3f/utils/make_deb.sh000077500000000000000000000024271507121476200213730ustar00rootroot00000000000000#!/bin/bash -x WORKDIR=work RUBY=ruby SPEC_DIR="usr/share/rubygems-integration/3.3.0/specifications" SO_DIR="usr/lib/x86_64-linux-gnu/ruby/vendor_ruby/3.3.0" RB_DIR="usr/lib/ruby/vendor_ruby" DOC_DIR="usr/share/doc/ninix-fmo" VERSION=$(ruby -r './lib/ninix-fmo/version.rb' -e 'print(NinixFMO::VERSION)') sed -e "s/@VERSION@/${VERSION}/g" < utils/ninix-fmo.gemspec.in > ninix-fmo.gemspec rake3.3 build mkdir gem gem install --install-dir ./gem pkg/ninix-fmo-${VERSION}.gem mkdir ${WORKDIR} pushd ${WORKDIR} # TODO ruby versionの特定 mkdir -p ${SPEC_DIR} mkdir -p ${SO_DIR} mkdir -p ${RB_DIR}/ninix-fmo mkdir -p ${DOC_DIR} cp -r ../debian DEBIAN grep -v 'extensions = ' < ../gem/specifications/ninix-fmo-${VERSION}.gemspec > ${SPEC_DIR}/ninix-fmo-${VERSION}.gemspec cp -r ../gem/extensions/x86_64-linux-gnu/3.3.0/ninix-fmo-${VERSION}/ninix-fmo ${SO_DIR} cp ../gem/gems/ninix-fmo-${VERSION}/lib/ninix-fmo.rb ${RB_DIR} cp ../gem/gems/ninix-fmo-${VERSION}/lib/ninix-fmo/version.rb ${RB_DIR}/ninix-fmo/ cp ../LICENSE.txt ${DOC_DIR}/copyright find usr -type f -exec md5sum {} \+ > DEBIAN/md5sums INSTALLED_SIZE=$(du -sk usr | cut -f 1) sed -i -e "s/@installed_size/${INSTALLED_SIZE}/g" -e "s/@version/${VERSION}/g" DEBIAN/control popd fakeroot dpkg-deb --build ${WORKDIR} . rm -r pkg gem work Tatakinov-ninix-fmo-a6cef3f/utils/ninix-fmo.gemspec.in000066400000000000000000000023121507121476200231540ustar00rootroot00000000000000# frozen_string_literal: true Gem::Specification.new do |spec| spec.name = "ninix-fmo" spec.version = "@VERSION@" spec.authors = ["Tatakinov"] spec.email = ["tatakinov@gmail.com"] spec.summary = "A gem for using FileMappingObject in ninix-kagari." spec.homepage = "https://tatakinov.github.io/" spec.license = "MIT" spec.required_ruby_version = ">= 3.1.0" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = "https://github.com/Tatakinov/ninix-fmo" # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path(__dir__)) do `git ls-files -z`.split("\x0").reject do |f| (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features|utils)/|\.(?:git|travis|circleci)|appveyor|debian)}) end end spec.bindir = "exe" spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.extensions = %w{ext/ninix-fmo/extconf.rb} spec.add_development_dependency "bundler", "~> 1.5" spec.add_development_dependency "rake" spec.add_development_dependency "rake-compiler" end