Skip to content
Snippets Groups Projects
func_env.c 38.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • 					}
    					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
    						ast_log(LOG_ERROR, "Short read: %s\n", strerror(errno));
    						fclose(ff);
    						return -1;
    					}
    					for (pos = fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
    						LINE_COUNTER(pos, newline_format, count);
    
    						if (length < 0 && count * -1 == length) {
    							length_offset = i + (pos - fbuf);
    						} else if (offset < 0 && count * -1 == (offset - 1)) {
    							/* Found our initial offset.  We're done with reverse motion! */
    							if (newline_format == FF_DOS) {
    								offset_offset = i + (pos - fbuf) + 2;
    							} else {
    								offset_offset = i + (pos - fbuf) + 1;
    							}
    							break;
    						}
    					}
    					if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
    						break;
    					}
    				}
    				/* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
    				if (offset < 0 && offset_offset < 0 && offset == count * -1) {
    					offset_offset = 0;
    				}
    			}
    
    			/* Positve line offset */
    			if (offset > 0) {
    				int64_t count = 0;
    				fseek(ff, 0, SEEK_SET);
    				for (i = 0; i < flength; i += sizeof(fbuf)) {
    					char *pos;
    					if (i + sizeof(fbuf) >= flength) {
    						memset(fbuf, 0, sizeof(fbuf));
    					}
    					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
    						ast_log(LOG_ERROR, "Short read?!!\n");
    						fclose(ff);
    						return -1;
    					}
    					for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
    						LINE_COUNTER(pos, newline_format, count);
    
    						if (count == offset) {
    							offset_offset = i + (pos - fbuf) + 1;
    							break;
    						}
    					}
    					if (offset_offset >= 0) {
    						break;
    					}
    				}
    			}
    
    			if (offset_offset < 0) {
    				ast_log(LOG_ERROR, "Offset '%s' refers to before the beginning of the file!\n", args.offset);
    				fclose(ff);
    				return -1;
    			}
    
    			if (length == 0) {
    				length_offset = offset_offset;
    			} else if (length == LLONG_MAX) {
    				length_offset = flength;
    			}
    
    			/* Positive line length */
    			if (length_offset < 0) {
    				fseeko(ff, offset_offset, SEEK_SET);
    				for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
    					char *pos;
    					if (i + sizeof(fbuf) >= flength) {
    						memset(fbuf, 0, sizeof(fbuf));
    					}
    					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
    						ast_log(LOG_ERROR, "Short read?!!\n");
    						fclose(ff);
    						return -1;
    					}
    					for (pos = fbuf; pos < fbuf + sizeof(fbuf); pos++) {
    						LINE_COUNTER(pos, newline_format, current_length);
    
    						if (current_length == length) {
    							length_offset = i + (pos - fbuf) + 1;
    							break;
    						}
    					}
    					if (length_offset >= 0) {
    						break;
    					}
    				}
    				if (length_offset < 0) {
    					/* Exceeds length of file */
    					ast_debug(3, "Exceeds length of file? length=%" PRId64 ", count=%" PRId64 ", flength=%" PRId64 "\n", length, current_length, flength);
    					length_offset = flength;
    				}
    			}
    
    			/* Have offset_offset and length_offset now */
    			if (length_offset - offset_offset == vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
    				/* Simple case - replacement of text inline */
    				fseeko(ff, offset_offset, SEEK_SET);
    				if (fwrite(value, 1, vlength, ff) < vlength) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    				} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    				}
    				fclose(ff);
    			} else if (length_offset - offset_offset > vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)))) {
    				/* More complex case - need to shorten file */
    				off_t cur;
    				int64_t length_length = length_offset - offset_offset;
    				size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
    
    				ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 " (%" PRId64 "), vlength=%" PRId64 ", flength=%" PRId64 "\n",
    					args.offset, offset_offset, args.length, length_offset, length_length, vlength, flength);
    
    				fseeko(ff, offset_offset, SEEK_SET);
    				if (fwrite(value, 1, vlength, ff) < vlength) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    					fclose(ff);
    					return -1;
    				} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, vlen - vlength, ff) < vlen - vlength) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    					fclose(ff);
    					return -1;
    				}
    				while ((cur = ftello(ff)) < flength) {
    
    					if (cur < 0) {
    						ast_log(AST_LOG_ERROR, "Unable to determine last write position for '%s': %s\n", args.filename, strerror(errno));
    						fclose(ff);
    						return -1;
    					}
    
    					fseeko(ff, length_length - vlen, SEEK_CUR);
    					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
    						ast_log(LOG_ERROR, "Short read?!!\n");
    						fclose(ff);
    						return -1;
    					}
    					/* Seek to where we last stopped writing */
    					fseeko(ff, cur, SEEK_SET);
    					if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
    						ast_log(LOG_ERROR, "Short write?!!\n");
    						fclose(ff);
    						return -1;
    					}
    				}
    				fclose(ff);
    				if (truncate(args.filename, flength - (length_length - vlen))) {
    					ast_log(LOG_ERROR, "Truncation of file failed: %s\n", strerror(errno));
    				}
    			} else {
    				/* Most complex case - need to lengthen file */
    				size_t vlen = vlength + (strchr(args.options, 'd') ? 0 : strlen(format2term(newline_format)));
    				int64_t origlen = length_offset - offset_offset;
    				off_t lastwritten = flength + vlen - origlen;
    
    				ast_debug(3, "offset=%s/%" PRId64 ", length=%s/%" PRId64 ", vlength=%" PRId64 ", flength=%" PRId64 "\n",
    					args.offset, offset_offset, args.length, length_offset, vlength, flength);
    
    				fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
    				while (offset_offset + sizeof(fbuf) < ftello(ff)) {
    					if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
    						ast_log(LOG_ERROR, "Short read?!!\n");
    						fclose(ff);
    						return -1;
    					}
    					fseeko(ff, sizeof(fbuf) - vlen - origlen, SEEK_CUR);
    					if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
    						ast_log(LOG_ERROR, "Short write?!!\n");
    						fclose(ff);
    						return -1;
    					}
    					if ((lastwritten = ftello(ff) - sizeof(fbuf)) < offset_offset + sizeof(fbuf)) {
    						break;
    					}
    					fseeko(ff, 2 * sizeof(fbuf) + vlen - origlen, SEEK_CUR);
    				}
    				fseek(ff, length_offset, SEEK_SET);
    				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
    					ast_log(LOG_ERROR, "Short read?!!\n");
    					fclose(ff);
    					return -1;
    				}
    				fseek(ff, offset_offset, SEEK_SET);
    				if (fwrite(value, 1, vlength, ff) < vlength) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    					fclose(ff);
    					return -1;
    				} else if (!strchr(args.options, 'd') && fwrite(format2term(newline_format), 1, strlen(format2term(newline_format)), ff) < strlen(format2term(newline_format))) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    					fclose(ff);
    					return -1;
    
    				} else {
    					off_t curpos = ftello(ff);
    					foplen = lastwritten - curpos;
    					if (fwrite(fbuf, 1, foplen, ff) < foplen) {
    						ast_log(LOG_ERROR, "Short write?!!\n");
    					}
    
    static struct ast_custom_function env_function = {
    
    	.write = env_write
    
    static struct ast_custom_function stat_function = {
    
    	.name = "STAT",
    
    	.read = stat_read,
    	.read_max = 12,
    
    static struct ast_custom_function file_function = {
    	.name = "FILE",
    
    	.read2 = file_read,
    	.write = file_write,
    };
    
    static struct ast_custom_function file_count_line_function = {
    	.name = "FILE_COUNT_LINE",
    	.read2 = file_count_line,
    	.read_max = 12,
    };
    
    static struct ast_custom_function file_format_function = {
    	.name = "FILE_FORMAT",
    	.read2 = file_format,
    	.read_max = 2,
    
    static int unload_module(void)
    
    {
    	int res = 0;
    
    	res |= ast_custom_function_unregister(&env_function);
    	res |= ast_custom_function_unregister(&stat_function);
    
    	res |= ast_custom_function_unregister(&file_function);
    
    	res |= ast_custom_function_unregister(&file_count_line_function);
    	res |= ast_custom_function_unregister(&file_format_function);
    
    static int load_module(void)
    
    {
    	int res = 0;
    
    	res |= ast_custom_function_register(&env_function);
    	res |= ast_custom_function_register(&stat_function);
    
    	res |= ast_custom_function_register(&file_function);
    
    	res |= ast_custom_function_register(&file_count_line_function);
    	res |= ast_custom_function_register(&file_format_function);
    
    AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");