From: navarro Date: Tue, 31 Jul 2012 15:43:23 +0000 (+0200) Subject: Add function to unlink a file. X-Git-Tag: v3_8~244 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/1632f396f414f85f50611cad05e13c47ad06387e?ds=sidebyside Add function to unlink a file. Add an example for unlink. --- diff --git a/examples/msg/io/CMakeLists.txt b/examples/msg/io/CMakeLists.txt index fbee5bbbae..7036f56cf9 100644 --- a/examples/msg/io/CMakeLists.txt +++ b/examples/msg/io/CMakeLists.txt @@ -3,12 +3,15 @@ cmake_minimum_required(VERSION 2.6) set(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}") add_executable(file file.c) +add_executable(file_unlink file_unlink.c) ### Add definitions for compile if(NOT WIN32) target_link_libraries(file simgrid m pthread) + target_link_libraries(file_unlink simgrid m pthread) else(NOT WIN32) target_link_libraries(file simgrid) + target_link_libraries(file_unlink simgrid) endif(NOT WIN32) set(tesh_files @@ -23,6 +26,7 @@ set(xml_files set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/file.c + ${CMAKE_CURRENT_SOURCE_DIR}/file_unlink.c PARENT_SCOPE ) set(bin_files diff --git a/examples/msg/io/file_unlink.c b/examples/msg/io/file_unlink.c new file mode 100644 index 0000000000..43a54fc816 --- /dev/null +++ b/examples/msg/io/file_unlink.c @@ -0,0 +1,101 @@ +/* Copyright (c) 2008, 2009, 2010. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +/** @addtogroup MSG_examples + * + * @subsection MSG_ex_resources Other resource kinds + * + * This section contains some sparse examples of how to use the other + * kind of resources, such as disk or GPU. These resources are quite + * experimental for now, but here we go anyway. + * + * - io/file.c Example with the disk resource + */ + +#define FILENAME1 "./doc/simgrid/examples/platforms/g5k.xml" + +#include +#include +#include "msg/msg.h" +#include "surf/surf_private.h" + +int host(int argc, char *argv[]); + +XBT_LOG_NEW_DEFAULT_CATEGORY(io_file, + "Messages specific for this io example"); + +int host(int argc, char *argv[]) +{ + msg_file_t file = NULL; + s_msg_stat_t stat; + void *ptr = NULL; + char* mount = bprintf("/home"); + double write; + + // First open + XBT_INFO("\tOpen file '%s'",FILENAME1); + file = MSG_file_open(mount,FILENAME1,"rw"); + + // Print stat + MSG_file_stat(file,&stat); + XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size); + MSG_file_free_stat(&stat); + + // Unlink the file + XBT_INFO("\tUnlink file '%s'",file->name); + MSG_file_unlink(file); + + // Re Open the file wich is in fact created + XBT_INFO("\tOpen file '%s'",FILENAME1); + file = MSG_file_open(mount,FILENAME1,"rw"); + + // Print stat + MSG_file_stat(file,&stat); + XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size); + MSG_file_free_stat(&stat); + + // Write into the new file + write = MSG_file_write(ptr,100000,sizeof(char*),file); // Write for 100Ko + XBT_INFO("\tHaving write %.1f \ton %s",write,file->name); + + // Print the stat + MSG_file_stat(file,&stat); + XBT_INFO("\tFile stat %s Size %.1f",file->name,stat.size); + MSG_file_free_stat(&stat); + + // Close the file + XBT_INFO("\tClose file '%s'",file->name); + MSG_file_close(file); + + free(mount); + + return 0; +} + +int main(int argc, char **argv) +{ + int res; + MSG_init(&argc, argv); + MSG_create_environment(argv[1]); + xbt_dynar_t hosts = MSG_hosts_as_dynar(); + MSG_function_register("host", host); + unsigned long nb_hosts = xbt_dynar_length(hosts); + XBT_INFO("Number of host '%lu'",nb_hosts); + char* name_host = bprintf("0"); + MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) ); + free(name_host); + + xbt_dynar_free(&hosts); + + res = MSG_main(); + XBT_INFO("Simulation time %g", MSG_get_clock()); + MSG_clean(); + if (res == MSG_OK) + return 0; + else + return 1; + +} diff --git a/include/msg/msg.h b/include/msg/msg.h index 2d62058829..e3b5ac1ce8 100644 --- a/include/msg/msg.h +++ b/include/msg/msg.h @@ -79,6 +79,7 @@ XBT_PUBLIC(int) MSG_file_close(msg_file_t fp); XBT_PUBLIC(int) MSG_file_stat(msg_file_t fd, s_msg_stat_t *buf); XBT_PUBLIC(void) MSG_file_free_stat(s_msg_stat_t *stat); +XBT_PUBLIC(int) MSG_file_unlink(msg_file_t fd); /************************** Host handling ***********************************/ XBT_PUBLIC(msg_error_t) MSG_host_set_data(msg_host_t host, void *data); diff --git a/include/simgrid/simix.h b/include/simgrid/simix.h index e27cbaa8c5..55a834b612 100644 --- a/include/simgrid/simix.h +++ b/include/simgrid/simix.h @@ -454,6 +454,7 @@ XBT_PUBLIC(size_t) simcall_file_write(const void* ptr, size_t size, size_t nmemb XBT_PUBLIC(smx_file_t) simcall_file_open(const char* storage, const char* path, const char* mode); XBT_PUBLIC(int) simcall_file_close(smx_file_t fp); XBT_PUBLIC(int) simcall_file_stat(smx_file_t fd, s_file_stat_t *buf); +XBT_PUBLIC(int) simcall_file_unlink(smx_file_t fd); SG_END_DECL() #endif /* _SIMIX_SIMIX_H */ diff --git a/src/include/surf/surf.h b/src/include/surf/surf.h index 7842c9e890..491ea4c3d4 100644 --- a/src/include/surf/surf.h +++ b/src/include/surf/surf.h @@ -223,6 +223,7 @@ typedef struct surf_storage_model_extension_public { surf_action_t(*read) (void *storage, void* ptr, double size, size_t nmemb, surf_file_t stream); surf_action_t(*write) (void *storage, const void* ptr, size_t size, size_t nmemb, surf_file_t stream); surf_action_t(*stat) (void *storage, surf_file_t stream); + surf_action_t(*unlink) (void *storage, surf_file_t stream); void* (*create_resource) (const char* id, const char* model, const char* type_id, const char *content); } s_surf_model_extension_storage_t; @@ -256,6 +257,7 @@ typedef struct surf_workstation_model_extension_public { surf_action_t(*read) (void *workstation, void* ptr, size_t size, size_t nmemb, surf_file_t stream); surf_action_t(*write) (void *workstation, const void* ptr, size_t size, size_t nmemb, surf_file_t stream); surf_action_t(*stat) (void *workstation, surf_file_t stream); + surf_action_t(*unlink) (void *workstation, surf_file_t stream); int (*link_shared) (const void *link); xbt_dict_t(*get_properties) (const void *resource); void* (*link_create_resource) (const char *name, diff --git a/src/msg/msg_io.c b/src/msg/msg_io.c index 7d51d15d75..2b03f677a7 100644 --- a/src/msg/msg_io.c +++ b/src/msg/msg_io.c @@ -115,3 +115,18 @@ void MSG_file_free_stat(s_msg_stat_t *stat) free(stat->user); free(stat->user_rights); } + +/** \ingroup msg_file_management + * \brief Unlink the file pointed by fd + * + * \param fd is the file descriptor (#msg_file_t) + * \return 0 on success or 1 on error + */ +int MSG_file_unlink(msg_file_t fd) +{ + int res = simcall_file_unlink(fd->simdata->smx_file); + free(fd->name); + xbt_free(fd->simdata); + xbt_free(fd); + return res; +} diff --git a/src/simix/smx_io.c b/src/simix/smx_io.c index 31aa2f6c71..7e6f53da2e 100644 --- a/src/simix/smx_io.c +++ b/src/simix/smx_io.c @@ -207,6 +207,42 @@ smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t return action; } +//SIMIX FILE UNLINK +void SIMIX_pre_file_unlink(smx_simcall_t simcall) +{ + smx_action_t action = SIMIX_file_unlink(simcall->issuer, + simcall->file_unlink.fd); + xbt_fifo_push(action->simcalls, simcall); + simcall->issuer->waiting_action = action; +} + +smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd) +{ + smx_action_t action; + smx_host_t host = process->smx_host; + /* check if the host is active */ + if (surf_workstation_model->extension. + workstation.get_state(host->host) != SURF_RESOURCE_ON) { + THROWF(host_error, 0, "Host %s failed, you cannot call this function", + host->name); + } + + action = xbt_mallocator_get(simix_global->action_mallocator); + action->type = SIMIX_ACTION_IO; + action->name = NULL; +#ifdef HAVE_TRACING + action->category = NULL; +#endif + + action->io.host = host; + action->io.surf_io = surf_workstation_model->extension.workstation.unlink(host->host, fd->surf_file); + + surf_workstation_model->action_data_set(action->io.surf_io, action); + XBT_DEBUG("Create io action %p", action); + + return action; +} + void SIMIX_post_io(smx_action_t action) { xbt_fifo_item_t i; @@ -234,6 +270,10 @@ void SIMIX_post_io(smx_action_t action) s_file_stat_t *src = &((action->io.surf_io)->stat); file_stat_copy(src,dst); break; + case SIMCALL_FILE_UNLINK: + xbt_free(simcall->file_unlink.fd); + simcall->file_unlink.result = 0; + break; default: break; } diff --git a/src/simix/smx_io_private.h b/src/simix/smx_io_private.h index c457a02999..6f7d361573 100644 --- a/src/simix/smx_io_private.h +++ b/src/simix/smx_io_private.h @@ -15,12 +15,14 @@ void SIMIX_pre_file_write(smx_simcall_t simcall); void SIMIX_pre_file_open(smx_simcall_t simcall); void SIMIX_pre_file_close(smx_simcall_t simcall); void SIMIX_pre_file_stat(smx_simcall_t simcall); +void SIMIX_pre_file_unlink(smx_simcall_t simcall); smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream); smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream); smx_action_t SIMIX_file_open(smx_process_t process, const char* storage, const char* path, const char* mode); smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp); smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t buf); +smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd); void SIMIX_post_io(smx_action_t action); void SIMIX_io_destroy(smx_action_t action); diff --git a/src/simix/smx_smurf.c b/src/simix/smx_smurf.c index dfb1268757..55a6093c19 100644 --- a/src/simix/smx_smurf.c +++ b/src/simix/smx_smurf.c @@ -522,6 +522,10 @@ void SIMIX_simcall_pre(smx_simcall_t simcall, int value) SIMIX_pre_file_stat(simcall); break; + case SIMCALL_FILE_UNLINK: + SIMIX_pre_file_unlink(simcall); + break; + case SIMCALL_NONE: THROWF(arg_error,0,"Asked to do the noop syscall on %s@%s", SIMIX_process_get_name(simcall->issuer), diff --git a/src/simix/smx_smurf_private.h b/src/simix/smx_smurf_private.h index 6e83587ede..30a1018ee5 100644 --- a/src/simix/smx_smurf_private.h +++ b/src/simix/smx_smurf_private.h @@ -91,7 +91,8 @@ SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_READ),\ SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_WRITE),\ SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_OPEN),\ SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_CLOSE),\ -SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_STAT) +SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_STAT), \ +SIMCALL_ENUM_ELEMENT(SIMCALL_FILE_UNLINK) /* SIMCALL_COMM_IS_LATENCY_BOUNDED and SIMCALL_SET_CATEGORY make things complicated @@ -562,6 +563,11 @@ typedef struct s_smx_simcall { int result; } file_stat; + struct { + smx_file_t fd; + int result; + } file_unlink; + }; } s_smx_simcall_t, *smx_simcall_t; diff --git a/src/simix/smx_user.c b/src/simix/smx_user.c index b09d08e446..c3074c01cb 100644 --- a/src/simix/smx_user.c +++ b/src/simix/smx_user.c @@ -1465,6 +1465,21 @@ int simcall_file_stat(smx_file_t fd, s_file_stat_t *buf) return simcall->file_stat.result; } +/** + * \ingroup simix_file_management + * + */ +int simcall_file_unlink(smx_file_t fd) +{ + smx_simcall_t simcall = SIMIX_simcall_mine(); + simcall->call = SIMCALL_FILE_UNLINK; + simcall->file_unlink.fd = fd; + + SIMIX_simcall_push(simcall->issuer); + + return simcall->file_unlink.result; +} + /* ************************************************************************** */ /** @brief returns a printable string representing a simcall */ diff --git a/src/surf/storage.c b/src/surf/storage.c index 2e868d7c7e..9983513271 100644 --- a/src/surf/storage.c +++ b/src/surf/storage.c @@ -47,6 +47,24 @@ static surf_action_t storage_action_stat(void *storage, surf_file_t stream) return action; } +static surf_action_t storage_action_unlink(void *storage, surf_file_t stream) +{ + surf_action_t action = storage_action_execute(storage,0, UNLINK); + + // Add memory to storage + ((storage_t)storage)->used_size -= stream->content->stat.size; + + // Remove the file from storage + xbt_dict_t content_dict = ((storage_t)storage)->content; + xbt_dict_remove(content_dict,stream->name); + + free(stream->name); + stream->content = NULL; + xbt_free(stream); + + return action; +} + static surf_action_t storage_action_open(void *storage, const char* mount, const char* path, const char* mode) { XBT_DEBUG("\tOpen file '%s'",path); @@ -137,6 +155,7 @@ static surf_action_t storage_action_execute (void *storage, double size, e_surf_ case OPEN: case CLOSE: case STAT: + case UNLINK: break; case READ: lmm_expand(storage_maxmin_system, STORAGE->constraint_read, @@ -146,7 +165,6 @@ static surf_action_t storage_action_execute (void *storage, double size, e_surf_ lmm_expand(storage_maxmin_system, STORAGE->constraint_write, GENERIC_LMM_ACTION(action).variable, 1.0); xbt_dynar_push(((storage_t)storage)->write_actions,&action); - break; } action->type = type; @@ -442,6 +460,7 @@ static void surf_storage_model_init_internal(void) surf_storage_model->extension.storage.read = storage_action_read; surf_storage_model->extension.storage.write = storage_action_write; surf_storage_model->extension.storage.stat = storage_action_stat; + surf_storage_model->extension.storage.unlink = storage_action_unlink; surf_storage_model->extension.storage.create_resource = storage_create_resource; if (!storage_maxmin_system) { diff --git a/src/surf/storage_private.h b/src/surf/storage_private.h index f27d6502b9..5900e8fb3d 100644 --- a/src/surf/storage_private.h +++ b/src/surf/storage_private.h @@ -45,7 +45,7 @@ typedef struct storage { } s_storage_t, *storage_t; typedef enum { - READ=0, WRITE, STAT, OPEN, CLOSE + READ=0, WRITE, STAT, OPEN, CLOSE, UNLINK } e_surf_action_storage_type_t; typedef struct surf_action_storage { diff --git a/src/surf/workstation.c b/src/surf/workstation.c index 62797b0d5e..c92801b35d 100644 --- a/src/surf/workstation.c +++ b/src/surf/workstation.c @@ -343,6 +343,14 @@ static surf_action_t ws_action_stat(void *workstation, surf_file_t stream) return model->extension.storage.stat(st, stream); } +static surf_action_t ws_action_unlink(void *workstation, surf_file_t stream) +{ + storage_t st = find_storage_on_mount_list(workstation, stream->storage); + XBT_DEBUG("UNLINK on disk '%s'",st->generic_resource.name); + surf_model_t model = st->generic_resource.model; + return model->extension.storage.unlink(st, stream); +} + static void surf_workstation_model_init_internal(void) { surf_workstation_model = surf_model_init(); @@ -400,6 +408,7 @@ static void surf_workstation_model_init_internal(void) surf_workstation_model->extension.workstation.read = ws_action_read; surf_workstation_model->extension.workstation.write = ws_action_write; surf_workstation_model->extension.workstation.stat = ws_action_stat; + surf_workstation_model->extension.workstation.unlink = ws_action_unlink; } void surf_workstation_model_init_current_default(void)