--- /dev/null
+/* Copyright (c) 2006-2013. 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. */
+
+/********************* Files and Storage handling ****************************
+ * This example implements all main storage and file functions of the MSG API
+ *
+ * Scenario :
+ * - display information on the disks mounted by the current host
+ * - create a 200,000 bytes file
+ * - completely read the created file
+ * - write 100,000 more bytes in the file
+ * - rename the created file
+ * - attach some user data to a disk
+ * - dump disk's contents
+ *
+******************************************************************************/
+
+#include "msg/msg.h"
+#include "xbt/log.h"
+
+ /* To use PRIu64 format specifier for printing uint64_t (sg_storage_size_t) */
+#include <inttypes.h>
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
+
+int host(int argc, char *argv[]){
+ const char* host_name = MSG_host_get_name(MSG_host_self());
+
+ // display information on the disks mounted by the current host
+ XBT_INFO("*** Storage info on %s ***", host_name);
+
+ xbt_dict_cursor_t cursor = NULL;
+ char* mount_name;
+ char* storage_name;
+ msg_storage_t storage;
+
+ // Retrieve all mount points of current host
+ xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self());
+
+ xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) {
+ // For each disk mounted on host
+
+ XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
+ storage = MSG_storage_get_by_name(storage_name);
+
+ // Retrieve disk's information
+ sg_storage_size_t free_size = MSG_storage_get_free_size(mount_name);
+ sg_storage_size_t used_size = MSG_storage_get_used_size(mount_name);
+ sg_storage_size_t size = MSG_storage_get_size(storage);
+
+ XBT_INFO("Total size: %"PRIu64" bytes", size);
+ XBT_INFO("Free size: %"PRIu64" bytes", free_size);
+ XBT_INFO("Used size: %"PRIu64" bytes", used_size);
+ }
+ xbt_free(storage_list);
+
+
+ // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
+
+ char* mount = xbt_strdup("/sd1");
+ char* file_name = xbt_strdup("./tmp/data.txt");
+ msg_file_t file = NULL;
+ sg_storage_size_t write, read, file_size;
+
+ // Open an non-existing file amounts to create it!
+ file = MSG_file_open(mount, file_name, NULL);
+ write = MSG_file_write(file, 200000); // Write 200,000 bytes
+ XBT_INFO("Create a %"PRIu64" bytes file named '%s' on /sd1", write, file_name);
+ MSG_file_dump(file);
+
+ // check that sizes have changed
+ XBT_INFO("Total size: %"PRIu64" bytes", MSG_storage_get_free_size("/sd1"));
+ XBT_INFO("Free size: %"PRIu64" bytes", MSG_storage_get_used_size("/sd1"));
+
+
+ // Now retrieve the size of created file and read it completely
+ file_size = MSG_file_get_size(file);
+ read = MSG_file_read(file, file_size);
+ XBT_INFO("Read %"PRIu64" bytes on %s", read, file_name);
+
+ // Now write 100,000 more bytes in tmp/data.txt
+ write = MSG_file_write(file, 100000); // Write 100,000 bytes
+ XBT_INFO("Write %"PRIu64" more bytes on %s", write, file_name);
+ MSG_file_dump(file);
+
+ MSG_file_close(file);
+ free(mount);
+ free(file_name);
+
+ // Now rename file from /tmp/data.txt to /tmp/simgrid.readme
+ msg_storage_t st = MSG_storage_get_by_name("/sd1");
+ MSG_storage_file_rename(st, "/tmp/data.txt", "/tmp/simgrid.readme");
+
+ // Now attach some user data to disk1
+ storage_name = xbt_strdup("disk1");
+ XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
+
+ storage = MSG_storage_get_by_name(storage_name);
+ char *data = MSG_storage_get_data(storage);
+
+ XBT_INFO("Get data: '%s'", data);
+
+ MSG_storage_set_data(storage,strdup("Some user data"));
+ data = MSG_storage_get_data(storage);
+ XBT_INFO("Set and get data: '%s'", data);
+ free(storage_name);
+
+
+ // Dump disks contents
+ XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
+ xbt_dict_t contents = NULL;
+ contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
+ xbt_dict_cursor_t curs, curs2 = NULL;
+ char* mountname;
+ xbt_dict_t content;
+ char* path;
+ sg_storage_size_t *size;
+ xbt_dict_foreach(contents, curs, mountname, content){
+ XBT_INFO("Print the content of mount point: %s",mountname);
+ xbt_dict_foreach(content,curs2,path,size){
+ XBT_INFO("%s size: %"PRIu64" bytes", path,*((sg_storage_size_t*)size));
+ }
+ }
+
+ return 1;
+}
+
+
+int main(int argc, char *argv[])
+{
+
+ MSG_init(&argc, argv);
+ /* Check the arguments */
+ if (argc < 3) {
+ printf("Usage: %s platform_file deployment_file \n", argv[0]);
+ return -1;
+ }
+
+ const char *platform_file = argv[1];
+ const char *deployment_file = argv[2];
+
+ MSG_create_environment(platform_file);
+ MSG_function_register("host", host);
+ MSG_launch_application(deployment_file);
+
+ msg_error_t res = MSG_main();
+ XBT_INFO("Simulated time: %g", MSG_get_clock());
+
+ if (res == MSG_OK)
+ return 0;
+ else
+ return 1;
+}
XBT_PUBLIC(void) MSG_file_dump(msg_file_t fd);
XBT_PUBLIC(int) MSG_file_unlink(msg_file_t fd);
XBT_PUBLIC(xbt_dict_t) MSG_file_ls(const char *mount, const char *path);
-XBT_PUBLIC(msg_error_t) MSG_file_move (msg_file_t fd, msg_host_t dest, char* mount, char* fullname);
-XBT_PUBLIC(msg_error_t) MSG_file_seek (msg_file_t fd, sg_storage_size_t offset, int whence);
-XBT_PUBLIC(msg_error_t) MSG_file_rename (msg_file_t fd, const char* new_name);
+XBT_PUBLIC(msg_error_t) MSG_file_seek(msg_file_t fd, sg_storage_size_t offset, int whence);
XBT_PUBLIC(void) __MSG_file_get_info(msg_file_t fd);
/************************** Storage handling ***********************************/
XBT_PUBLIC(msg_host_t) MSG_get_storage_by_name(const char *name);
XBT_PUBLIC(void *) MSG_storage_get_data(msg_storage_t storage);
XBT_PUBLIC(xbt_dict_t) MSG_storage_get_content(msg_storage_t storage);
XBT_PUBLIC(sg_storage_size_t) MSG_storage_get_size(msg_storage_t storage);
+XBT_PUBLIC(msg_error_t) MSG_storage_file_move(msg_file_t fd, msg_host_t dest, char* mount, char* fullname);
+XBT_PUBLIC(msg_error_t) MSG_storage_file_rename(msg_storage_t storage, const char* src, const char* dest);
/************************** AS Router handling ************************************/
XBT_PUBLIC(const char *) MSG_as_router_get_property_value(const char* asr, const char *name);
XBT_PUBLIC(xbt_dict_t) MSG_as_router_get_properties(const char* asr);
XBT_PUBLIC(xbt_dict_t) simcall_file_ls(const char* mount, const char* path);
XBT_PUBLIC(sg_storage_size_t) simcall_file_get_size(smx_file_t fd);
XBT_PUBLIC(xbt_dynar_t) simcall_file_get_info(smx_file_t fd);
-XBT_PUBLIC(void) simcall_file_rename(smx_file_t fd, const char* new_name);
/***************************** Storage **********************************/
XBT_PUBLIC(sg_storage_size_t) simcall_storage_get_free_size (const char* name);
XBT_PUBLIC(sg_storage_size_t) simcall_storage_get_used_size (const char* name);
XBT_PUBLIC(xbt_dict_t) simcall_storage_get_content(smx_storage_t storage);
XBT_PUBLIC(const char*) SIMIX_storage_get_name(smx_host_t host);
XBT_PUBLIC(sg_storage_size_t) SIMIX_storage_get_size(smx_storage_t storage);
+XBT_PUBLIC(void) simcall_storage_file_rename(smx_storage_t storage, const char* src, const char* dest);
/************************** AS router **********************************/
XBT_PUBLIC(xbt_dict_t) SIMIX_asr_get_properties(const char *name);
/************************** AS router simcalls ***************************/
surf_action_t(*stat) (void *storage, surf_file_t fd);
surf_action_t(*ls) (void *storage, const char *path);
xbt_dict_t(*get_properties) (const void *storage);
+ void (*rename) (const void *storage, const char *src, const char *dest);
xbt_dict_t(*get_content) (void *storage);
sg_storage_size_t(*get_size) (void *storage);
} s_surf_model_extension_storage_t;
surf_action_t(*ls) (void *workstation, const char* mount, const char *path);
sg_storage_size_t (*get_size) (void *workstation, surf_file_t fd);
xbt_dynar_t (*get_info) (void *workstation, surf_file_t fd);
- void (*rename) (void *workstation, surf_file_t fd, const char *new_name);
int (*link_shared) (const void *link);
xbt_dict_t(*get_properties) (const void *resource);
void (*add_traces) (void);
return simcall_file_ls(mount, path);
}
-/*
- * Move a file to another location. Depending on the values of dest, dest, mount,
- * and fullname, this move can be local or remote and, within a host, on the same
- * mounted disk or between mounted disks.
- *
- */
-msg_error_t MSG_file_move (msg_file_t fd, msg_host_t dest, char* mount, char* fullname)
-{
- THROW_UNIMPLEMENTED;
- return MSG_OK;
-}
-
/*
* Set the file position indicator in the msg_file_t by adding offset bytes
* to the position specified by whence (either SEEK_SET, SEEK_CUR, or SEEK_END).
return MSG_OK;
}
-/*
- * Rename the file in the contents of its associated storage.
- */
-msg_error_t MSG_file_rename (msg_file_t fd, const char* new_name)
-{
- simcall_file_rename(fd->simdata->smx_file, new_name);
- return MSG_OK;
-}
-
/********************************* Storage **************************************/
/** @addtogroup msg_storage_management
return SIMIX_storage_get_size(storage);
}
+/*
+ * Rename the file in the contents of its associated storage.
+ */
+msg_error_t MSG_storage_file_rename(msg_storage_t storage, const char* src, const char* dest)
+{
+ simcall_storage_file_rename(storage, src, dest);
+ return MSG_OK;
+}
+
+/*
+ * Move a file to another location. Depending on the values of dest, dest, mount,
+ * and fullname, this move can be local or remote and, within a host, on the same
+ * mounted disk or between mounted disks.
+ *
+ */
+msg_error_t MSG_storage_file_move (msg_file_t fd, msg_host_t dest, char* mount, char* fullname)
+{
+ THROW_UNIMPLEMENTED;
+ return MSG_OK;
+}
fd->surf_file);
}
-void SIMIX_pre_file_rename(smx_simcall_t simcall, smx_file_t fd, const char* new_name)
+void SIMIX_pre_storage_file_rename(smx_simcall_t simcall, smx_storage_t storage, const char* src, const char* dest)
{
- return SIMIX_file_rename(simcall->issuer, fd, new_name);
+ return SIMIX_storage_file_rename(simcall->issuer, storage, src, dest);
}
-void SIMIX_file_rename(smx_process_t process, smx_file_t fd, const char* new_name)
+void SIMIX_storage_file_rename(smx_process_t process, smx_storage_t storage, const char* src, const char* dest)
{
- smx_host_t host = process->smx_host;
- return surf_workstation_model->extension.workstation.rename(host,
- fd->surf_file, new_name);
+ return surf_workstation_model->extension.storage.rename(storage, src, dest);
}
-
sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
{
return SIMIX_storage_get_free_size(simcall->issuer, name);
const char* mount, const char* path);
sg_storage_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd);
xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd);
-void SIMIX_pre_file_rename(smx_simcall_t simcall, smx_file_t fd, const char* new_name);
+void SIMIX_pre_storage_file_rename(smx_simcall_t simcall,smx_storage_t storage, const char* src, const char* dest);
void* SIMIX_file_get_data(smx_file_t fd);
void SIMIX_file_set_data(smx_file_t fd, void *data);
const char *path);
sg_storage_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd);
xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd);
-void SIMIX_file_rename(smx_process_t process, smx_file_t fd, const char* new_name);
+void SIMIX_storage_file_rename(smx_process_t process, smx_storage_t storage, const char* src, const char* dest);
sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall,const char* name);
sg_storage_size_t SIMIX_storage_get_free_size(smx_process_t process,const char* name);
ACTION(SIMCALL_FILE_LS, file_ls, WITHOUT_ANSWER, TSPEC(result, xbt_dict_t), TSTRING(mount), TSTRING(path)) sep \
ACTION(SIMCALL_FILE_GET_SIZE, file_get_size, WITH_ANSWER, TSIZE(result), TSPEC(fd, smx_file_t)) sep \
ACTION(SIMCALL_FILE_GET_INFO, file_get_info, WITH_ANSWER, TSPEC(result, xbt_dynar_t), TSPEC(fd, smx_file_t)) sep \
-ACTION(SIMCALL_FILE_RENAME, file_rename, WITH_ANSWER, TVOID(result), TSPEC(fd, smx_file_t), TSTRING(new_name)) sep \
+ACTION(SIMCALL_STORAGE_FILE_RENAME, storage_file_rename, WITH_ANSWER, TVOID(result), TSPEC(storage, smx_storage_t), TSTRING(src), TSTRING(dest)) sep \
ACTION(SIMCALL_STORAGE_GET_FREE_SIZE, storage_get_free_size, WITH_ANSWER, TSIZE(result), TSTRING(name)) sep \
ACTION(SIMCALL_STORAGE_GET_USED_SIZE, storage_get_used_size, WITH_ANSWER, TSIZE(result), TSTRING(name)) sep \
ACTION(SIMCALL_STORAGE_GET_PROPERTIES, storage_get_properties, WITH_ANSWER, TSPEC(result, xbt_dict_t), TSPEC(storage, smx_storage_t)) sep \
* \ingroup simix_file_management
*
*/
-void simcall_file_rename(smx_file_t fd, const char* new_name)
+void simcall_storage_file_rename(smx_storage_t storage, const char* src, const char* dest)
{
- return simcall_BODY_file_rename(fd, new_name);
+ return simcall_BODY_storage_file_rename(storage, src, dest);
}
/**
return ((storage_t)storage_resource)->size;
}
+static void storage_file_rename(const void *storage, const char *src, const char *dest)
+{
+ void *storage_resource = surf_storage_resource_priv(storage);
+
+ sg_storage_size_t *psize;
+ psize = (sg_storage_size_t*) xbt_dict_get_or_null(((storage_t)storage_resource)->content,src);
+ if (psize){// src file exists
+ xbt_dict_remove(((storage_t)storage_resource)->content, src);
+ xbt_dict_set(((storage_t)storage_resource)->content, dest, psize,NULL);
+ XBT_DEBUG("Change file name from %s to %s, size '%" PRIu64 "'",src, dest, *psize);
+ }
+ else
+ XBT_DEBUG("File %s doesn't exist",src);
+}
+
+
static void* storage_create_resource(const char* id, const char* model,
const char* type_id, const char* content_name, const char* content_type, xbt_dict_t properties){
storage_t storage = NULL;
surf_storage_model->extension.storage.get_properties = storage_get_properties;
surf_storage_model->extension.storage.get_content = storage_get_content;
surf_storage_model->extension.storage.get_size = storage_get_size;
+ surf_storage_model->extension.storage.rename = storage_file_rename;
if (!storage_maxmin_system) {
storage_maxmin_system = lmm_system_new(storage_selective_update);
}
return info;
}
-static void ws_file_rename(void *workstation, surf_file_t fd, const char* new_name)
-{
- storage_t storage = find_storage_on_mount_list(workstation, fd->mount);
-
- const char* old_full_name = fd->name;
- xbt_dynar_t dyn = NULL;
- const char* separator;
- const char* ctype = storage->content_type;
-
- // TODO: PV: use an enum and a switch case to manage content type properly
- if(!strcmp(ctype, "txt_unix"))
- separator = strdup("/");
- else
- separator = strdup("\\");
-
- // Split file with separator and replace file name
- dyn = xbt_str_split(old_full_name, separator);
- xbt_dynar_pop_ptr(dyn);
- xbt_dynar_push(dyn, &new_name);
- char *new_full_name = xbt_str_join(dyn, separator);
-
- sg_storage_size_t *psize;
- psize = (sg_storage_size_t*) xbt_dict_get_or_null(storage->content,old_full_name);
- xbt_dict_remove(storage->content, old_full_name);
- xbt_dict_set(storage->content,new_full_name,psize,NULL);
- XBT_DEBUG("Change file name from %s to %s, size '%" PRIu64 "'",fd->name, new_full_name, *psize);
-}
-
static sg_storage_size_t ws_storage_get_free_size(void *workstation,const char* name)
{
storage_t st = find_storage_on_mount_list(workstation, name);
surf_workstation_model->extension.workstation.ls = ws_action_ls;
surf_workstation_model->extension.workstation.get_size = ws_file_get_size;
surf_workstation_model->extension.workstation.get_info = ws_file_get_info;
- surf_workstation_model->extension.workstation.rename = ws_file_rename;
surf_workstation_model->extension.workstation.get_free_size = ws_storage_get_free_size;
surf_workstation_model->extension.workstation.get_used_size = ws_storage_get_used_size;
surf_workstation_model->extension.workstation.get_storage_list = ws_get_storage_list;