-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
177 lines (142 loc) · 4.7 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include "file.h"
#include "dir.h"
#include "debug.h"
#include "internal.h"
#include "inode.h"
int DOKAN_CALLBACK MxfsCreateFile(
LPCWSTR FileName,
DWORD DesiredAccess,
DWORD ShareMode,
DWORD CreationDisposition,
DWORD FlagsAndAttributes,
PDOKAN_FILE_INFO FileInfo)
{
TRACE("FileName %ls DesiredAccess %lx CreationDisposition %ld FileInfo %p\n", FileName, DesiredAccess, CreationDisposition, FileInfo);
char Path[MAX_PATH];
MINIX_FS *FileSys = (MINIX_FS*)(ULONG_PTR)FileInfo->DokanOptions->GlobalContext;
wcstombs(Path, FileName, sizeof(Path));
int FileIdx = MxfsParsePath(FileSys, Path, FALSE);
if(FileIdx < 0)
{
INFO("Failed to create file %s\n", Path);
return -ERROR_FILE_NOT_FOUND;
}
minix_inode Info;
int Result = MxfsReadInode(FileSys, FileIdx, &Info);
if(Result < 0)
return Result;
if(CreationDisposition == CREATE_NEW)
{
INFO("Already exists %s\n", Path);
return -ERROR_ALREADY_EXISTS;
}
FILE_CTX *FileCtx = MxfsAlloc(sizeof(FILE_CTX));
if(!FileCtx)
return -ERROR_NOT_ENOUGH_MEMORY;
FileCtx->Index = FileIdx;
FileCtx->Write = (DesiredAccess & (GENERIC_WRITE|GENERIC_ALL|FILE_WRITE_DATA)) ? TRUE : FALSE;
FileCtx->Changed = FALSE;
#ifndef NDEBUG
wcscpy_s(FileCtx->DbgBuf, sizeof(FileCtx->DbgBuf), FileName);
#endif // NDEBUG
if(FileInfo->Context)
WARN("MxfsCreateFile: Context %I64u\n", FileInfo->Context);
FileInfo->Context = (LONG64)(ULONG_PTR)FileCtx;
FileInfo->IsDirectory = (Info.d2_mode & I_DIRECTORY) ? TRUE : FALSE;
FileCtx->Length = Info.d2_size;
if(CreationDisposition == TRUNCATE_EXISTING || CreationDisposition == CREATE_ALWAYS)
FileCtx->Length = 0;
if(FileCtx->Write)
INFO("CreationDisposition %ld\n", CreationDisposition);
return 0;
}
int DOKAN_CALLBACK MxfsOpenDirectory(
LPCWSTR FileName,
PDOKAN_FILE_INFO FileInfo)
{
TRACE("%ls %p", FileName, FileInfo);
char Path[MAX_PATH];
MINIX_FS *FileSys = (MINIX_FS*)(ULONG_PTR)FileInfo->DokanOptions->GlobalContext;
wcstombs(Path, FileName, sizeof(Path));
int FileIdx = MxfsParsePath(FileSys, Path, FALSE);
if(FileIdx < 0)
{
INFO("Failed to open dir %s\n", Path);
return -ERROR_FILE_NOT_FOUND;
}
minix_inode Info;
int Result = MxfsReadInode(FileSys, FileIdx, &Info);
if(Result < 0)
return Result;
FILE_CTX *FileCtx = MxfsAlloc(sizeof(FILE_CTX));
if(!FileCtx)
return -ERROR_NOT_ENOUGH_MEMORY;
FileCtx->Index = FileIdx;
FileCtx->Length = Info.d2_size;
FileCtx->Changed = FALSE;
#ifndef NDEBUG
wcscpy_s(FileCtx->DbgBuf, sizeof(FileCtx->DbgBuf), FileName);
#endif // NDEBUG
if(!(Info.d2_mode & I_DIRECTORY))
{
MxfsFree(FileCtx);
INFO("Failed to open dir %s\n", Path);
return -ERROR_INVALID_PARAMETER;
}
if(FileInfo->Context)
WARN("MxfsOpenDirectory: Context %I64u\n", FileInfo->Context);
FileInfo->IsDirectory = TRUE;
FileInfo->Context = (ULONG64)(ULONG_PTR)FileCtx;
return 0;
}
int DOKAN_CALLBACK MxfsCreateDirectory(
LPCWSTR FileName,
PDOKAN_FILE_INFO FileInfo)
{
TRACE("%ls %p", FileName, FileInfo);
char Path[MAX_PATH];
MINIX_FS *FileSys = (MINIX_FS*)(ULONG_PTR)FileInfo->DokanOptions->GlobalContext;
wcstombs(Path, FileName, sizeof(Path));
int FileIdx = MxfsParsePath(FileSys, Path, FALSE);
if(FileIdx > 0)
return -ERROR_ALREADY_EXISTS;
UNIMPLEMENTED("%ls", FileName);
return -ERROR_CALL_NOT_IMPLEMENTED;
}
// When FileInfo->DeleteOnClose is true, you must delete the file in Cleanup.
int DOKAN_CALLBACK MxfsCleanup(
LPCWSTR FileName,
PDOKAN_FILE_INFO FileInfo)
{
TRACE("%ls %p", FileName, FileInfo);
MINIX_FS *FileSys = (MINIX_FS*)(ULONG_PTR)FileInfo->DokanOptions->GlobalContext;
FILE_CTX *FileCtx = (FILE_CTX*)(ULONG_PTR)FileInfo->Context;
ASSERT(FileCtx);
minix_inode Info;
if(MxfsReadInode(FileSys, FileCtx->Index, &Info) >= 0)
{
Info.d2_atime = time(NULL);
if(FileCtx->Changed)
{
Info.d2_mtime = time(NULL);
Info.d2_ctime = time(NULL);
}
MxfsWriteInode(FileSys, FileCtx->Index, &Info);
}
if(FileCtx->Changed)
MxfsSetEndOfFile(FileName, FileCtx->Length, FileInfo);
MxfsFree(FileCtx);
FileInfo->Context = 0;
return 0;
}
int DOKAN_CALLBACK MxfsCloseFile(
LPCWSTR FileName,
PDOKAN_FILE_INFO FileInfo)
{
TRACE("%ls %p", FileName, FileInfo);
ASSERT(FileInfo->Context == 0);
return 0;
}