1 /*$
2 Copyright (C) 2013-2020 Azel.
3
4 This file is part of AzPainter.
5
6 AzPainter 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 3 of the License, or
9 (at your option) any later version.
10
11 AzPainter 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, see <http://www.gnu.org/licenses/>.
18 $*/
19
20 /*****************************************
21 * <linux> ファイル関連ユーティリティ
22 *****************************************/
23
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include "mDef.h"
30
31 #include "mStr.h"
32 #include "mFileStat.h"
33 #include "mUtilCharCode.h"
34
35
36 /**
37
38 @addtogroup util_file
39 @{
40
41 */
42
43
44 //========================
45 // sub
46 //========================
47
48
49 /** ファイルから情報取得 */
50
_getFileStat(const char * filename,struct stat * dst)51 static mBool _getFileStat(const char *filename,struct stat *dst)
52 {
53 char *name;
54 mBool ret;
55
56 name = mUTF8ToLocal_alloc(filename, -1, NULL);
57 if(!name) return FALSE;
58
59 ret = (stat(name, dst) == 0);
60
61 mFree(name);
62
63 return ret;
64 }
65
66 /** struct stat から mFileStat にセット */
67
_conv_filestat(mFileStat * dst,struct stat * src)68 static void _conv_filestat(mFileStat *dst,struct stat *src)
69 {
70 uint32_t f = 0;
71
72 dst->perm = src->st_mode & 0777;
73 dst->size = src->st_size;
74 dst->timeAccess = src->st_atime;
75 dst->timeModify = src->st_mtime;
76
77 //フラグ
78
79 if(S_ISREG(src->st_mode)) f |= MFILESTAT_F_NORMAL;
80 if(S_ISDIR(src->st_mode)) f |= MFILESTAT_F_DIRECTORY;
81 if(S_ISLNK(src->st_mode)) f |= MFILESTAT_F_SYMLINK;
82
83 dst->flags = f;
84 }
85
86
87 //========================
88 //
89 //========================
90
91
92 /** 指定ファイルが存在するか
93 *
94 * @param bDirectory ディレクトリかどうかも判定 */
95
mIsFileExist(const char * filename,mBool bDirectory)96 mBool mIsFileExist(const char *filename,mBool bDirectory)
97 {
98 struct stat st;
99
100 if(!_getFileStat(filename, &st)) return FALSE;
101
102 if(bDirectory)
103 return ((st.st_mode & S_IFDIR) != 0);
104 else
105 return TRUE;
106 }
107
108 /** 指定ファイルの情報取得 */
109
mGetFileStat(const char * filename,mFileStat * dst)110 mBool mGetFileStat(const char *filename,mFileStat *dst)
111 {
112 struct stat st;
113
114 if(!_getFileStat(filename, &st))
115 return FALSE;
116 else
117 {
118 _conv_filestat(dst, &st);
119 return TRUE;
120 }
121 }
122
123 /** ディレクトリ作成 */
124
mCreateDir(const char * path)125 mBool mCreateDir(const char *path)
126 {
127 char *name;
128 mBool ret;
129
130 name = mUTF8ToLocal_alloc(path, -1, NULL);
131 if(!name) return FALSE;
132
133 ret = (mkdir(name, 0755) == 0);
134
135 mFree(name);
136
137 return ret;
138 }
139
140 /** ホームディレクトリ+指定パスのディレクトリを作成 */
141
mCreateDirHome(const char * pathadd)142 mBool mCreateDirHome(const char *pathadd)
143 {
144 mStr str = MSTR_INIT;
145 mBool ret;
146
147 mStrPathSetHomeDir(&str);
148 mStrPathAdd(&str, pathadd);
149
150 ret = mCreateDir(str.buf);
151
152 mStrFree(&str);
153
154 return ret;
155 }
156
157 /** ファイルを削除 */
158
mDeleteFile(const char * filename)159 mBool mDeleteFile(const char *filename)
160 {
161 char *name;
162 mBool ret;
163
164 name = mUTF8ToLocal_alloc(filename, -1, NULL);
165 if(!name) return FALSE;
166
167 ret = (unlink(name) == 0);
168
169 mFree(name);
170
171 return ret;
172 }
173
174 /** ディレクトリを削除
175 *
176 * 中身が空であること。 */
177
mDeleteDir(const char * path)178 mBool mDeleteDir(const char *path)
179 {
180 char *name;
181 mBool ret;
182
183 name = mUTF8ToLocal_alloc(path, -1, NULL);
184 if(!name) return FALSE;
185
186 ret = (rmdir(name) == 0);
187
188 mFree(name);
189
190 return ret;
191 }
192
193 /* @} */
194