xref: /unit/src/nxt_job_file.h (revision 65:10688b89aa16)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_JOB_FILE_H_INCLUDED_
8 #define _NXT_JOB_FILE_H_INCLUDED_
9 
10 
11 /*
12  * nxt_job_file_read() allows to open a file, to get its type, size, and
13  * modification time, to read or map file content to memory, and to close
14  * the file.  It can be done as one operation for small file or as several
15  * operations for large file.  On each operation completion ready_handler
16  * or error_handler completion handlers are called.  Since they are job
17  * operations, they can be run by a thread pool.
18  *
19  * If a file is not opened then it is opened and its type, size, and
20  * modification time are got.  Then file content starting from given offset
21  * is read or mapped in memory if there is a buffer supplied.  The offset
22  * field is correspondingly updated.
23  *
24  * If there is no buffer but the read_ahead flag is set then the first
25  * byte is read to initiate read ahead operation.
26  *
27  * If the close flag is set then file descriptor is closed when the file
28  * is completely read.
29  *
30  * The complete flag is set by nxt_job_file_read() when the file is
31  * completely read.
32  *
33  * The test_before_open flag allows to save syscalls in some case, for
34  * example, not to open and then not to close a directory.  It calls
35  * nxt_file_info() to get file type, size, and modification time before
36  * opening the file.  A custom read_required() callback combined with this
37  * flag can also omit opening and reading on some conditions.  However,
38  * if the callback forces opening then additional nxt_file_info() is
39  * called after opening.  The default read_required() callback always
40  * forces opening and reading.
41  */
42 
43 
44 typedef struct nxt_job_file_s  nxt_job_file_t;
45 
46 struct nxt_job_file_s {
47     nxt_job_t           job;
48 
49     nxt_file_t          file;
50 
51     nxt_off_t           offset;
52     nxt_buf_t           *buffer;
53 
54     nxt_work_handler_t  ready_handler;
55     nxt_work_handler_t  error_handler;
56 
57     nxt_int_t           (*read_required)(nxt_job_file_t *jbf);
58 
59     uint16_t            directory_end;
60 
61     uint16_t            close_before_open:1;
62     uint16_t            test_before_open:1;
63     uint16_t            read_ahead:1;
64     uint16_t            close:1;
65     uint16_t            complete:1;
66 };
67 
68 
69 NXT_EXPORT nxt_job_file_t *nxt_job_file_create(nxt_mp_t *mp);
70 NXT_EXPORT void nxt_job_file_init(nxt_job_file_t *jbf);
71 NXT_EXPORT void nxt_job_file_read(nxt_task_t *task, nxt_job_t *job);
72 
73 
74 #endif /* _NXT_JOB_FILE_H_INCLUDED_ */
75