📄 ircp_io.c
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <time.h>#include "obex.h"#include "obex_main.h"#include "ircp_io.h"#include "types.h"//// Get some file-info. (size and lastmod)//static int get_fileinfo(const char *name, char *lastmod){ struct stat stats; struct tm *tm; memset(&stats,0,sizeof(struct stat)); stat(name, &stats); tm = gmtime(&stats.st_mtime); snprintf(lastmod, 21, "%04d-%02d-%02dT%02d:%02d:%02dZ", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); return (int) stats.st_size;}//// Create an object from a file. Attach some info-headers to it//obex_object_t *build_object_from_file(obex_t *handle, const char *localname, const char *remotename){ obex_object_t *object = NULL; obex_headerdata_t hdd; uint8_t *ucname; int ucname_len, size; char lastmod[21*2] = {"1970-01-01T00:00:00Z"}; /* Get filesize and modification-time */ size = get_fileinfo(localname, lastmod); object = OBEX_ObjectNew(handle, OBEX_CMD_PUT); if(object == NULL) return NULL; ucname_len = strlen(remotename)*2 + 2; ucname = malloc(ucname_len); if(ucname == NULL) { DEBUG(4,"can not alloc memory\n"); goto err; } ucname_len = OBEX_CharToUnicode(ucname, (uint8_t *) remotename, ucname_len); hdd.bs = ucname; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_NAME, hdd, ucname_len, 0); free(ucname); hdd.bq4 = size; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_LENGTH, hdd, sizeof(uint32_t), 0); #if 0 /* Win2k excpects this header to be in unicode. I suspect this in incorrect so this will have to wait until that's investigated */ hdd.bs = lastmod; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_TIME, hdd, strlen(lastmod)+1, 0);#endif hdd.bs = NULL; OBEX_ObjectAddHeader(handle, object, OBEX_HDR_BODY, hdd, 0, OBEX_FL_STREAM_START); DEBUG(4, "Lastmod = %s\n", lastmod); return object; err: if(object != NULL) OBEX_ObjectDelete(handle, object); return NULL;}//// Check for dangerous filenames.//static int ircp_nameok(const char *name){ DEBUG(4, "\n"); /* No abs paths */ if(name[0] == '/') return FALSE; if(strlen(name) >= 3) { /* "../../vmlinuz" */ if(name[0] == '.' && name[1] == '.' && name[2] == '/') return FALSE; /* "dir/../../../vmlinuz" */ if(strstr(name, "/../") != NULL) return FALSE; } return TRUE;}//// Open a file, but do some sanity-checking first.//int ircp_open_safe(const char *path, const char *name){#ifndef EMMI_SPARC#define MAXPATHLEN 255#endif char diskname[MAXPATHLEN]; int fd; DEBUG(4, "\n"); /* Check for dangerous filenames */ if(ircp_nameok(name) == FALSE) { return -1; } //TODO! Rename file if already exist. snprintf(diskname, MAXPATHLEN, "%s/%s", path, name); fd = srv_dfile_open(diskname); DEBUG(5, "Creating file %s %d\n", diskname,fd); return fd;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -