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 return rc;
00130 }
00131
00138 int
00139 netsnmp_file_open(netsnmp_file * filei)
00140 {
00141
00142
00143
00144 if ((NULL == filei) || (NULL == filei->name))
00145 return -1;
00146
00147
00148
00149
00150 if (-1 != filei->fd)
00151 return filei->fd;
00152
00153
00154
00155
00156 if (0 == filei->mode)
00157 filei->fd = open(filei->name, filei->fs_flags);
00158 else
00159 filei->fd = open(filei->name, filei->fs_flags, filei->mode);
00160
00161 if (filei->fd < 0) {
00162 snmp_log(LOG_ERR, "error opening %s (%d)\n", filei->name, errno);
00163 }
00164
00165
00166
00167
00168 return filei->fd;
00169 }
00170
00171
00178 int
00179 netsnmp_file_close(netsnmp_file * filei)
00180 {
00181 int rc;
00182
00183
00184
00185
00186 if ((NULL == filei) || (NULL != filei->name))
00187 return -1;
00188
00189
00190
00191
00192 if (-1 == filei->fd) {
00193 return 0;
00194 }
00195
00196
00197
00198
00199 rc = close(filei->fd);
00200 if (rc < 0) {
00201 snmp_log(LOG_ERR, "error closing %s (%d)\n", filei->name, errno);
00202 }
00203 else
00204 filei->fd = -1;
00205
00206 return rc;
00207 }
00208