00001 #include <net-snmp/net-snmp-config.h>
00002 #include <net-snmp/net-snmp-includes.h>
00003
00004 #include <stdio.h>
00005 #include <ctype.h>
00006 #if HAVE_STDLIB_H
00007 # include <stdlib.h>
00008 #endif
00009 #if HAVE_UNISTD_H
00010 # include <unistd.h>
00011 #endif
00012 #if HAVE_STRING_H
00013 # include <string.h>
00014 #else
00015 # include <strings.h>
00016 #endif
00017
00018 #include <sys/types.h>
00019
00020 #if HAVE_SYS_PARAM_H
00021 # include <sys/param.h>
00022 #endif
00023 #ifdef HAVE_SYS_STAT_H
00024 # include <sys/stat.h>
00025 #endif
00026 #ifdef HAVE_FCNTL_H
00027 # include <fcntl.h>
00028 #endif
00029
00030 #include <errno.h>
00031
00032 #if HAVE_DMALLOC_H
00033 # include <dmalloc.h>
00034 #endif
00035
00036 #include <net-snmp/types.h>
00037 #include <net-snmp/library/container.h>
00038 #include <net-snmp/library/file_utils.h>
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00062 netsnmp_file *
00063 netsnmp_file_create(void)
00064 {
00065 netsnmp_file *filei = SNMP_MALLOC_TYPEDEF(netsnmp_file);
00066
00067
00068
00069
00070 if (NULL != filei)
00071 filei->fd = -1;
00072 else {
00073 snmp_log(LOG_WARNING,"failed to malloc netsnmp_file structure\n");
00074 }
00075
00076 return filei;
00077 }
00078
00088 netsnmp_file *
00089 netsnmp_file_fill(netsnmp_file * filei, const char* name,
00090 int fs_flags, mode_t mode, u_int ns_flags)
00091 {
00092 if (NULL == filei) {
00093 filei = netsnmp_file_create();
00094 if (NULL == filei)
00095 return NULL;
00096 }
00097
00098 if (NULL != name)
00099 filei->name = strdup(name);
00100
00101 filei->fs_flags = fs_flags;
00102 filei->ns_flags = ns_flags;
00103
00104 return filei;
00105 }
00106
00112 int
00113 netsnmp_file_release(netsnmp_file * filei)
00114 {
00115 int rc = 0;
00116
00117 if (NULL == filei)
00118 return -1;
00119
00120 if ((filei->fd > 0) && NS_FI_AUTOCLOSE(filei->ns_flags))
00121 rc = close(filei->fd);
00122
00123 if (NULL != filei->name)
00124 free(filei->name);
00125
00126 if (NULL != filei->extras)
00127 netsnmp_free_all_list_data(filei->extras);
00128
00129 SNMP_FREE(filei);
00130
00131 return rc;
00132 }
00133
00140 int
00141 netsnmp_file_open(netsnmp_file * filei)
00142 {
00143
00144
00145
00146 if ((NULL == filei) || (NULL == filei->name))
00147 return -1;
00148
00149
00150
00151
00152 if (-1 != filei->fd)
00153 return filei->fd;
00154
00155
00156
00157
00158 if (0 == filei->mode)
00159 filei->fd = open(filei->name, filei->fs_flags);
00160 else
00161 filei->fd = open(filei->name, filei->fs_flags, filei->mode);
00162
00163 if (filei->fd < 0) {
00164 snmp_log(LOG_ERR, "error opening %s (%d)\n", filei->name, errno);
00165 }
00166
00167
00168
00169
00170 return filei->fd;
00171 }
00172
00173
00180 int
00181 netsnmp_file_close(netsnmp_file * filei)
00182 {
00183 int rc;
00184
00185
00186
00187
00188 if ((NULL == filei) || (NULL != filei->name))
00189 return -1;
00190
00191
00192
00193
00194 if (-1 == filei->fd) {
00195 return 0;
00196 }
00197
00198
00199
00200
00201 rc = close(filei->fd);
00202 if (rc < 0) {
00203 snmp_log(LOG_ERR, "error closing %s (%d)\n", filei->name, errno);
00204 }
00205 else
00206 filei->fd = -1;
00207
00208 return rc;
00209 }
00210