From 3c77ef047dbc39c65628862d3a7212114e627e13 Mon Sep 17 00:00:00 2001 From: yuqing Date: Mon, 31 Oct 2016 18:46:03 +0800 Subject: [PATCH] id generator support file mode --- HISTORY | 3 +- libfastcommon.spec | 1 + php-fastcommon/fastcommon.c | 14 ++++---- php-fastcommon/php-fastcommon.spec.in | 6 ++-- php-fastcommon/test.php | 2 +- src/fast_allocator.h | 2 +- src/fast_mpool.h | 2 +- src/id_generator.c | 48 ++++++++++++++++++++++----- src/id_generator.h | 21 +++++++++++- 9 files changed, 76 insertions(+), 23 deletions(-) diff --git a/HISTORY b/HISTORY index 96c0f8f..dfee4d0 100644 --- a/HISTORY +++ b/HISTORY @@ -1,10 +1,11 @@ -Version 1.30 2016-09-08 +Version 1.30 2016-10-31 * modify php-fastcommon/test.php * php7_ext_wrapper.h: fix memory leak in php 7 * setsockopt SO_NOSIGPIPE when nessary * add function iovent_add_to_deleted_list * fixbug for select with microseconds + * id generator support file mode Version 1.29 2016-06-17 * ini_file_reader support #@if diff --git a/libfastcommon.spec b/libfastcommon.spec index e420887..cc63499 100644 --- a/libfastcommon.spec +++ b/libfastcommon.spec @@ -1,5 +1,6 @@ %define LibFastcommonDevel libfastcommon-devel +%define LibFastcommonDebuginfo libfastcommon-debuginfo Name: libfastcommon Version: 1.0.30 diff --git a/php-fastcommon/fastcommon.c b/php-fastcommon/fastcommon.c index 93e266c..142cee0 100644 --- a/php-fastcommon/fastcommon.c +++ b/php-fastcommon/fastcommon.c @@ -409,7 +409,7 @@ ZEND_FUNCTION(fastcommon_is_private_ip) /* resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_generator.sn", - int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16]) + int machine_id = 0, int mid_bits = 16, int extra_bits = 0, int sn_bits = 16, int mode = 0644]) return resource handle for success, false for fail */ ZEND_FUNCTION(fastcommon_id_generator_init) @@ -420,11 +420,12 @@ ZEND_FUNCTION(fastcommon_id_generator_init) long mid_bits; long extra_bits; long sn_bits; + long mode; char *filename; PHPIDGContext *php_idg_context; argc = ZEND_NUM_ARGS(); - if (argc > 5) { + if (argc > 6) { logError("file: "__FILE__", line: %d, " "fastcommon_id_generator_init parameters count: %d is invalid", __LINE__, argc); @@ -437,9 +438,10 @@ ZEND_FUNCTION(fastcommon_id_generator_init) mid_bits = 16; extra_bits = 0; sn_bits = 16; - if (zend_parse_parameters(argc TSRMLS_CC, "|sllll", &filename, + mode = ID_GENERATOR_DEFAULT_FILE_MODE; + if (zend_parse_parameters(argc TSRMLS_CC, "|slllll", &filename, &filename_len, &machine_id, &mid_bits, &extra_bits, - &sn_bits) == FAILURE) + &sn_bits, &mode) == FAILURE) { logError("file: "__FILE__", line: %d, " "zend_parse_parameters fail!", __LINE__); @@ -454,8 +456,8 @@ ZEND_FUNCTION(fastcommon_id_generator_init) RETURN_BOOL(false); } - if (id_generator_init_extra(&php_idg_context->idg_context, filename, - machine_id, mid_bits, extra_bits, sn_bits) != 0) + if (id_generator_init_extra_ex(&php_idg_context->idg_context, filename, + machine_id, mid_bits, extra_bits, sn_bits, mode) != 0) { RETURN_BOOL(false); } diff --git a/php-fastcommon/php-fastcommon.spec.in b/php-fastcommon/php-fastcommon.spec.in index 36c7294..0ba226c 100644 --- a/php-fastcommon/php-fastcommon.spec.in +++ b/php-fastcommon/php-fastcommon.spec.in @@ -1,7 +1,7 @@ %define php_inidir %(php --ini | head -n 1 | awk -F ':' '{print $2;}' | sed 's/ //g') %define php_extdir %(php-config --extension-dir 2>/dev/null) Name: php-fastcommon -Version: 1.0.8 +Version: 1.0.9 Release: 1%{?dist} Summary: The php extension for libfastcommon License: GPL @@ -11,8 +11,8 @@ Source: http://perso.orange.fr/sebastien.godard/%{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) -BuildRequires: libfastcommon-devel >= 1.0.29 -Requires: libfastcommon >= 1.0.29 +BuildRequires: libfastcommon-devel >= 1.0.30 +Requires: libfastcommon >= 1.0.30 %description This package provides the php extension for libfastcommon diff --git a/php-fastcommon/test.php b/php-fastcommon/test.php index f53581f..4e1c871 100644 --- a/php-fastcommon/test.php +++ b/php-fastcommon/test.php @@ -30,7 +30,7 @@ resource fastcommon_id_generator_init([string filename = "/tmp/fastcommon_id_gen */ $id = 6301319781687017475; -$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14); +$handle1 = fastcommon_id_generator_init("/tmp/sn1.txt", 0, 8, 10, 14, 0666); echo 'extra no: ' . fastcommon_id_generator_get_extra($id, $handle1) . "\n"; $handle2 = fastcommon_id_generator_init("/tmp/sn2.txt", 0, 8, 8, 16); diff --git a/src/fast_allocator.h b/src/fast_allocator.h index fc9aa8b..9a23c2a 100644 --- a/src/fast_allocator.h +++ b/src/fast_allocator.h @@ -43,7 +43,7 @@ struct fast_allocator_array int reclaim_interval; //<= 0 for never reclaim int last_reclaim_time; volatile int64_t malloc_bytes; //total alloc bytes - int64_t malloc_bytes_limit; //mater mark bytes for malloc + int64_t malloc_bytes_limit; //water mark bytes for malloc double expect_usage_ratio; struct fast_allocator_info **allocators; }; diff --git a/src/fast_mpool.h b/src/fast_mpool.h index 267477f..5cbb1ca 100644 --- a/src/fast_mpool.h +++ b/src/fast_mpool.h @@ -53,7 +53,7 @@ mpool init parameters: mpool: the mpool pointer alloc_size_once: malloc elements once, 0 for malloc 1MB memory once - discard_size: discard when remain size <= discard_size, 0 for 8 bytes + discard_size: discard when remain size <= discard_size, 0 for 64 bytes return error no, 0 for success, != 0 fail */ int fast_mpool_init(struct fast_mpool_man *mpool, diff --git a/src/id_generator.c b/src/id_generator.c index 5cabb1e..f6f7105 100644 --- a/src/id_generator.c +++ b/src/id_generator.c @@ -21,9 +21,9 @@ #include "local_ip_func.h" #include "id_generator.h" -int id_generator_init_extra(struct idg_context *context, const char *filename, +int id_generator_init_extra_ex(struct idg_context *context, const char *filename, const int machine_id, const int mid_bits, const int extra_bits, - const int sn_bits) + const int sn_bits, const mode_t mode) { int result; int mid; @@ -116,14 +116,35 @@ int id_generator_init_extra(struct idg_context *context, const char *filename, mid = ntohl(ip_addr.s_addr) & ((1 << mid_bits) - 1); } - if ((context->fd = open(filename, O_RDWR | O_CREAT, 0644)) < 0) + if ((context->fd = open(filename, O_RDWR)) < 0) { - result = errno != 0 ? errno : EACCES; - logError("file: "__FILE__", line: %d, " - "open file \"%s\" fail, " - "errno: %d, error info: %s", __LINE__, - filename, result, STRERROR(result)); - return result; + if (errno == ENOENT) + { + mode_t old_mode; + old_mode = umask(0); + if ((context->fd=open(filename, O_RDWR | O_CREAT, mode)) < 0) + { + result = errno != 0 ? errno : EACCES; + } + else + { + result = 0; + } + umask(old_mode); + } + else + { + result = errno != 0 ? errno : EACCES; + } + + if (result != 0) + { + logError("file: "__FILE__", line: %d, " + "open file \"%s\" fail, " + "errno: %d, error info: %s", __LINE__, + filename, result, STRERROR(result)); + return result; + } } context->machine_id = mid; @@ -143,6 +164,15 @@ int id_generator_init_extra(struct idg_context *context, const char *filename, return 0; } +int id_generator_init_extra(struct idg_context *context, const char *filename, + const int machine_id, const int mid_bits, const int extra_bits, + const int sn_bits) +{ + return id_generator_init_extra_ex(context, filename, + machine_id, mid_bits, extra_bits, sn_bits, + ID_GENERATOR_DEFAULT_FILE_MODE); +} + void id_generator_destroy(struct idg_context *context) { if (context->fd >= 0) diff --git a/src/id_generator.h b/src/id_generator.h index 765b227..d045e85 100644 --- a/src/id_generator.h +++ b/src/id_generator.h @@ -19,10 +19,13 @@ #include #include #include -#include +#include #include +#include #include "common_define.h" +#define ID_GENERATOR_DEFAULT_FILE_MODE 0666 + #ifdef __cplusplus extern "C" { #endif @@ -39,6 +42,22 @@ struct idg_context { int64_t sn_mask; }; +/** +* init function +* parameter: +* context: the id generator context +* filename: the filename to store id +* machine_id: the machine id, 0 for auto generate by local ip address +* mid_bits: the bits of machine id, such as 16 +* extra_bits: the extra bits, such as 0 +* sn_bits: the bits of serial no, such as 16, mid_bits + sn_bits must <= 32 +* mode: the mode for file open +* return error no, 0 for success, none zero for fail +*/ +int id_generator_init_extra_ex(struct idg_context *context, const char *filename, + const int machine_id, const int mid_bits, const int extra_bits, + const int sn_bits, const mode_t mode); + /** * init function * parameter: