[Groonga-commit] groonga/groonga at 6de407e [master] io: add grn_io_is_corrupt()

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Wed Jun 14 15:42:15 JST 2017


Kouhei Sutou	2017-06-14 15:42:15 +0900 (Wed, 14 Jun 2017)

  New Revision: 6de407e27d555f25ae9e5f1b0d5f4a553745a115
  https://github.com/groonga/groonga/commit/6de407e27d555f25ae9e5f1b0d5f4a553745a115

  Message:
    io: add grn_io_is_corrupt()
    
    It just detects that used "PATH.XXX" doesn't exist case.

  Modified files:
    lib/grn_io.h
    lib/io.c

  Modified: lib/grn_io.h (+2 -1)
===================================================================
--- lib/grn_io.h    2017-06-12 18:48:22 +0900 (9ba3bc5)
+++ lib/grn_io.h    2017-06-14 15:42:15 +0900 (99eabb7)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
-  Copyright(C) 2009-2016 Brazil
+  Copyright(C) 2009-2017 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -313,6 +313,7 @@ GRN_API grn_rc grn_io_lock(grn_ctx *ctx, grn_io *io, int timeout);
 GRN_API void grn_io_unlock(grn_io *io);
 void grn_io_clear_lock(grn_io *io);
 uint32_t grn_io_is_locked(grn_io *io);
+grn_bool grn_io_is_corrupt(grn_ctx *ctx, grn_io *io);
 
 #define GRN_IO_ARRAY_AT(io,array,offset,flags,res) do {\
   grn_io_array_info *ainfo = &(io)->ainfo[array];\

  Modified: lib/io.c (+36 -9)
===================================================================
--- lib/io.c    2017-06-12 18:48:22 +0900 (913d24d)
+++ lib/io.c    2017-06-14 15:42:15 +0900 (36e682d)
@@ -785,6 +785,14 @@ gen_pathname(const char *path, char *buffer, int fno)
   }
 }
 
+static uint32_t
+grn_io_n_files(grn_ctx *ctx, grn_io *io)
+{
+  unsigned long file_size;
+  file_size = grn_io_compute_file_size(io->header->version);
+  return ((io->header->curr_size + file_size - 1) / file_size);
+}
+
 grn_rc
 grn_io_size(grn_ctx *ctx, grn_io *io, uint64_t *size)
 {
@@ -792,16 +800,10 @@ grn_io_size(grn_ctx *ctx, grn_io *io, uint64_t *size)
   struct stat s;
   uint64_t tsize = 0;
   char buffer[PATH_MAX];
-  uint32_t nfiles;
+  uint32_t n_files;
 
-  if (io->header->curr_size) {
-    unsigned long file_size;
-    file_size = grn_io_compute_file_size(io->header->version);
-    nfiles = (uint32_t) ((io->header->curr_size + file_size - 1) / file_size);
-  } else {
-    nfiles = grn_io_max_n_files(io);
-  }
-  for (fno = 0; fno < nfiles; fno++) {
+  n_files = grn_io_n_files(ctx, io);
+  for (fno = 0; fno < n_files; fno++) {
     gen_pathname(io->path, buffer, fno);
     if (stat(buffer, &s)) {
       SERR("failed to stat path to compute size: <%s>",
@@ -1496,6 +1498,31 @@ grn_io_flush(grn_ctx *ctx, grn_io *io)
   return rc;
 }
 
+grn_bool
+grn_io_is_corrupt(grn_ctx *ctx, grn_io *io)
+{
+  uint32_t i;
+  uint32_t n_files;
+
+  if (!io) {
+    return GRN_FALSE;
+  }
+
+  n_files = grn_io_n_files(ctx, io);
+  for (i = 0; i < n_files; i++) {
+    char path[PATH_MAX];
+    struct stat s;
+    gen_pathname(io->path, path, i);
+    if (stat(path, &s) != 0) {
+      SERR("[io][corrupt] used path doesn't exist: <%s>",
+           path);
+      return GRN_FALSE;
+    }
+  }
+
+  return GRN_TRUE;
+}
+
 /** mmap abstraction **/
 
 static size_t mmap_size = 0;
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る