updates.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 17 Feb 2002 15:19:40 +0000 (15:19 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 17 Feb 2002 15:19:40 +0000 (15:19 +0000)
22 files changed:
CHANGES
TODO
apps/irssi/src/silc/core/client_ops.c
apps/silcd/command.c
apps/silcd/command_reply.c
apps/silcd/idlist.h
apps/silcd/packet_receive.c
apps/silcd/server.c
apps/silcd/server_util.c
apps/silcd/serverconfig.c
apps/silcd/serverconfig.h
doc/example_silcd.conf.in
includes/silcincludes.h
lib/silcclient/client.c
lib/silcclient/idlist.c
lib/silcsftp/sftp_fs_memory.c
lib/silcutil/DIRECTORY
lib/silcutil/Makefile.am
lib/silcutil/silcfileutil.c [new file with mode: 0644]
lib/silcutil/silcfileutil.h [new file with mode: 0644]
lib/silcutil/silcutil.c
lib/silcutil/silcutil.h

diff --git a/CHANGES b/CHANGES
index 460b7846993255523c34057651c31198c82f18d2..0faf7a25fc53eefe0c127b98851c2b96d65fe7e4 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,24 @@
+Sun Feb 17 15:52:30 EET 2002  Pekka Riikonen <priikone@silcnet.org>
+
+       * 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 <priikone@silcnet.org>
 
        * ROBOdoc documented the lib/silcske/silcske.h, and improved
diff --git a/TODO b/TODO
index c2c96454a661ecd68868f6fbf47f404d493b2f9b..abc1a8ac5584e889593aceab3d9b490ab0e4e400 100644 (file)
--- 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.
 
index 11b33fc0c1422641e98e6b129a50d7b622ae7b87..7f968a6fb75d61e5792ab94a77d0277f319e93c4 100644 (file)
@@ -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;
index 5221c09efd9079f105257f52ae8408e272366703..7a4d7aa7e45236952d2371d2f1a0b8776564062b 100644 (file)
@@ -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,
index 68e6143337f85ff6aaa16c92a911ce1a0d80b35f..d191b6b66a4ade12d1d8fb508270bc8ab385d678 100644 (file)
@@ -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);
index 74d054f7b22938b4efa21056b9d699596f01f52a..b2a7a413b6a2b8cf118c90c3130bfb65da64687f 100644 (file)
@@ -479,6 +479,7 @@ struct SilcChannelEntryStruct {
 
   /* All users on this channel */
   SilcHashTable user_list;
+  uint32 user_count;
 
   /* Pointer to the router */
   SilcServerEntry router;
index 98800ee11f801a6dd84b06f57c64f297e1bac3a9..7d210e83c24a71bc8517c65fd539b7afd0ba2943 100644 (file)
@@ -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;
 
index c2caa843ab5c8fe977e536bf8e2d3c3d0b264540..04912e9c18567d12f98eada3fe9a031bd8d10216 100644 (file)
@@ -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++;
     }
   }
 }
index 3592df581202c158e9c4b1dce0f4388ef048f929..0827924c29b6d5f3bace247a525e45a8162cbc27 100644 (file)
@@ -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);
index d6754da5b95177a85a1d1c339bee0928f7388063..7177c103d8fd65cd230ac53fd730dc00f35c6eb3 100644 (file)
@@ -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) {
index 4508c71783d748007a9821c034fec8556a3f041f..f06ab5796bab5d0fe8213d6824c7a457ec15e372 100644 (file)
@@ -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 *
index 29dd23a7d42e8ecf838f82ccbce39d42a242084f..89dc2f26ba545b74f1c5d2f0161600cdf61d9bc0 100644 (file)
@@ -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 {
index 4569bafef5b6a06dcea744b3720c8349c61180a1..d15953faaaa13c1b3817af9020f1ae47d0af1a68 100644 (file)
@@ -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"
index a49973f429df5f64fd2c134465812ad440215ce2..4674c0921b230087a9333778f49f46929b200e8e 100644 (file)
@@ -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);
 }
 
index aa5ccc27f22e86c2a4355bd92bb54822f49625b4..fdb4eece395d59d44c492b1da8759094e2ffa7ff 100644 (file)
@@ -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. */
index b2c3d84d740a8a8a2bc2a34a36006b51c8a83a0e..33d2fcb88f09361b939b82fde6d212d605e2ec61 100644 (file)
@@ -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) {
index c02522fd4cb7500473505ead75027d3f44677a61..9fce1564d9d9b3b4f906eb1bd77ed35e1b12fb76 100644 (file)
@@ -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
index 07fb94619bb4d008c72a9fc45fa7446a6a6ff5ad..a88f243ba4c8cd2cbc21b3c3e77b06c310d41ec5 100644 (file)
@@ -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 (file)
index 0000000..4cb3e2f
--- /dev/null
@@ -0,0 +1,178 @@
+/*
+
+  silcfileutil.c 
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  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 (file)
index 0000000..8f717b6
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+
+  silcfileutil.h 
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  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 */
index 21d779e653576e750437739955bda79cdc2c55d1..2cf26fe92df9f0011cd3b36b95e07e4f7a246391 100644 (file)
 
 #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. */
index a8e2addad0fc7317d53047acc3a4907e32044609..5a31b43d0aa866e2e984e526158c01bdb8eb9fe6 100644 (file)
 #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();