一直帮老板搬运代码!!!
全部博文(116)
分类: LINUX
2013-02-06 10:07:10
打开:
fd = ngx_open_file(filename->data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
#define ngx_open_file(name, mode, create, access) \
open((const char *) name, mode|create, access)
获取文件的信息:
if (ngx_fd_info(fd, &cf->conf_file->file.info) == -1) {
#define ngx_fd_info(fd, sb) fstat(fd, sb)
读取:
//从文件里面读取信息
n = ngx_read_file(&cf->conf_file->file, b->start + len, size, cf->conf_file->file.offset);
ssize_t
ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset)
{
ssize_t n;
ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
"read: %d, %p, %uz, %O", file->fd, buf, size, offset);
#if (NGX_HAVE_PREAD)
n = pread(file->fd, buf, size, offset); //从偏移量开始读数据,第一次把所有的东西都读出来
if (n == -1) {
ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
"pread() \"%s\" failed", file->name.data);
return NGX_ERROR;
}
#else
if (file->sys_offset != offset) {
if (lseek(file->fd, offset, SEEK_SET) == -1) {
ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
"lseek() \"%s\" failed", file->name.data);
return NGX_ERROR;
}
file->sys_offset = offset;
}
n = read(file->fd, buf, size);
if (n == -1) {
ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
"read() \"%s\" failed", file->name.data);
return NGX_ERROR;
}
file->sys_offset += n;
#endif
file->offset += n;
return n;
}