int lp_config_read_relative_file(const LpConfig *lpconfig, const char *filename, char *data, size_t max_length) { char *dup_config_file = NULL; const char *dir = NULL; char *filepath = NULL; int fd = 0; bctbx_vfs_file_t* pFile = NULL; char* realfilepath = NULL; if (lpconfig->filename == NULL) return -1; dup_config_file = ms_strdup(lpconfig->filename); dir = _lp_config_dirname(dup_config_file); filepath = ms_strdup_printf("%s/%s", dir, filename); realfilepath = lp_realpath(filepath, NULL); if(realfilepath == NULL) { ms_error("Could not resolv %s: %s", filepath, strerror(errno)); goto err; } pFile = bctbx_file_open(lpconfig->g_bctbx_vfs,realfilepath,"r"); if (pFile !=NULL) fd = pFile->fd; if(fd == -1 ) { ms_error("Could not open %s for read.", realfilepath); goto err; } if(bctbx_file_read(pFile, data, 1, max_length) < 0){ ms_error("%s could not be loaded.", realfilepath); goto err; } bctbx_file_close(pFile); ms_free(dup_config_file); ms_free(filepath); ms_free(realfilepath); return 0; err: ms_free(dup_config_file); ms_free(filepath); if(realfilepath) ms_free(realfilepath); return -1; }
/** * Gets a line of max_len length and stores it to the allocaed buffer s. * Reads at most max_len characters from the file descriptor associated with the argument pFile * and looks for an end of line character (\r or \n). Stores the line found * into the buffer pointed by s. * Modifies the open file offset using pFile->offset. * * @param pFile File handle pointer. * @param s Buffer where to store the line. * @param max_len Maximum number of characters to read in one fetch. * @return size of line read, 0 if empty */ static int bcGetLine(bctbx_vfs_file_t *pFile, char *s, int max_len) { int64_t ret; int sizeofline, isEof; char *pNextLine = NULL; char *pNextLineR = NULL; char *pNextLineN = NULL; if (pFile->fd == -1) { return BCTBX_VFS_ERROR; } if (s == NULL || max_len < 1) { return BCTBX_VFS_ERROR; } sizeofline = 0; isEof = 0; s[max_len-1] = '\0'; /* Read returns 0 if end of file is found */ ret = bctbx_file_read(pFile, s, max_len - 1, pFile->offset); if (ret > 0) { pNextLineR = strchr(s, '\r'); pNextLineN = strchr(s, '\n'); if ((pNextLineR != NULL) && (pNextLineN != NULL)) pNextLine = MIN(pNextLineR, pNextLineN); else if (pNextLineR != NULL) pNextLine = pNextLineR; else if (pNextLineN != NULL) pNextLine = pNextLineN; if (pNextLine) { /* Got a line! */ *pNextLine = '\0'; sizeofline = pNextLine - s + 1; if (pNextLine[1] == '\n') sizeofline += 1; /*take into account the \r\n" case*/ /* offset to next beginning of line*/ pFile->offset += sizeofline; } else { /*did not find end of line char, is EOF?*/ sizeofline = (int)ret; pFile->offset += sizeofline; s[ret] = '\0'; } } else if (ret < 0) { bctbx_error("bcGetLine error"); } return sizeofline; }
/** * Read count bytes from the open file given by p, starting at offset and puts them in * the buffer pointed by buf. * Calls bctbx_file_read. * * @param p sqlite3_file file handle pointer. * @param buf buffer to write the read bytes to. * @param count number of bytes to read * @param offset file offset where to start reading * @return SQLITE_OK if read bytes equals count, * SQLITE_IOERR_SHORT_READ if the number of bytes read is inferior to count * SQLITE_IOERR_READ if an error occurred. */ static int sqlite3bctbx_Read(sqlite3_file *p, void *buf, int count, sqlite_int64 offset){ int ret; sqlite3_bctbx_file_t *pFile = (sqlite3_bctbx_file_t*) p; if (pFile){ ret = bctbx_file_read(pFile->pbctbx_file, buf, count, (off_t)offset); if( ret==count ){ return SQLITE_OK; } else if( ret >= 0 ){ /*fill in unread portion of buffer, as requested by sqlite3 documentation*/ memset(((uint8_t*)buf) + ret, 0, count-ret); return SQLITE_IOERR_SHORT_READ; }else { return SQLITE_IOERR_READ; } } return SQLITE_IOERR_READ; }