[Groonga-commit] groonga/groonga at bb0be9a [master] Add grn_obj_remove_force()

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Aug 26 15:18:23 JST 2016


Kouhei Sutou	2016-08-26 15:18:23 +0900 (Fri, 26 Aug 2016)

  New Revision: bb0be9adf90dd9b2dd90cb59da4cb60274a2c09c
  https://github.com/groonga/groonga/commit/bb0be9adf90dd9b2dd90cb59da4cb60274a2c09c

  Message:
    Add grn_obj_remove_force()

  Modified files:
    include/groonga/groonga.h
    lib/db.c
    lib/grn_io.h
    lib/io.c

  Modified: include/groonga/groonga.h (+3 -0)
===================================================================
--- include/groonga/groonga.h    2016-08-26 14:52:48 +0900 (a79c1ae)
+++ include/groonga/groonga.h    2016-08-26 15:18:23 +0900 (aaf33c6)
@@ -738,6 +738,9 @@ GRN_API int grn_obj_get_values(grn_ctx *ctx, grn_obj *obj, grn_id offset, void *
 GRN_API grn_rc grn_obj_set_value(grn_ctx *ctx, grn_obj *obj, grn_id id, grn_obj *value, int flags);
 GRN_API grn_rc grn_obj_remove(grn_ctx *ctx, grn_obj *obj);
 GRN_API grn_rc grn_obj_remove_dependent(grn_ctx *ctx, grn_obj *obj);
+GRN_API grn_rc grn_obj_remove_force(grn_ctx *ctx,
+                                    const char *name,
+                                    int name_size);
 GRN_API grn_rc grn_obj_rename(grn_ctx *ctx, grn_obj *obj,
                               const char *name, unsigned int name_size);
 GRN_API grn_rc grn_table_rename(grn_ctx *ctx, grn_obj *table,

  Modified: lib/db.c (+40 -0)
===================================================================
--- lib/db.c    2016-08-26 14:52:48 +0900 (20c8055)
+++ lib/db.c    2016-08-26 15:18:23 +0900 (2c7bbc1)
@@ -9757,6 +9757,46 @@ grn_obj_remove_dependent(grn_ctx *ctx, grn_obj *obj)
 }
 
 grn_rc
+grn_obj_remove_force(grn_ctx *ctx, const char *name, int name_size)
+{
+  grn_rc rc = GRN_SUCCESS;
+  grn_obj *db;
+  grn_id obj_id;
+  char path[PATH_MAX];
+
+  GRN_API_ENTER;
+
+  if (!(ctx->impl && ctx->impl->db)) {
+    ERR(GRN_INVALID_ARGUMENT,
+        "[object][remove][force] database isn't initialized");
+    rc = ctx->rc;
+    goto exit;
+  }
+
+  db = ctx->impl->db;
+  if (name_size == -1) {
+    name_size = strlen(name);
+  }
+  obj_id = grn_table_get(ctx, db, name, name_size);
+  if (obj_id == GRN_ID_NIL) {
+    ERR(GRN_INVALID_ARGUMENT,
+        "[object][remove][force] nonexistent object: <%.*s>",
+        name_size, name);
+    rc = ctx->rc;
+    goto exit;
+  }
+
+  grn_obj_delete_by_id(ctx, db, obj_id, GRN_TRUE);
+  grn_obj_path_by_id(ctx, db, obj_id, path);
+  grn_io_remove_if_exist(ctx, path);
+  grn_strcat(path, PATH_MAX, ".c");
+  grn_io_remove_if_exist(ctx, path);
+
+exit :
+  GRN_API_RETURN(rc);
+}
+
+grn_rc
 grn_table_update_by_id(grn_ctx *ctx, grn_obj *table, grn_id id,
                        const void *dest_key, unsigned int dest_key_size)
 {

  Modified: lib/grn_io.h (+1 -0)
===================================================================
--- lib/grn_io.h    2016-08-26 14:52:48 +0900 (4bb02d4)
+++ lib/grn_io.h    2016-08-26 15:18:23 +0900 (9ba3bc5)
@@ -117,6 +117,7 @@ GRN_API grn_io *grn_io_create(grn_ctx *ctx, const char *path,
 grn_io *grn_io_open(grn_ctx *ctx, const char *path, grn_io_mode mode);
 GRN_API grn_rc grn_io_close(grn_ctx *ctx, grn_io *io);
 grn_rc grn_io_remove(grn_ctx *ctx, const char *path);
+grn_rc grn_io_remove_if_exist(grn_ctx *ctx, const char *path);
 grn_rc grn_io_size(grn_ctx *ctx, grn_io *io, uint64_t *size);
 grn_rc grn_io_rename(grn_ctx *ctx, const char *old_name, const char *new_name);
 GRN_API void *grn_io_header(grn_io *io);

  Modified: lib/io.c (+42 -20)
===================================================================
--- lib/io.c    2016-08-26 14:52:48 +0900 (90d5807)
+++ lib/io.c    2016-08-26 15:18:23 +0900 (8942d69)
@@ -814,32 +814,54 @@ grn_io_size(grn_ctx *ctx, grn_io *io, uint64_t *size)
 }
 
 grn_rc
-grn_io_remove(grn_ctx *ctx, const char *path)
+grn_io_remove_raw(grn_ctx *ctx, const char *path)
 {
-  struct stat s;
-  if (stat(path, &s)) {
-    SERR("failed to stat: <%s>", path);
-    return ctx->rc;
-  } else if (grn_unlink(path)) {
+  grn_rc rc = GRN_SUCCESS;
+  int fno;
+  char buffer[PATH_MAX];
+
+  if (grn_unlink(path) != 0) {
     ERRNO_ERR("failed to remove path: <%s>",
               path);
     return ctx->rc;
-  } else {
-    int fno;
-    char buffer[PATH_MAX];
-    for (fno = 1; ; fno++) {
-      gen_pathname(path, buffer, fno);
-      if (!stat(buffer, &s)) {
-        if (grn_unlink(buffer)) {
-          ERRNO_ERR("failed to remove path: <%s>",
-                    buffer);
-        }
-      } else {
-        break;
-      }
+  }
+
+  for (fno = 1; ; fno++) {
+    struct stat s;
+    gen_pathname(path, buffer, fno);
+    if (stat(buffer, &s) != 0) {
+      break;
     }
-    return GRN_SUCCESS;
+    if (grn_unlink(buffer) != 0) {
+      ERRNO_ERR("failed to remove path: <%s>",
+                buffer);
+      rc = ctx->rc;
+    }
+  }
+  return rc;
+}
+
+grn_rc
+grn_io_remove(grn_ctx *ctx, const char *path)
+{
+  struct stat s;
+
+  if (stat(path, &s) != 0) {
+    SERR("failed to stat: <%s>", path);
+    return ctx->rc;
+  }
+
+  return grn_io_remove_raw(ctx, path);
+}
+
+grn_rc
+grn_io_remove_if_exist(grn_ctx *ctx, const char *path)
+{
+  struct stat s;
+  if (stat(path, &s) == 0) {
+    return grn_io_remove_raw(ctx, path);
   }
+  return GRN_SUCCESS;
 }
 
 grn_rc
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index