more helper functions added to nxinstall
[public/netxms.git] / src / nxinstall / file.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NXSL-based installer tool collection
4 ** Copyright (C) 2005-2011 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: file.cpp
21 **
22 **/
23
24 #include "nxinstall.h"
25
26
27 /**
28 * Check file access.
29 * Parameters:
30 * 1) file name
31 * 2) desired access
32 */
33 int F_access(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
34 {
35 if (!argv[0]->isString())
36 return NXSL_ERR_NOT_STRING;
37
38 if (!argv[1]->isInteger())
39 return NXSL_ERR_NOT_INTEGER;
40
41 *ppResult = new NXSL_Value((LONG)((_taccess(argv[0]->getValueAsCString(), argv[1]->getValueAsInt32()) == 0) ? 1 : 0));
42 return 0;
43 }
44
45 /**
46 * Copy file
47 * Parameters:
48 * 1) source file name
49 * 2) destination file name
50 */
51 int F_CopyFile(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
52 {
53 if (!argv[0]->isString())
54 return NXSL_ERR_NOT_STRING;
55
56 if (!argv[1]->isString())
57 return NXSL_ERR_NOT_STRING;
58
59 #ifdef _WIN32
60 *ppResult = new NXSL_Value((LONG)CopyFile(argv[0]->getValueAsCString(), argv[1]->getValueAsCString(), FALSE));
61 #else
62 *ppResult = new NXSL_Value; /* TODO: implement file copy on UNIX */
63 #endif
64 return 0;
65 }
66
67 /**
68 * Rename file or directory
69 * Parameters:
70 * 1) old file name
71 * 2) new file name
72 */
73 int F_RenameFile(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
74 {
75 if (!argv[0]->isString())
76 return NXSL_ERR_NOT_STRING;
77
78 if (!argv[1]->isString())
79 return NXSL_ERR_NOT_STRING;
80
81 *ppResult = new NXSL_Value((LONG)_trename(argv[0]->getValueAsCString(), argv[1]->getValueAsCString()));
82 return 0;
83 }
84
85 /**
86 * Delete file.
87 * Parameters:
88 * 1) file name
89 */
90 int F_DeleteFile(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
91 {
92 if (!argv[0]->isString())
93 return NXSL_ERR_NOT_STRING;
94
95 *ppResult = new NXSL_Value((LONG)((_tremove(argv[0]->getValueAsCString()) == 0) ? 1 : 0));
96 return 0;
97 }
98
99 /**
100 * Create directory
101 * Parameters:
102 * 1) directory name
103 */
104 int F_mkdir(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
105 {
106 if (!argv[0]->isString())
107 return NXSL_ERR_NOT_STRING;
108
109 *ppResult = new NXSL_Value((LONG)_tmkdir(argv[0]->getValueAsCString()));
110 return 0;
111 }
112
113 /**
114 * Remove directory
115 * Parameters:
116 * 1) directory name
117 */
118 int F_rmdir(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program)
119 {
120 if (!argv[0]->isString())
121 return NXSL_ERR_NOT_STRING;
122
123 *ppResult = new NXSL_Value((LONG)_trmdir(argv[0]->getValueAsCString()));
124 return 0;
125 }