From 06de75789d7eb830f8358b1b784e82b9941b33c8 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Sun, 17 Feb 2002 15:19:40 +0000 Subject: [PATCH] updates. --- CHANGES | 21 +++ TODO | 5 - apps/irssi/src/silc/core/client_ops.c | 19 ++- apps/silcd/command.c | 3 +- apps/silcd/command_reply.c | 2 + apps/silcd/idlist.h | 1 + apps/silcd/packet_receive.c | 1 + apps/silcd/server.c | 9 +- apps/silcd/server_util.c | 2 + apps/silcd/serverconfig.c | 35 +++-- apps/silcd/serverconfig.h | 3 +- doc/example_silcd.conf.in | 27 ++-- includes/silcincludes.h | 1 + lib/silcclient/client.c | 12 +- lib/silcclient/idlist.c | 4 + lib/silcsftp/sftp_fs_memory.c | 31 ++--- lib/silcutil/DIRECTORY | 1 + lib/silcutil/Makefile.am | 2 + lib/silcutil/silcfileutil.c | 178 ++++++++++++++++++++++++++ lib/silcutil/silcfileutil.h | 43 +++++++ lib/silcutil/silcutil.c | 151 ---------------------- lib/silcutil/silcutil.h | 9 -- 22 files changed, 329 insertions(+), 231 deletions(-) create mode 100644 lib/silcutil/silcfileutil.c create mode 100644 lib/silcutil/silcfileutil.h diff --git a/CHANGES b/CHANGES index 460b7846..0faf7a25 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,24 @@ +Sun Feb 17 15:52:30 EET 2002 Pekka Riikonen + + * Added `user_count' to the SilcChannelEntry which now tells the + number of users on the channel. The user count is now saved + in normal server of global channels as well. Affected files + silcd/server.c, idlist.h, packet_receive.c and command.c. + + * Splitted lib/silcutil/silcutil.[ch] into silcfileutil.[ch] to + include file utility functions. + + * Fixed the lib/silcsftp/sftp_fs_memory.c to use silcutil routines + instead of calling directly OS routines. + + * Fixed NICK change printing in Irssi SILC Client. Fixed + KICKED notify printing in Irssi SILC Client. Affected file + irssi/src/silc/core/client_ops.c. + + * Fixed a NICK change bug in client library, to not recreate the + client_entry->channels hash table everytime nick is changed. + Affected file lib/silcclient/client.c. + Sun Feb 17 10:10:14 EET 2002 Pekka Riikonen * ROBOdoc documented the lib/silcske/silcske.h, and improved diff --git a/TODO b/TODO index c2c96454..abc1a8ac 100644 --- a/TODO +++ b/TODO @@ -50,8 +50,6 @@ TODO/bugs In SILC Server no founder on channel at all), the router will accept the server's founder mode change, even though it perhaps should not do that. - o Make the normal server save user counts with LIST command reply. - o The router should check for validity of received notify packets from servers (after all buggy servers may send notify that is actually something that should have not been sent). @@ -85,9 +83,6 @@ TODO/bugs In SILC Libraries o Rewrite the lib/silcsim/silcsim.h. The SilcSimContext should be private and silc_sim_alloc should take necessary arguments. - o lib/silcsftp/sftp_fs_memory.c use directly open(), close() etc. - routines. Change to use silc_file_* routines. - o SILC RNG does not implement random seed files, and they should be implemented. diff --git a/apps/irssi/src/silc/core/client_ops.c b/apps/irssi/src/silc/core/client_ops.c index 11b33fc0..7f968a6f 100644 --- a/apps/irssi/src/silc/core/client_ops.c +++ b/apps/irssi/src/silc/core/client_ops.c @@ -332,15 +332,14 @@ void silc_notify(SilcClient client, SilcClientConnection conn, client_entry = va_arg(va, SilcClientEntry); client_entry2 = va_arg(va, SilcClientEntry); - nicklist_rename_unique(SERVER(server), - client_entry, client_entry->nickname, - client_entry2, client_entry2->nickname); - memset(userhost, 0, sizeof(userhost)); snprintf(userhost, sizeof(userhost) - 1, "%s@%s", client_entry2->username, client_entry2->hostname); + nicklist_rename_unique(SERVER(server), + client_entry, client_entry->nickname, + client_entry2, client_entry2->nickname); signal_emit("message nick", 4, server, client_entry2->nickname, - client_entry2->nickname, userhost); + client_entry->nickname, userhost); break; case SILC_NOTIFY_TYPE_CMODE_CHANGE: @@ -460,8 +459,8 @@ void silc_notify(SilcClient client, SilcClientConnection conn, if (client_entry == conn->local_entry) { printformat_module("fe-common/silc", server, channel->channel_name, MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED_YOU, - client_entry2->nickname, - channel->channel_name, tmp ? tmp : ""); + channel->channel_name, client_entry2->nickname, + tmp ? tmp : ""); if (chanrec) { chanrec->kicked = TRUE; channel_destroy((CHANNEL_REC *)chanrec); @@ -469,9 +468,8 @@ void silc_notify(SilcClient client, SilcClientConnection conn, } else { printformat_module("fe-common/silc", server, channel->channel_name, MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED, - client_entry->nickname, - client_entry2->nickname, - channel->channel_name, tmp ? tmp : ""); + client_entry->nickname, channel->channel_name, + client_entry2->nickname, tmp ? tmp : ""); if (chanrec) { SILC_NICK_REC *nickrec = silc_nicklist_find(chanrec, client_entry); @@ -1033,7 +1031,6 @@ silc_command_reply(SilcClient client, SilcClientConnection conn, nicklist_rename_unique(SERVER(server), server->conn->local_entry, server->nick, client, client->nickname); - signal_emit("message own_nick", 4, server, server->nick, old, ""); g_free(old); break; diff --git a/apps/silcd/command.c b/apps/silcd/command.c index 5221c09e..7a4d7aa7 100644 --- a/apps/silcd/command.c +++ b/apps/silcd/command.c @@ -2165,7 +2165,7 @@ silc_server_command_list_send_reply(SilcServerCommandContext cmd, memset(usercount, 0, sizeof(usercount)); } else { topic = entry->topic; - users = silc_hash_table_count(entry->user_list); + users = entry->user_count; SILC_PUT32_MSB(users, usercount); } @@ -3187,6 +3187,7 @@ static void silc_server_command_join_channel(SilcServer server, chl->channel = channel; silc_hash_table_add(channel->user_list, client, chl); silc_hash_table_add(client->channels, channel, chl); + channel->user_count++; /* Get users on the channel */ silc_server_get_users_on_channel(server, channel, &user_list, &mode_list, diff --git a/apps/silcd/command_reply.c b/apps/silcd/command_reply.c index 68e61433..d191b6b6 100644 --- a/apps/silcd/command_reply.c +++ b/apps/silcd/command_reply.c @@ -1201,6 +1201,8 @@ SILC_SERVER_CMD_REPLY_FUNC(list) cache->expire = time(NULL) + 60; } + channel->user_count = usercount; + if (topic) { silc_free(channel->topic); channel->topic = strdup(topic); diff --git a/apps/silcd/idlist.h b/apps/silcd/idlist.h index 74d054f7..b2a7a413 100644 --- a/apps/silcd/idlist.h +++ b/apps/silcd/idlist.h @@ -479,6 +479,7 @@ struct SilcChannelEntryStruct { /* All users on this channel */ SilcHashTable user_list; + uint32 user_count; /* Pointer to the router */ SilcServerEntry router; diff --git a/apps/silcd/packet_receive.c b/apps/silcd/packet_receive.c index 98800ee1..7d210e83 100644 --- a/apps/silcd/packet_receive.c +++ b/apps/silcd/packet_receive.c @@ -228,6 +228,7 @@ void silc_server_notify(SilcServer server, silc_hash_table_add(channel->user_list, client, chl); silc_hash_table_add(client->channels, channel, chl); silc_free(client_id); + channel->user_count++; break; diff --git a/apps/silcd/server.c b/apps/silcd/server.c index c2caa843..04912e9c 100644 --- a/apps/silcd/server.c +++ b/apps/silcd/server.c @@ -1041,9 +1041,9 @@ silc_server_accept_new_connection_lookup(SilcSocketConnection sock, port = server->sockets[server->sock]->port; /* Listenning port */ /* Check whether this connection is denied to connect to us. */ - deny = silc_server_config_find_denied(server, sock->ip, port); + deny = silc_server_config_find_denied(server, sock->ip); if (!deny) - deny = silc_server_config_find_denied(server, sock->hostname, port); + deny = silc_server_config_find_denied(server, sock->hostname); if (deny) { /* The connection is denied */ SILC_LOG_INFO(("Connection %s (%s) is denied", @@ -2642,6 +2642,7 @@ void silc_server_remove_from_channels(SilcServer server, /* Remove client from channel's client list */ silc_hash_table_del(channel->user_list, chl->client); + channel->user_count--; /* If there is no global users on the channel anymore mark the channel as local channel. Do not check if the removed client is local client. */ @@ -2679,6 +2680,7 @@ void silc_server_remove_from_channels(SilcServer server, while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) { silc_hash_table_del(chl2->client->channels, channel); silc_hash_table_del(channel->user_list, chl2->client); + channel->user_count--; silc_free(chl2); } silc_hash_table_list_reset(&htl2); @@ -2766,6 +2768,7 @@ int silc_server_remove_from_one_channel(SilcServer server, /* Remove client from channel's client list */ silc_hash_table_del(channel->user_list, chl->client); + channel->user_count--; /* If there is no global users on the channel anymore mark the channel as local channel. Do not check if the client is local client. */ @@ -2802,6 +2805,7 @@ int silc_server_remove_from_one_channel(SilcServer server, while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) { silc_hash_table_del(chl2->client->channels, channel); silc_hash_table_del(channel->user_list, chl2->client); + channel->user_count--; silc_free(chl2); } silc_hash_table_list_reset(&htl2); @@ -3880,6 +3884,7 @@ void silc_server_save_users_on_channel(SilcServer server, chl->channel = channel; silc_hash_table_add(channel->user_list, chl->client, chl); silc_hash_table_add(client->channels, chl->channel, chl); + channel->user_count++; } } } diff --git a/apps/silcd/server_util.c b/apps/silcd/server_util.c index 3592df58..0827924c 100644 --- a/apps/silcd/server_util.c +++ b/apps/silcd/server_util.c @@ -71,6 +71,7 @@ static void silc_server_remove_clients_channels(SilcServer server, /* Remove client from channel's client list */ silc_hash_table_del(channel->user_list, chl->client); + channel->user_count--; /* If there is no global users on the channel anymore mark the channel as local channel. Do not check if the removed client is local client. */ @@ -103,6 +104,7 @@ static void silc_server_remove_clients_channels(SilcServer server, while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) { silc_hash_table_del(chl2->client->channels, channel); silc_hash_table_del(channel->user_list, chl2->client); + channel->user_count--; silc_free(chl2); } silc_hash_table_list_reset(&htl2); diff --git a/apps/silcd/serverconfig.c b/apps/silcd/serverconfig.c index d6754da5..7177c103 100644 --- a/apps/silcd/serverconfig.c +++ b/apps/silcd/serverconfig.c @@ -779,15 +779,6 @@ SILC_CONFIG_CALLBACK(fetch_deny) CONFIG_IS_DOUBLE(tmp->host); tmp->host = (*(char *)val ? strdup((char *) val) : strdup("*")); } - else if (!strcmp(name, "port")) { - int port = *(int *)val; - if ((port <= 0) || (port > 65535)) { - fprintf(stderr, "Invalid port number!\n"); - got_errno = SILC_CONFIG_ESILENT; - goto got_err; - } - tmp->port = (uint16) port; - } else if (!strcmp(name, "reason")) { CONFIG_IS_DOUBLE(tmp->reason); tmp->reason = strdup((char *) val); @@ -948,6 +939,17 @@ SILC_CONFIG_CALLBACK(fetch_router) tmp->backup_replace_ip = (*(char *)val ? strdup((char *) val) : strdup("*")); } + else if (!strcmp(name, "backupport")) { + int port = *(int *)val; + if ((port <= 0) || (port > 65535)) { + fprintf(stderr, "Invalid port number!\n"); + return SILC_CONFIG_ESILENT; + } + tmp->backup_replace_port = (uint16) port; + } + else if (!strcmp(name, "backuplocal")) { + tmp->backup_local = *(bool *)val; + } else return SILC_CONFIG_EINTERNAL; @@ -1081,7 +1083,6 @@ static const SilcConfigTable table_admin[] = { static const SilcConfigTable table_deny[] = { { "host", SILC_CONFIG_ARG_STRE, fetch_deny, NULL }, - { "port", SILC_CONFIG_ARG_INT, fetch_deny, NULL }, { "reason", SILC_CONFIG_ARG_STR, fetch_deny, NULL }, { 0, 0, 0, 0 } }; @@ -1106,7 +1107,7 @@ static const SilcConfigTable table_routerconn[] = { { "initiator", SILC_CONFIG_ARG_TOGGLE, fetch_router, NULL }, { "backuphost", SILC_CONFIG_ARG_STRE, fetch_router, NULL }, { "backupport", SILC_CONFIG_ARG_INT, fetch_router, NULL }, - { "localbackup", SILC_CONFIG_ARG_TOGGLE, fetch_router, NULL }, + { "backuplocal", SILC_CONFIG_ARG_TOGGLE, fetch_router, NULL }, { 0, 0, 0, 0 } }; @@ -1597,22 +1598,16 @@ silc_server_config_find_admin(SilcServer server, char *host, char *user, return admin; } -/* Returns the denied connection configuration entry by host and port. */ +/* Returns the denied connection configuration entry by host. */ SilcServerConfigDeny * -silc_server_config_find_denied(SilcServer server, char *host, uint16 port) +silc_server_config_find_denied(SilcServer server, char *host) { SilcServerConfig config = server->config; SilcServerConfigDeny *deny; /* make sure we have a value for the matching parameters */ - if (!config || !port) { - SILC_LOG_WARNING(("Bogus: config_find_denied(config=0x%08x, " - "host=0x%08x \"%s\", port=%hu)", - (uint32) config, (uint32) host, host, port)); - return NULL; - } - if (!host) + if (!config || !host) return NULL; for (deny = config->denied; deny; deny = deny->next) { diff --git a/apps/silcd/serverconfig.h b/apps/silcd/serverconfig.h index 4508c717..f06ab579 100644 --- a/apps/silcd/serverconfig.h +++ b/apps/silcd/serverconfig.h @@ -109,7 +109,6 @@ typedef struct SilcServerConfigAdminStruct { /* Holds all configured denied connections from config file */ typedef struct SilcServerConfigDenyStruct { char *host; - uint16 port; char *reason; struct SilcServerConfigDenyStruct *next; } SilcServerConfigDeny; @@ -194,7 +193,7 @@ SilcServerConfigAdmin * silc_server_config_find_admin(SilcServer server, char *host, char *user, char *nick); SilcServerConfigDeny * -silc_server_config_find_denied(SilcServer server, char *host, uint16 port); +silc_server_config_find_denied(SilcServer server, char *host); SilcServerConfigServer * silc_server_config_find_server_conn(SilcServer server, char *host); SilcServerConfigRouter * diff --git a/doc/example_silcd.conf.in b/doc/example_silcd.conf.in index 29dd23a7..89dc2f26 100644 --- a/doc/example_silcd.conf.in +++ b/doc/example_silcd.conf.in @@ -5,6 +5,18 @@ # configuration possibilities and may not actually give any sensible # configuration. For real life example see the examples/ directory. # +# Most of the settings in this file are optional. If some setting is +# mandatory it is mentioned separately. If some setting is omitted it means +# that its builtin default value will be used. Boolean values, that is +# setting something on or off, is done by setting either "true" or "false" +# value, respectively. +# +# The ServerInfo section is mandatory section. Other sections are optional. +# However, if General section is defined it must be defined before the +# ConnectionParams sections. On the other hand, the ConnectionParams section +# must be defined before Client, ServerConnection or RouterConnection +# sections. Other sections can be in free order. +# # # Include global algorithms from the "silcalgs.conf" file. This file @@ -38,7 +50,7 @@ General { #require_reverse_lookup = true; # Maximum number of incoming connections allowed to this server. - # If more attempt to connet they will be refused. + # If more attempt to connect they will be refused. connections_max = 1000; # Maximum number of incoming connections allowed per single host. @@ -98,7 +110,7 @@ General { #conn_auth_timeout = 60; # Channel key rekey interval (seconds). How often channel key is - # regenerated. Note that channel key regenerated also always when + # regenerated. Note that channel key is regenerated also always when # someone joins or leaves the channel. #channel_rekey_secs = 3600; @@ -293,7 +305,7 @@ ConnectionParams { # required. # # Next example connection will match to all incoming client connections, -# and no authentication is required +# and no authentication is required. # Client { #Host = "10.1.*"; @@ -371,7 +383,7 @@ ServerConnection { # option to the IP address of the router that the backup router will # replace if it becomes unavailable. Set also the router's port to the # "BackupPort" option. For normal connection leave both commented. If this -# backup router is in our cell then set the "LocalBackup" option to true. +# backup router is in our cell then set the "BackupLocal" option to true. # If the backup router is in other cell then set it to false. # RouterConnection { @@ -384,7 +396,7 @@ RouterConnection { Initiator = true; #BackupHost = "10.2.1.6"; #BackupPort = 706; - #LocalBackup = true; + #BackupLocal = true; }; # @@ -392,12 +404,11 @@ RouterConnection { # # These connections are denied to connect to our server. # -# The "Reason" field is mandatory, while the "Host" and "Port" fields can be -# omitted to match everything. +# The "Reason" field is mandatory, while the "Host" field can be omitted to +# match everything. # #Deny { # Host = "10.2.1.99"; -# Port = 706; # Reason = "Go away spammer"; #}; #Deny { diff --git a/includes/silcincludes.h b/includes/silcincludes.h index 4569bafe..d15953fa 100644 --- a/includes/silcincludes.h +++ b/includes/silcincludes.h @@ -266,6 +266,7 @@ typedef uint32 * void *; #include "silcbufutil.h" #include "silcbuffmt.h" #include "silcnet.h" +#include "silcfileutil.h" #include "silcutil.h" #include "silcconfig.h" #include "silcprotocol.h" diff --git a/lib/silcclient/client.c b/lib/silcclient/client.c index a49973f4..4674c092 100644 --- a/lib/silcclient/client.c +++ b/lib/silcclient/client.c @@ -1525,12 +1525,15 @@ void silc_client_receive_new_id(SilcClient client, conn->local_entry->username = strdup(client->username); if (!conn->local_entry->hostname) conn->local_entry->hostname = strdup(client->hostname); - conn->local_entry->server = strdup(conn->remote_host); + if (!conn->local_entry->server) + conn->local_entry->server = strdup(conn->remote_host); conn->local_entry->id = conn->local_id; conn->local_entry->valid = TRUE; - conn->local_entry->channels = silc_hash_table_alloc(1, silc_hash_ptr, - NULL, NULL, - NULL, NULL, NULL, TRUE); + if (!conn->local_entry->channels) + conn->local_entry->channels = silc_hash_table_alloc(1, silc_hash_ptr, + NULL, NULL, + NULL, NULL, NULL, + TRUE); /* Put it to the ID cache */ silc_idcache_add(conn->client_cache, strdup(conn->nickname), conn->local_id, @@ -1575,6 +1578,7 @@ void silc_client_remove_from_channels(SilcClient client, silc_hash_table_del(chu->channel->user_list, chu->client); silc_free(chu); } + silc_hash_table_list_reset(&htl); } diff --git a/lib/silcclient/idlist.c b/lib/silcclient/idlist.c index aa5ccc27..fdb4eece 100644 --- a/lib/silcclient/idlist.c +++ b/lib/silcclient/idlist.c @@ -722,6 +722,8 @@ static void silc_client_del_channel_foreach(void *key, void *context, { SilcChannelUser chu = (SilcChannelUser)context; + SILC_LOG_DEBUG(("Start")); + /* Remove the context from the client's channel hash table as that table and channel's user_list hash table share this same context. */ silc_hash_table_del(chu->client->channels, chu->channel); @@ -735,6 +737,8 @@ bool silc_client_del_channel(SilcClient client, SilcClientConnection conn, { bool ret = silc_idcache_del_by_context(conn->channel_cache, channel); + SILC_LOG_DEBUG(("Start")); + /* Free all client entrys from the users list. The silc_hash_table_free will free all the entries so they are not freed at the foreach callback. */ diff --git a/lib/silcsftp/sftp_fs_memory.c b/lib/silcsftp/sftp_fs_memory.c index b2c3d84d..33d2fcb8 100644 --- a/lib/silcsftp/sftp_fs_memory.c +++ b/lib/silcsftp/sftp_fs_memory.c @@ -277,7 +277,7 @@ static bool mem_del_handle(MemFS fs, MemFSFileHandle handle) if (fs->handles[handle->handle] == handle) { fs->handles[handle->handle] = NULL; if (handle->fd != -1) - close(handle->fd); + silc_file_close(handle->fd); silc_free(handle); return TRUE; } @@ -514,9 +514,9 @@ void mem_open(void *context, SilcSFTP sftp, flags |= O_APPEND; /* Attempt to open the file for real. */ - fd = open(entry->data + 7, flags, - (attrs->flags & SILC_SFTP_ATTR_PERMISSIONS ? - attrs->permissions : 0600)); + fd = silc_file_open_mode(entry->data + 7, flags, + (attrs->flags & SILC_SFTP_ATTR_PERMISSIONS ? + attrs->permissions : 0600)); if (fd == -1) { (*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context); return; @@ -538,7 +538,7 @@ void mem_close(void *context, SilcSFTP sftp, int ret; if (h->fd != -1) { - ret = close(h->fd); + ret = silc_file_close(h->fd); if (ret == -1) { (*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL, callback_context); @@ -568,7 +568,7 @@ void mem_read(void *context, SilcSFTP sftp, lseek(h->fd, (off_t)offset, SEEK_SET); /* Attempt to read */ - ret = read(h->fd, data, len); + ret = silc_file_read(h->fd, data, len); if (ret <= 0) { if (!ret) (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context); @@ -599,7 +599,7 @@ void mem_write(void *context, SilcSFTP sftp, lseek(h->fd, (off_t)offset, SEEK_SET); /* Attempt to write */ - ret = write(h->fd, data, data_len); + ret = silc_file_write(h->fd, data, data_len); if (ret <= 0) { (*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL, callback_context); @@ -699,7 +699,7 @@ void mem_readdir(void *context, SilcSFTP sftp, SilcSFTPAttributes attrs; int i; char long_name[256]; - unsigned long filesize = 0; + uint64 filesize = 0; char *date; struct stat stats; @@ -730,18 +730,13 @@ void mem_readdir(void *context, SilcSFTP sftp, *strrchr(date, ':') = '\0'; if (!entry->directory) -#ifndef SILC_WIN32 - if (!lstat(entry->data + 7, &stats)) -#else - if (!stat(entry->data + 7, &stats)) -#endif - filesize = stats.st_size; + filesize = silc_file_size(entry->data + 7); /* Long name format is: drwx------ 1 324210 Apr 8 08:40 mail/ 1234567890 123 12345678 123456789012 */ snprintf(long_name, sizeof(long_name) - 1, - "%c%c%c%c------ %3d %8lu %12s %s%s", + "%c%c%c%c------ %3d %8llu %12s %s%s", (entry->directory ? 'd' : '-'), ((entry->perm & SILC_SFTP_FS_PERM_READ) ? 'r' : '-'), ((entry->perm & SILC_SFTP_FS_PERM_WRITE) ? 'w' : '-'), @@ -800,7 +795,7 @@ void mem_stat(void *context, SilcSFTP sftp, struct stat stats; if (!path || !strlen(path)) - path = (const char *)strdup("/"); + path = (const char *)DIR_SEPARATOR; /* Find such directory */ entry = mem_find_entry_path(fs->root, path); @@ -850,7 +845,7 @@ void mem_lstat(void *context, SilcSFTP sftp, struct stat stats; if (!path || !strlen(path)) - path = (const char *)strdup("/"); + path = (const char *)DIR_SEPARATOR; /* Find such directory */ entry = mem_find_entry_path(fs->root, path); @@ -984,7 +979,7 @@ void mem_realpath(void *context, SilcSFTP sftp, SilcSFTPName name; if (!path || !strlen(path)) - path = (const char *)strdup("/"); + path = (const char *)DIR_SEPARATOR; realpath = mem_expand_path(fs->root, path); if (!realpath) { diff --git a/lib/silcutil/DIRECTORY b/lib/silcutil/DIRECTORY index c02522fd..9fce1564 100644 --- a/lib/silcutil/DIRECTORY +++ b/lib/silcutil/DIRECTORY @@ -13,6 +13,7 @@ @LINK=silcschedule.html:SILC Schedule API @LINK=silcsockconn.html:SILC Socket Connection API @LINK=silcprotocol.html:SILC Protocol API +@LINK=silcfileutil.html:SILC File Util API @LINK=silcutil.html:SILC Util API @LINK=silczip.html:SILC Zip API @LINK=silclist.html:SILC List API diff --git a/lib/silcutil/Makefile.am b/lib/silcutil/Makefile.am index 07fb9461..a88f243b 100644 --- a/lib/silcutil/Makefile.am +++ b/lib/silcutil/Makefile.am @@ -36,6 +36,7 @@ libsilcutil_a_SOURCES = \ silcmemory.c \ silcnet.c \ silcschedule.c \ + silcfileutil.c \ silcutil.c \ silchashtable.c \ silcsockconn.c \ @@ -58,6 +59,7 @@ include_HEADERS = \ silcthread.h \ silclist.h \ silcdlist.h \ + silcfileutil.h \ silcutil.h endif diff --git a/lib/silcutil/silcfileutil.c b/lib/silcutil/silcfileutil.c new file mode 100644 index 00000000..4cb3e2f7 --- /dev/null +++ b/lib/silcutil/silcfileutil.c @@ -0,0 +1,178 @@ +/* + + silcfileutil.c + + Author: Pekka Riikonen + + Copyright (C) 1997 - 2002 Pekka Riikonen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ +/* $Id$ */ + +#include "silcincludes.h" + +/* Opens a file indicated by the filename `filename' with flags indicated + by the `flags'. */ + +int silc_file_open(const char *filename, int flags) +{ + int fd = open(filename, flags, 0600); + return fd; +} + +/* Opens a file indicated by the filename `filename' with flags indicated + by the `flags', and with the specified `mode'. */ + +int silc_file_open_mode(const char *filename, int flags, int mode) +{ + int fd = open(filename, flags, mode); + return fd; +} + +/* Reads data from file descriptor `fd' to `buf'. */ + +int silc_file_read(int fd, unsigned char *buf, uint32 buf_len) +{ + return read(fd, (void *)buf, buf_len); +} + +/* Writes `buffer' of length of `len' to file descriptor `fd. */ + +int silc_file_write(int fd, const char *buffer, uint32 len) +{ + return write(fd, (const void *)buffer, len); +} + +/* Closes file descriptor */ + +int silc_file_close(int fd) +{ + return close(fd); +} + +/* Writes a buffer to the file. */ + +int silc_file_writefile(const char *filename, const char *buffer, uint32 len) +{ + int fd; + + if ((fd = creat(filename, 0644)) == -1) { + SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename, + strerror(errno))); + return -1; + } + + if ((silc_file_write(fd, buffer, len)) == -1) { + SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno))); + silc_file_close(fd); + return -1; + } + + silc_file_close(fd); + + return 0; +} + +/* Writes a buffer to the file. If the file is created specific mode is + set to the file. */ + +int silc_file_writefile_mode(const char *filename, const char *buffer, + uint32 len, int mode) +{ + int fd; + + if ((fd = creat(filename, mode)) == -1) { + SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename, + strerror(errno))); + return -1; + } + + if ((silc_file_write(fd, buffer, len)) == -1) { + SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno))); + silc_file_close(fd); + return -1; + } + + silc_file_close(fd); + + return 0; +} + +/* Reads a file to a buffer. The allocated buffer is returned. Length of + the file read is returned to the return_len argument. */ + +char *silc_file_readfile(const char *filename, uint32 *return_len) +{ + int fd; + char *buffer; + int filelen; + + fd = silc_file_open(filename, O_RDONLY); + if (fd < 0) { + if (errno == ENOENT) + return NULL; + SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno))); + return NULL; + } + + filelen = lseek(fd, (off_t)0L, SEEK_END); + if (filelen < 0) { + silc_file_close(fd); + return NULL; + } + if (lseek(fd, (off_t)0L, SEEK_SET) < 0) { + silc_file_close(fd); + return NULL; + } + + if (filelen < 0) { + SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno))); + silc_file_close(fd); + return NULL; + } + + buffer = silc_calloc(filelen + 1, sizeof(char)); + + if ((silc_file_read(fd, buffer, filelen)) == -1) { + memset(buffer, 0, sizeof(buffer)); + silc_file_close(fd); + SILC_LOG_ERROR(("Cannot read from file %s: %s", filename, + strerror(errno))); + return NULL; + } + + silc_file_close(fd); + buffer[filelen] = EOF; + + if (return_len) + *return_len = filelen; + + return buffer; +} + +/* Returns files size. Returns 0 on error. */ + +uint64 silc_file_size(const char *filename) +{ + int ret; + struct stat stats; + +#ifndef SILC_WIN32 + ret = lstat(filename, &stats); +#else + ret = stat(filename, &stats); +#endif + if (ret < 0) + return 0; + + return (uint64)stats.st_size; +} diff --git a/lib/silcutil/silcfileutil.h b/lib/silcutil/silcfileutil.h new file mode 100644 index 00000000..8f717b6a --- /dev/null +++ b/lib/silcutil/silcfileutil.h @@ -0,0 +1,43 @@ +/* + + silcfileutil.h + + Author: Pekka Riikonen + + Copyright (C) 1997 - 2002 Pekka Riikonen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ + +/****h* silcutil/SilcFileUtilAPI + * + * DESCRIPTION + * + * + ***/ + +#ifndef SILCFILEUTIL_H +#define SILCFILEUTIL_H + +/* Prototypes */ + +int silc_file_open(const char *filename, int flags); +int silc_file_open_mode(const char *filename, int flags, int mode); +int silc_file_read(int fd, unsigned char *buf, uint32 buf_len); +int silc_file_write(int fd, const char *buffer, uint32 len); +int silc_file_close(int fd); +char *silc_file_readfile(const char *filename, uint32 *return_len); +int silc_file_writefile(const char *filename, const char *buffer, uint32 len); +int silc_file_writefile_mode(const char *filename, const char *buffer, + uint32 len, int mode); +uint64 silc_file_size(const char *filename); + +#endif /* SILCFILEUTIL_H */ diff --git a/lib/silcutil/silcutil.c b/lib/silcutil/silcutil.c index 21d779e6..2cf26fe9 100644 --- a/lib/silcutil/silcutil.c +++ b/lib/silcutil/silcutil.c @@ -24,157 +24,6 @@ #include "silcincludes.h" -/* Opens a file indicated by the filename `filename' with flags indicated - by the `flags'. */ - -int silc_file_open(const char *filename, int flags) -{ - int fd; - - fd = open(filename, flags, 0600); - - return fd; -} - -/* Reads data from file descriptor `fd' to `buf'. */ - -int silc_file_read(int fd, unsigned char *buf, uint32 buf_len) -{ - return read(fd, (void *)buf, buf_len); -} - -/* Writes `buffer' of length of `len' to file descriptor `fd. */ - -int silc_file_write(int fd, const char *buffer, uint32 len) -{ - return write(fd, (const void *)buffer, len); -} - -/* Closes file descriptor */ - -int silc_file_close(int fd) -{ - return close(fd); -} - -/* Writes a buffer to the file. */ - -int silc_file_writefile(const char *filename, const char *buffer, uint32 len) -{ - int fd; - - if ((fd = creat(filename, 0644)) == -1) { - SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename, - strerror(errno))); - return -1; - } - - if ((write(fd, buffer, len)) == -1) { - SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno))); - close(fd); - return -1; - } - - close(fd); - - return 0; -} - -/* Writes a buffer to the file. If the file is created specific mode is - set to the file. */ - -int silc_file_writefile_mode(const char *filename, const char *buffer, - uint32 len, int mode) -{ - int fd; - - if ((fd = creat(filename, mode)) == -1) { - SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename, - strerror(errno))); - return -1; - } - - if ((write(fd, buffer, len)) == -1) { - SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno))); - close(fd); - return -1; - } - - close(fd); - - return 0; -} - -/* Reads a file to a buffer. The allocated buffer is returned. Length of - the file read is returned to the return_len argument. */ - -char *silc_file_readfile(const char *filename, uint32 *return_len) -{ - int fd; - char *buffer; - int filelen; - - fd = silc_file_open(filename, O_RDONLY); - if (fd < 0) { - if (errno == ENOENT) - return NULL; - SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno))); - return NULL; - } - - filelen = lseek(fd, (off_t)0L, SEEK_END); - if (filelen < 0) { - close(fd); - return NULL; - } - if (lseek(fd, (off_t)0L, SEEK_SET) < 0) { - close(fd); - return NULL; - } - - if (filelen < 0) { - SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno))); - close(fd); - return NULL; - } - - buffer = silc_calloc(filelen + 1, sizeof(char)); - - if ((read(fd, buffer, filelen)) == -1) { - memset(buffer, 0, sizeof(buffer)); - close(fd); - SILC_LOG_ERROR(("Cannot read from file %s: %s", filename, - strerror(errno))); - return NULL; - } - - close(fd); - buffer[filelen] = EOF; - - if (return_len) - *return_len = filelen; - - return buffer; -} - -/* Returns files size. Returns 0 on error. */ - -uint64 silc_file_size(const char *filename) -{ - int ret; - struct stat stats; - -#ifndef SILC_WIN32 - ret = lstat(filename, &stats); -#else - ret = stat(filename, &stats); -#endif - if (ret < 0) - return 0; - - return (uint64)stats.st_size; -} - /* Gets line from a buffer. Stops reading when a newline or EOF occurs. This doesn't remove the newline sign from the destination buffer. The argument begin is returned and should be passed again for the function. */ diff --git a/lib/silcutil/silcutil.h b/lib/silcutil/silcutil.h index a8e2adda..5a31b43d 100644 --- a/lib/silcutil/silcutil.h +++ b/lib/silcutil/silcutil.h @@ -21,15 +21,6 @@ #define SILCUTIL_H /* Prototypes */ -int silc_file_open(const char *filename, int flags); -int silc_file_read(int fd, unsigned char *buf, uint32 buf_len); -int silc_file_write(int fd, const char *buffer, uint32 len); -int silc_file_close(int fd); -char *silc_file_readfile(const char *filename, uint32 *return_len); -int silc_file_writefile(const char *filename, const char *buffer, uint32 len); -int silc_file_writefile_mode(const char *filename, const char *buffer, - uint32 len, int mode); -uint64 silc_file_size(const char *filename); int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin); int silc_check_line(char *buf); char *silc_get_time(); -- 2.24.0