int onlp_file_vwrite(uint8_t* data, int len, const char* fmt, va_list vargs) { int fd; char fname[PATH_MAX]; int rv; int wlen; ONLPLIB_VSNPRINTF(fname, sizeof(fname)-1, fmt, vargs); if ((fd = open(fname, O_WRONLY)) == -1) { rv = ONLP_STATUS_E_MISSING; } else { if ((wlen = write(fd, data, len)) != len) { AIM_LOG_ERROR("Failed to write output file '%s'", fname); rv = ONLP_STATUS_E_INTERNAL; } else { rv = ONLP_STATUS_OK; } close(fd); } return rv; }
int onlp_file_vopen(int flags, int log, const char* fmt, va_list vargs) { int rv; char fname[PATH_MAX]; ONLPLIB_VSNPRINTF(fname, sizeof(fname)-1, fmt, vargs); rv = open(fname, flags); if(rv < 0 && log) { AIM_LOG_ERROR("failed to open file %s (0x%x): %{errno}", fname, flags, errno); } return rv; }
/** * @brief Open a file or domain socket. * @param dst Receives the full filename (for logging purposes). * @param flags The open flags. * @param fmt Format specifier. * @param vargs Format specifier arguments. */ static int vopen__(char** dst, int flags, const char* fmt, va_list vargs) { int fd; struct stat sb; char fname[PATH_MAX]; char* asterisk; ONLPLIB_VSNPRINTF(fname, sizeof(fname)-1, fmt, vargs); /** * An asterisk in the filename separates a search root * directory from a filename. */ if( (asterisk = strchr(fname, '*')) ) { char* root = fname; char* rpath = NULL; *asterisk = 0; if(onlp_file_find(root, asterisk+1, &rpath) < 0) { return ONLP_STATUS_E_MISSING; } strcpy(fname, rpath); aim_free(rpath); } if(dst) { *dst = aim_strdup(fname); } if(stat(fname, &sb) == -1) { return ONLP_STATUS_E_MISSING; } if(S_ISSOCK(sb.st_mode)) { fd = ds_connect__(fname); } else { fd = open(fname, flags); } return (fd > 0) ? fd : ONLP_STATUS_E_MISSING; }
int onlp_file_vread(uint8_t* data, int max, int* len, const char* fmt, va_list vargs) { int fd; char fname[PATH_MAX]; int rv; ONLPLIB_VSNPRINTF(fname, sizeof(fname)-1, fmt, vargs); if ((fd = open(fname, O_RDONLY)) == -1) { rv = ONLP_STATUS_E_MISSING; } else { if ((*len = read(fd, data, max)) <= 0) { AIM_LOG_ERROR("Failed to read input file '%s'", fname); rv = ONLP_STATUS_E_INTERNAL; } else { rv = ONLP_STATUS_OK; } close(fd); } return rv; }