]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/PyUtility/PyUtility.c
Check In tool source code based on Build tool project revision r1655.
[mirror_edk2.git] / BaseTools / Source / C / PyUtility / PyUtility.c
1 #include <Python.h>
2 #include <Windows.h>
3 #include <Common/UefiBaseTypes.h>
4
5 /*
6 SaveFileToDisk(FilePath, Content)
7 */
8 STATIC
9 PyObject*
10 SaveFileToDisk (
11 PyObject *Self,
12 PyObject *Args
13 )
14 {
15 CHAR8 *File;
16 UINT8 *Data;
17 UINTN DataLength;
18 UINTN WriteBytes;
19 UINTN Status;
20 HANDLE FileHandle;
21 PyObject *ReturnValue = Py_False;
22
23 Status = PyArg_ParseTuple(
24 Args,
25 "ss#",
26 &File,
27 &Data,
28 &DataLength
29 );
30 if (Status == 0) {
31 return NULL;
32 }
33
34 FileHandle = CreateFile(
35 File,
36 GENERIC_WRITE,
37 FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
38 NULL,
39 CREATE_ALWAYS,
40 FILE_ATTRIBUTE_NORMAL,
41 NULL
42 );
43 if (FileHandle == INVALID_HANDLE_VALUE) {
44 PyErr_SetString(PyExc_Exception, "File creation failure");
45 return NULL;
46 }
47
48 while (WriteFile(FileHandle, Data, DataLength, &WriteBytes, NULL)) {
49 if (DataLength <= WriteBytes) {
50 DataLength = 0;
51 break;
52 }
53
54 Data += WriteBytes;
55 DataLength -= WriteBytes;
56 }
57
58 if (DataLength != 0) {
59 // file saved unsuccessfully
60 PyErr_SetString(PyExc_Exception, "File write failure");
61 goto Done;
62 }
63
64 //
65 // Flush buffer may slow down the whole build performance (average 10s slower)
66 //
67 //if (!FlushFileBuffers(FileHandle)) {
68 // PyErr_SetString(PyExc_Exception, "File flush failure");
69 // goto Done;
70 //}
71
72 // success!
73 ReturnValue = Py_True;
74
75 Done:
76 CloseHandle(FileHandle);
77 return ReturnValue;
78 }
79
80 STATIC INT8 SaveFileToDiskDocs[] = "SaveFileToDisk(): Make sure the file is saved to disk\n";
81
82 STATIC PyMethodDef PyUtility_Funcs[] = {
83 {"SaveFileToDisk", (PyCFunction)SaveFileToDisk, METH_VARARGS, SaveFileToDiskDocs},
84 {NULL, NULL, 0, NULL}
85 };
86
87 PyMODINIT_FUNC
88 initPyUtility(VOID) {
89 Py_InitModule3("PyUtility", PyUtility_Funcs, "Utilties Module Implemented C Language");
90 }
91
92