* magic_process - process input file r Apache API request record * (formerly called "process" in file command, prefix added for clarity) Opens * the file and reads a fixed-size buffer to begin processing the contents. */
| 819 | * the file and reads a fixed-size buffer to begin processing the contents. |
| 820 | */ |
| 821 | static int magic_process(request_rec *r) |
| 822 | { |
| 823 | apr_file_t *fd = NULL; |
| 824 | unsigned char buf[HOWMANY + 1]; /* one extra for terminating '\0' */ |
| 825 | apr_size_t nbytes = 0; /* number of bytes read from a datafile */ |
| 826 | int result; |
| 827 | |
| 828 | /* |
| 829 | * first try judging the file based on its filesystem status |
| 830 | */ |
| 831 | switch ((result = fsmagic(r, r->filename))) { |
| 832 | case DONE: |
| 833 | magic_rsl_putchar(r, '\n'); |
| 834 | return OK; |
| 835 | case OK: |
| 836 | break; |
| 837 | default: |
| 838 | /* fatal error, bail out */ |
| 839 | return result; |
| 840 | } |
| 841 | |
| 842 | if (apr_file_open(&fd, r->filename, APR_READ, APR_OS_DEFAULT, r->pool) != APR_SUCCESS) { |
| 843 | /* We can't open it, but we were able to stat it. */ |
| 844 | ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01512) |
| 845 | MODNAME ": can't read `%s'", r->filename); |
| 846 | /* let some other handler decide what the problem is */ |
| 847 | return DECLINED; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * try looking at the first HOWMANY bytes |
| 852 | */ |
| 853 | nbytes = sizeof(buf) - 1; |
| 854 | if ((result = apr_file_read(fd, (char *) buf, &nbytes)) != APR_SUCCESS) { |
| 855 | ap_log_rerror(APLOG_MARK, APLOG_ERR, result, r, APLOGNO(01513) |
| 856 | MODNAME ": read failed: %s", r->filename); |
| 857 | return HTTP_INTERNAL_SERVER_ERROR; |
| 858 | } |
| 859 | |
| 860 | if (nbytes == 0) { |
| 861 | return DECLINED; |
| 862 | } |
| 863 | else { |
| 864 | buf[nbytes++] = '\0'; /* null-terminate it */ |
| 865 | result = tryit(r, buf, nbytes, 1); |
| 866 | if (result != OK) { |
| 867 | return result; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | (void) apr_file_close(fd); |
| 872 | (void) magic_rsl_putchar(r, '\n'); |
| 873 | |
| 874 | return OK; |
| 875 | } |
| 876 | |
| 877 | |
| 878 | static int tryit(request_rec *r, unsigned char *buf, apr_size_t nb, |
no test coverage detected