[Groonga-commit] groonga/groonga [master] add new macros GRN_OPEN, GRN_CLOSE, GRN_READ and GRN_WRITE.

Back to archive index

null+****@clear***** null+****@clear*****
2012年 2月 28日 (火) 13:30:33 JST


Susumu Yata	2012-02-28 13:30:33 +0900 (Tue, 28 Feb 2012)

  New Revision: a46240d9cb657ce161247e436a3e432cd9405a13

  Log:
    add new macros GRN_OPEN, GRN_CLOSE, GRN_READ and GRN_WRITE.

  Modified files:
    configure.ac
    lib/com.c
    lib/dat.cpp
    lib/dat/file-impl.hpp
    lib/groonga_in.h
    lib/ii.c
    lib/io.c
    lib/proc.c
    src/groonga.c

  Modified: configure.ac (+1 -0)
===================================================================
--- configure.ac    2012-02-28 13:10:38 +0900 (d08ea8d)
+++ configure.ac    2012-02-28 13:30:33 +0900 (70352e9)
@@ -310,6 +310,7 @@ AC_CHECK_HEADERS(sys/mman.h sys/time.h sys/timeb.h sys/param.h sys/types.h sys/r
 AC_CHECK_HEADERS(netdb.h sys/wait.h sys/socket.h netinet/in.h netinet/tcp.h)
 AC_CHECK_HEADERS(ucontext.h signal.h errno.h execinfo.h sys/sysctl.h)
 AC_CHECK_HEADERS(time.h)
+AC_CHECK_FUNCS(open close read write)
 AC_CHECK_FUNCS(localtime_r gmtime_r mkostemp)
 BACKTRACE_LIBS=
 AC_CHECK_FUNCS(backtrace,

  Modified: lib/com.c (+2 -2)
===================================================================
--- lib/com.c    2012-02-28 13:10:38 +0900 (da028ec)
+++ lib/com.c    2012-02-28 13:30:33 +0900 (f8449e5)
@@ -328,10 +328,10 @@ grn_com_event_fin(grn_ctx *ctx, grn_com_event *ev)
 #ifndef USE_SELECT
   if (ev->events) { GRN_FREE(ev->events); }
 #ifdef USE_EPOLL
-  close(ev->epfd);
+  GRN_CLOSE(ev->epfd);
 #endif /* USE_EPOLL */
 #ifdef USE_KQUEUE
-  close(ev->kqfd);
+  GRN_CLOSE(ev->kqfd);
 #endif /* USE_KQUEUE*/
 #endif /* USE_SELECT */
   return GRN_SUCCESS;

  Modified: lib/dat.cpp (+1 -11)
===================================================================
--- lib/dat.cpp    2012-02-28 13:10:38 +0900 (04b56d8)
+++ lib/dat.cpp    2012-02-28 13:30:33 +0900 (2422577)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2011 Brazil
+/* Copyright(C) 2011-2012 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -24,16 +24,6 @@
 #include "dat.h"
 #include "util.h"
 
-/*
-  When this code is compiled with MinGW, a macro "open" is defined to replace
-  "open()" by "_open()". This causes a critical problem because grn::dat::Trie
-  and grn::dat::CursorFactory have member functions named "open()". So, the
-  macro must be undefined before the following #includes.
- */
-#ifdef open
-#  undef open
-#endif
-
 #include "dat/trie.hpp"
 #include "dat/cursor-factory.hpp"
 

  Modified: lib/dat/file-impl.hpp (+1 -4)
===================================================================
--- lib/dat/file-impl.hpp    2012-02-28 13:10:38 +0900 (7d4da15)
+++ lib/dat/file-impl.hpp    2012-02-28 13:30:33 +0900 (f44f424)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2011 Brazil
+/* Copyright(C) 2011-2012 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -20,9 +20,6 @@
 
 #ifdef WIN32
 #include <windows.h>
-# ifdef open
-#  undef open
-# endif  // open
 #endif  // WIN32
 
 #include "dat.hpp"

  Modified: lib/groonga_in.h (+28 -9)
===================================================================
--- lib/groonga_in.h    2012-02-28 13:10:38 +0900 (0bf117a)
+++ lib/groonga_in.h    2012-02-28 13:30:33 +0900 (67a136e)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2009-2011 Brazil
+/* Copyright(C) 2009-2012 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -70,6 +70,30 @@
 #  define GRN_VAR extern
 #endif
 
+#ifdef HAVE_OPEN
+# define GRN_OPEN(pathname, ...) open(pathname, __VA_ARGS__)
+#else
+# define GRN_OPEN(pathname, ...) _open(pathname, __VA_ARGS__)
+#endif /* HAVE_OPEN */
+
+#ifdef HAVE_CLOSE
+# define GRN_CLOSE(fd) close(fd)
+#else
+# define GRN_CLOSE(fd) _close(fd)
+#endif /* HAVE_CLOSE */
+
+#ifdef HAVE_READ
+# define GRN_READ(fd, buf, count) read(fd, buf, count)
+#else
+# define GRN_READ(fd, buf, count) _read(fd, buf, count)
+#endif /* HAVE_READ */
+
+#ifdef HAVE_WRITE
+# define GRN_WRITE(fd, buf, count) write(fd, buf, count)
+#else
+# define GRN_WRITE(fd, buf, count) _write(fd, buf, count)
+#endif /* HAVE_WRITE */
+
 #ifdef WIN32
 
 #if defined(__GNUC__) && !defined(WINVER)
@@ -98,22 +122,17 @@
   #define vsnprintf _vsnprintf
 #endif /* _MSC_VER < 1500 */
 #define unlink _unlink
-#define open _open
 #define lseek _lseek
-#define read _read
 #define getpid _getpid
 #if !defined(__GNUC__) && _MSC_VER < 1400
 # define fstat _fstat
 #endif /* !defined(__GNUC__) && _MSC_VER < 1400 */
-#define write _write
-#define close _close
 #define usleep(x) Sleep((x) / 1000)
 #define sleep(x) Sleep((x) * 1000)
 #if !defined(strcasecmp)
 #  define strcasecmp stricmp
 #endif /* !defined(strcasecmp) */
 
-
 #ifdef __GNUC__
 #include <stdint.h>
 #else
@@ -417,7 +436,7 @@ typedef int grn_cond;
 #  define GRN_MKOSTEMP mkostemp
 # else /* HAVE_MKOSTEMP */
 #  define GRN_MKOSTEMP(template,flags) \
-  (mktemp(template), open((template),flags))
+  (mktemp(template), GRN_OPEN((template),flags))
 # endif /* HAVE_MKOSTEMP */
 
 #elif (defined(WIN32) || defined (_WIN64)) /* __GNUC__ */
@@ -451,7 +470,7 @@ typedef int grn_cond;
 # define GRN_BIT_SCAN_REV0 GRN_BIT_SCAN_REV
 
 # define GRN_MKOSTEMP(template,flags) \
-  (mktemp(template), open((template),((flags)|O_BINARY)))
+  (mktemp(template), GRN_OPEN((template),((flags)|O_BINARY)))
 
 #else /* __GNUC__ */
 
@@ -472,7 +491,7 @@ typedef int grn_cond;
 # define GRN_BIT_SCAN_REV0 GRN_BIT_SCAN_REV
 
 # define GRN_MKOSTEMP(template,flags) \
-  (mktemp(template), open((template),flags))
+  (mktemp(template), GRN_OPEN((template),flags))
 
 #endif /* __GNUC__ */
 

  Modified: lib/ii.c (+6 -6)
===================================================================
--- lib/ii.c    2012-02-28 13:10:38 +0900 (e41bc97)
+++ lib/ii.c    2012-02-28 13:30:33 +0900 (f2770b5)
@@ -6627,7 +6627,7 @@ grn_ii_buffer_flush(grn_ctx *ctx, grn_ii_buffer *ii_buffer)
   encode_postings(ctx, ii_buffer, outbuf);
   encode_last_tf(ctx, ii_buffer, outbuf);
   {
-    ssize_t r = write(ii_buffer->tmpfd, outbuf, encsize);
+    ssize_t r = GRN_WRITE(ii_buffer->tmpfd, outbuf, encsize);
     if (r != encsize) {
       ERR(GRN_INPUT_OUTPUT_ERROR, "write returned %d != %d", r, encsize);
       return;
@@ -7137,7 +7137,7 @@ grn_ii_buffer_commit(grn_ctx *ctx, grn_ii_buffer *ii_buffer)
     grn_ii_buffer_flush(ctx, ii_buffer);
   }
   if (ii_buffer->tmpfd != -1) {
-    close(ii_buffer->tmpfd);
+    GRN_CLOSE(ii_buffer->tmpfd);
   }
   if (ii_buffer->block_buf) {
     GRN_FREE(ii_buffer->block_buf);
@@ -7164,9 +7164,9 @@ grn_ii_buffer_commit(grn_ctx *ctx, grn_ii_buffer *ii_buffer)
 
   datavec_init(ctx, ii_buffer->data_vectors, ii_buffer->ii->n_elements, 0, 0);
 #ifdef WIN32
-  ii_buffer->tmpfd = open(ii_buffer->tmpfpath, O_RDONLY|O_BINARY);
+  ii_buffer->tmpfd = GRN_OPEN(ii_buffer->tmpfpath, O_RDONLY|O_BINARY);
 #else /* WIN32 */
-  ii_buffer->tmpfd = open(ii_buffer->tmpfpath, O_RDONLY);
+  ii_buffer->tmpfd = GRN_OPEN(ii_buffer->tmpfpath, O_RDONLY);
 #endif /* WIN32 */
   if (ii_buffer->tmpfd == -1) {
     SERR("oepn");
@@ -7208,7 +7208,7 @@ grn_ii_buffer_commit(grn_ctx *ctx, grn_ii_buffer *ii_buffer)
   datavec_fin(ctx, ii_buffer->data_vectors);
   GRN_LOG(ctx, GRN_LOG_NOTICE, "tmpfile_size:%jd > total_chunk_size:%zu",
           ii_buffer->filepos, ii_buffer->total_chunk_size);
-  close(ii_buffer->tmpfd);
+  GRN_CLOSE(ii_buffer->tmpfd);
   unlink(ii_buffer->tmpfpath);
   ii_buffer->tmpfd = -1;
   return ctx->rc;
@@ -7227,7 +7227,7 @@ grn_ii_buffer_close(grn_ctx *ctx, grn_ii_buffer *ii_buffer)
     grn_obj_close(ctx, ii_buffer->tmp_lexicon);
   }
   if (ii_buffer->tmpfd != -1) {
-    close(ii_buffer->tmpfd);
+    GRN_CLOSE(ii_buffer->tmpfd);
     unlink(ii_buffer->tmpfpath);
   }
   if (ii_buffer->block_buf) {

  Modified: lib/io.c (+6 -6)
===================================================================
--- lib/io.c    2012-02-28 13:10:38 +0900 (dcb0026)
+++ lib/io.c    2012-02-28 13:30:33 +0900 (5f07600)
@@ -381,7 +381,7 @@ grn_io_detect_type(grn_ctx *ctx, const char *path)
 {
   struct _grn_io_header h;
   uint32_t res = 0;
-  int fd = open(path, O_RDWR);
+  int fd = GRN_OPEN(path, O_RDWR);
   if (fd != -1) {
     struct stat s;
     if (fstat(fd, &s) != -1 && s.st_size >= sizeof(struct _grn_io_header)) {
@@ -397,7 +397,7 @@ grn_io_detect_type(grn_ctx *ctx, const char *path)
     } else {
       ERR(GRN_INVALID_FORMAT, "grn_io_detect_type failed");
     }
-    close(fd);
+    GRN_CLOSE(fd);
   } else {
     SERR(path);
   }
@@ -417,7 +417,7 @@ grn_io_open(grn_ctx *ctx, const char *path, grn_io_mode mode)
   if (!path || !*path || (strlen(path) > PATH_MAX - 4)) { return NULL; }
   {
     struct _grn_io_header h;
-    int fd = open(path, O_RDWR);
+    int fd = GRN_OPEN(path, O_RDWR);
     if (fd == -1) { SERR(path); return NULL; }
     if (fstat(fd, &s) != -1 && s.st_size >= sizeof(struct _grn_io_header)) {
       if (read(fd, &h, sizeof(struct _grn_io_header)) == sizeof(struct _grn_io_header)) {
@@ -431,7 +431,7 @@ grn_io_open(grn_ctx *ctx, const char *path, grn_io_mode mode)
         }
       }
     }
-    close(fd);
+    GRN_CLOSE(fd);
     if (!segment_size) { return NULL; }
   }
   total_header_size = IO_HEADER_SIZE + header_size;
@@ -1810,7 +1810,7 @@ inline static grn_rc
 grn_open(grn_ctx *ctx, fileinfo *fi, const char *path, int flags, size_t maxsize)
 {
   struct stat st;
-  if ((fi->fd = open(path, flags, 0666)) == -1) {
+  if ((fi->fd = GRN_OPEN(path, flags, 0666)) == -1) {
     SERR(path);
     return ctx->rc;
   }
@@ -1839,7 +1839,7 @@ inline static grn_rc
 grn_close(grn_ctx *ctx, fileinfo *fi)
 {
   if (fi->fd != -1) {
-    if (close(fi->fd) == -1) {
+    if (GRN_CLOSE(fi->fd) == -1) {
       SERR("close");
       return ctx->rc;
     }

  Modified: lib/proc.c (+4 -4)
===================================================================
--- lib/proc.c    2012-02-28 13:10:38 +0900 (613f1f8)
+++ lib/proc.c    2012-02-28 13:30:33 +0900 (07292d9)
@@ -45,7 +45,7 @@ grn_bulk_put_from_file(grn_ctx *ctx, grn_obj *bulk, const char *path)
   /* FIXME: implement more smartly with grn_bulk */
   int fd, ret = 0;
   struct stat stat;
-  if ((fd = open(path, O_RDONLY|O_NOFOLLOW)) == -1) {
+  if ((fd = GRN_OPEN(path, O_RDONLY|O_NOFOLLOW)) == -1) {
     switch (errno) {
     case EACCES :
       ERR(GRN_OPERATION_NOT_PERMITTED, "request is not allowed: <%s>", path);
@@ -60,7 +60,7 @@ grn_bulk_put_from_file(grn_ctx *ctx, grn_obj *bulk, const char *path)
       break;
 #endif /* WIN32 */
     default :
-      ERR(GRN_UNKNOWN_ERROR, "open() failed(errno: %d): <%s>", errno, path);
+      ERR(GRN_UNKNOWN_ERROR, "GRN_OPEN() failed(errno: %d): <%s>", errno, path);
       break;
     }
     return 0;
@@ -71,7 +71,7 @@ grn_bulk_put_from_file(grn_ctx *ctx, grn_obj *bulk, const char *path)
     if ((buf = GRN_MALLOC(rest))) {
       ssize_t ss;
       for (bp = buf; rest; rest -= ss, bp += ss) {
-        if ((ss = read(fd, bp, rest)) == -1) { goto exit; }
+        if ((ss = GRN_READ(fd, bp, rest)) == -1) { goto exit; }
       }
       GRN_TEXT_PUT(ctx, bulk, buf, stat.st_size);
       ret = 1;
@@ -81,7 +81,7 @@ grn_bulk_put_from_file(grn_ctx *ctx, grn_obj *bulk, const char *path)
     ERR(GRN_INVALID_ARGUMENT, "cannot stat file: <%s>", path);
   }
 exit :
-  close(fd);
+  GRN_CLOSE(fd);
   return ret;
 }
 

  Modified: src/groonga.c (+2 -2)
===================================================================
--- src/groonga.c    2012-02-28 13:10:38 +0900 (7cac88e)
+++ src/groonga.c    2012-02-28 13:30:33 +0900 (8e8706a)
@@ -1999,12 +1999,12 @@ do_daemon(char *path)
     _exit(0);
   }
   {
-    int null_fd = open("/dev/null", O_RDWR, 0);
+    int null_fd = GRN_OPEN("/dev/null", O_RDWR, 0);
     if (null_fd != -1) {
       dup2(null_fd, 0);
       dup2(null_fd, 1);
       dup2(null_fd, 2);
-      if (null_fd > 2) { close(null_fd); }
+      if (null_fd > 2) { GRN_CLOSE(null_fd); }
     }
   }
 #endif /* WIN32 */




Groonga-commit メーリングリストの案内
Back to archive index