Skip to content
Snippets Groups Projects
func_env.c 44.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 				fseeko(ff, cur + vlength - length, SEEK_SET);
    				if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    				}
    
    				/* Seek to where we stopped reading */
    				if (fseeko(ff, cur + sizeof(fbuf), SEEK_SET) < 0) {
    					/* Only reason for seek to fail is EOF */
    					break;
    				}
    
    			}
    			fclose(ff);
    			if (truncate(args.filename, flength - (length - vlength))) {
    				ast_log(LOG_ERROR, "Unable to truncate the file: %s\n", strerror(errno));
    
    			/* Most complex -- need to open a gap */
    			char fbuf[4096];
    			off_t lastwritten = flength + vlength - length;
    
    			/* Start reading exactly the buffer size back from the end. */
    			fseeko(ff, flength - sizeof(fbuf), SEEK_SET);
    			while (offset < ftello(ff)) {
    				if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
    					ast_log(LOG_ERROR, "Short read?!!\n");
    					fclose(ff);
    					return -1;
    				}
    				/* Since the read moved our file ptr forward, we reverse, but
    				 * seek an offset equal to the amount we want to extend the
    				 * file by */
    				fseeko(ff, vlength - length - sizeof(fbuf), SEEK_CUR);
    
    				/* Note the location of this buffer -- we must not overwrite this position. */
    				lastwritten = ftello(ff);
    
    				if (fwrite(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf)) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    					fclose(ff);
    					return -1;
    				}
    
    				if (lastwritten < offset + sizeof(fbuf)) {
    					break;
    				}
    				/* Our file pointer is now either pointing to the end of the
    				 * file (new position) or a multiple of the fbuf size back from
    				 * that point.  Move back to where we want to start reading
    				 * again.  We never actually try to read beyond the end of the
    				 * file, so we don't have do deal with short reads, as we would
    				 * when we're shortening the file. */
    				fseeko(ff, 2 * sizeof(fbuf) + vlength - length, SEEK_CUR);
    			}
    
    			/* Last part of the file that we need to preserve */
    			if (fseeko(ff, offset + length, SEEK_SET)) {
    				ast_log(LOG_WARNING, "Unable to seek to %" PRId64 " + %" PRId64 " != %" PRId64 "?)\n", offset, length, ftello(ff));
    			}
    
    			/* Doesn't matter how much we read -- just need to restrict the write */
    			ast_debug(1, "Reading at %" PRId64 "\n", ftello(ff));
    			if (fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf) && !feof(ff)) {
    				ast_log(LOG_ERROR, "Short read?!!\n");
    			}
    			fseek(ff, offset, SEEK_SET);
    			/* Write out the value, then write just up until where we last moved some data */
    			if (fwrite(value, 1, vlength, ff) < vlength) {
    				ast_log(LOG_ERROR, "Short write?!!\n");
    
    			} else {
    				off_t curpos = ftello(ff);
    				foplen = lastwritten - curpos;
    				if (fwrite(fbuf, 1, foplen, ff) < foplen) {
    					ast_log(LOG_ERROR, "Short write?!!\n");
    				}
    
    	} else {
    		enum file_format newline_format = FF_UNKNOWN;
    
    		/* Line mode */
    		if (args.argc == 5) {
    			if (tolower(args.format[0]) == 'u') {
    				newline_format = FF_UNIX;
    			} else if (tolower(args.format[0]) == 'm') {
    				newline_format = FF_MAC;
    			} else if (tolower(args.format[0]) == 'd') {
    				newline_format = FF_DOS;
    			}
    		}
    		if (newline_format == FF_UNKNOWN && (newline_format = file2format(args.filename)) == FF_UNKNOWN) {
    			ast_log(LOG_ERROR, "File '%s' not in line format\n", args.filename);
    			return -1;
    		}
    
    		if (strchr(args.options, 'a')) {
    			/* Append to file */
    			if (!(ff = fopen(args.filename, "a"))) {
    				ast_log(LOG_ERROR, "Unable to open '%s' for appending: %s\n", args.filename, strerror(errno));
    				return -1;
    			}
    			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 (offset == 0 && length == LLONG_MAX) {
    			/* Overwrite file */
    			off_t truncsize;
    			if (!(ff = fopen(args.filename, "w"))) {
    				ast_log(LOG_ERROR, "Unable to open '%s' for writing: %s\n", args.filename, strerror(errno));
    				return -1;
    			}
    			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");
    			}
    
    			if ((truncsize = ftello(ff)) < 0) {
    				ast_log(AST_LOG_ERROR, "Unable to determine truncate position of '%s': %s\n", args.filename, strerror(errno));
    			}
    
    			if (truncsize >= 0 && truncate(args.filename, truncsize)) {
    				ast_log(LOG_ERROR, "Unable to truncate file '%s': %s\n", args.filename, strerror(errno));
    				return -1;
    
    			}
    		} else {
    			int64_t offset_offset = (offset == 0 ? 0 : -1), length_offset = -1, flength, i, current_length = 0;
    			char dos_state = 0, fbuf[4096];
    
    			if (offset < 0 && length < offset) {
    				/* Nonsense! */
    				ast_log(LOG_ERROR, "Length cannot specify a position prior to the offset\n");
    				return -1;
    			}
    
    			if (!(ff = fopen(args.filename, "r+"))) {
    				ast_log(LOG_ERROR, "Cannot open '%s' for modification: %s\n", args.filename, strerror(errno));
    				return -1;
    			}
    
    			if (fseek(ff, 0, SEEK_END)) {
    				ast_log(LOG_ERROR, "Cannot seek to end of file '%s': %s\n", args.filename, strerror(errno));
    				fclose(ff);
    				return -1;
    			}
    
    			if ((flength = ftello(ff)) < 0) {
    				ast_log(AST_LOG_ERROR, "Cannot determine end position of file '%s': %s\n", args.filename, strerror(errno));
    				fclose(ff);
    				return -1;
    			}
    
    
    			/* For negative offset and/or negative length */
    			if (offset < 0 || length < 0) {
    				int64_t count = 0;
    				for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
    					char *pos;
    					if (fseeko(ff, i, SEEK_SET)) {
    						ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
    					}
    					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: %s\n", strerror(errno));
    						fclose(ff);
    						return -1;
    					}
    
    					for (pos = fbuf + sizeof(fbuf) - 1; pos >= fbuf; 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 struct ast_custom_function file_dirname_function = {
    	.name = "DIRNAME",
    	.read = file_dirname,
    	.read_max = 12,
    };
    
    static struct ast_custom_function file_basename_function = {
    	.name = "BASENAME",
    	.read = file_basename,
    	.read_max = 12,
    };
    
    
    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);
    
    	res |= ast_custom_function_unregister(&file_dirname_function);
    	res |= ast_custom_function_unregister(&file_basename_function);
    
    static int load_module(void)
    
    {
    	int res = 0;
    
    	res |= ast_custom_function_register(&env_function);
    
    	res |= ast_custom_function_register_escalating(&stat_function, AST_CFE_READ);
    	res |= ast_custom_function_register_escalating(&file_function, AST_CFE_BOTH);
    	res |= ast_custom_function_register_escalating(&file_count_line_function, AST_CFE_READ);
    	res |= ast_custom_function_register_escalating(&file_format_function, AST_CFE_READ);
    
    	res |= ast_custom_function_register(&file_dirname_function);
    	res |= ast_custom_function_register(&file_basename_function);
    
    AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Environment/filesystem dialplan functions");