CcPurgeCacheSection(): Permit NULL 'SharedCacheMap'.
[captive.git] / src / libcaptive / cc / cache.c
1 /* $Id$
2  * reactos Cache Manager (Cc*) cache handling of libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "privatebcbpin.h"
23 #include "reactos/ddk/status.h"
24
25
26 /**
27  * CcPurgeCacheSection:
28  * @SectionObjectPointer: Pointer specifying file to purge;
29  * %NULL value is forbidden.
30  * libcaptive interprets only #SharedCacheMap field as #PUBLIC_BCB pointer.
31  * Field #SharedCacheMap value %NULL is permitted; libcaptive does a NOP with %TRUE return code in such case.
32  * @FileOffset: Starting offset of the ranger to purge.
33  * %NULL pointer is permitted and it means to purge the whole whole.
34  * FIXME: Non %NULL pointer is NOT IMPLEMENTED YET by libcaptive.
35  * @Length: Length of the range to purge. Ignored if @FileOffset==NULL.
36  * @UninitializeCacheMaps: Purge also private cache maps (FIXME: ???).
37  *
38  * Drop any caching for shrunken file which is not being deleted.
39  * libcaptive will no longer consider such #BCB as dirty.
40  *
41  * Undocumented: It is required during %FSCTL_LOCK_VOLUME by ntfs.sys of NT-5.1sp1
42  * to return %TRUE value if #SharedCacheMap value is %NULL.
43  *
44  * Returns: %TRUE if the range was purged successfuly.
45  */
46 BOOLEAN CcPurgeCacheSection(IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
47                 IN PLARGE_INTEGER FileOffset OPTIONAL,IN ULONG Length,IN BOOLEAN UninitializeCacheMaps)
48 {
49 CaptiveSharedCacheMapObject *SharedCacheMap;
50 BOOLEAN r;
51
52         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcPurgeCacheSection: SectionObjectPointer=0x%lX,->SharedCacheMap=0x%lX,FileOffset=0x%lX,Length=0x%lX,"
53                                         "UninitializeCacheMaps=%d",
54                         (long)SectionObjectPointer,
55                         (!SectionObjectPointer ? -1 : (long)SectionObjectPointer->SharedCacheMap),
56                         (!FileOffset ? -1 : (long)FileOffset->QuadPart),Length,
57                                         UninitializeCacheMaps);
58
59         g_return_val_if_fail(SectionObjectPointer!=NULL,FALSE);
60         g_return_val_if_fail(FileOffset==NULL,FALSE);   /* NOT IMPLEMENTED YET */
61         g_return_val_if_fail(UninitializeCacheMaps==0,FALSE);   /* NOT IMPLEMENTED YET */
62
63         if (SectionObjectPointer->SharedCacheMap) {
64                 SharedCacheMap=captive_SectionObjectPointers_to_SharedCacheMap(SectionObjectPointer);
65                 captive_shared_cache_map_purge(SharedCacheMap);
66                 }
67
68         r=TRUE;
69
70         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcPurgeCacheSection: r=%d",r);
71
72         return r;
73 }
74
75
76 /**
77  * CcFlushCache:
78  * @SectionObjectPointer: Pointer specifying file to flush;
79  * %NULL value is forbidden.
80  * libcaptive interprets only #SharedCacheMap field as #PUBLIC_BCB pointer.
81  * Field #SharedCacheMap value %NULL is permitted; libcaptive does a NOP in such case.
82  * @FileOffset: Optional starting point of the range to flush.
83  * %NULL value is permitted.
84  * @Length: Length of the range to flush. Ignored if @FileOffset is %NULL.
85  * @IoStatus: Optionally returns the resulting operation status.
86  * #Information field will contain the number of bytes flushed.
87  * %NULL value is permitted.
88  *
89  * Flushes out any pending dirty data in cache manager BCB mapping.
90  * FIXME: libcaptive currently always flushes the full file ignoring any @FileOffset or @Length.
91  *
92  * VERIFIED: Ranged flushes.
93  * VERIFIED: Synchronous write.
94  */
95 VOID CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
96                 IN PLARGE_INTEGER FileOffset OPTIONAL,IN ULONG Length,OUT PIO_STATUS_BLOCK IoStatus OPTIONAL)
97 {
98 CaptiveSharedCacheMapObject *SharedCacheMap;
99 guint64 flushed;
100
101         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcFlushCache: SectionObjectPointer=0x%lX,->SharedCacheMap=0x%lX,FileOffset=0x%lX,Length=0x%lX",
102                         (long)SectionObjectPointer,
103                         (!SectionObjectPointer ? -1 : (long)SectionObjectPointer->SharedCacheMap),
104                         (!FileOffset ? -1 : (long)FileOffset->QuadPart),Length);
105
106         g_return_if_fail(SectionObjectPointer!=NULL);
107
108         if (SectionObjectPointer->SharedCacheMap) {
109                 SharedCacheMap=captive_SectionObjectPointers_to_SharedCacheMap(SectionObjectPointer);
110
111                 if (FileOffset)
112                         flushed=captive_shared_cache_map_flush(SharedCacheMap,FileOffset->QuadPart,FileOffset->QuadPart+Length);
113                 else
114                         flushed=captive_shared_cache_map_flush(SharedCacheMap,0,G_MAXUINT64-1); /* '-1' for overflow safety */
115                 }
116         else
117                 flushed=0;
118
119         if (IoStatus) {
120                 IoStatus->Status=STATUS_SUCCESS;
121                 IoStatus->Information=flushed;
122                 }
123
124         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcFlushCache: IoStatus->Status=0x%lX,IoStatus->Information=0x%lX",
125                         (!IoStatus ? -1 : (long)IoStatus->Status),(!IoStatus ? -1 : (long)IoStatus->Information));
126 }
127
128
129 BOOLEAN CcIsThereDirtyData(IN PVPB Vpb)
130 {
131 BOOLEAN r;
132
133         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"enter: CcIsThereDirtyData: Vpb=0x%lX",(long)Vpb);
134
135         g_return_val_if_fail(Vpb!=NULL,FALSE);  /* We have just one volume mounted anyway. */
136
137         r=captive_shared_cache_map_is_any_dirty();
138
139         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"leave: CcIsThereDirtyData: r=%d",r);
140
141         return r;
142 }