| 1 |
/* |
| 2 |
* fm-file-ops-job-change-attr.c |
| 3 |
* |
| 4 |
* Copyright 2009 PCMan <[email protected]> |
| 5 |
* Copyright 2013-2014 Andriy Grytsenko (LStranger) <[email protected]> |
| 6 |
* |
| 7 |
* This file is a part of the Libfm library. |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Lesser General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public |
| 20 |
* License along with this library; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <glib/gi18n-lib.h> |
| 29 |
|
| 30 |
#include "fm-file-ops-job-change-attr.h" |
| 31 |
#include "fm-folder.h" |
| 32 |
|
| 33 |
static const char query[] = G_FILE_ATTRIBUTE_STANDARD_TYPE"," |
| 34 |
G_FILE_ATTRIBUTE_STANDARD_NAME"," |
| 35 |
G_FILE_ATTRIBUTE_UNIX_GID"," |
| 36 |
G_FILE_ATTRIBUTE_UNIX_UID"," |
| 37 |
G_FILE_ATTRIBUTE_UNIX_MODE"," |
| 38 |
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME; |
| 39 |
|
| 40 |
static gboolean _fm_file_ops_job_change_attr_file(FmFileOpsJob* job, GFile* gf, |
| 41 |
GFileInfo* inf, FmFolder *folder) |
| 42 |
{ |
| 43 |
GError* err = NULL; |
| 44 |
FmJob* fmjob = FM_JOB(job); |
| 45 |
GCancellable* cancellable = fm_job_get_cancellable(fmjob); |
| 46 |
FmPath *path; |
| 47 |
GFileType type; |
| 48 |
gboolean ret = TRUE; |
| 49 |
gboolean changed = FALSE; |
| 50 |
|
| 51 |
if( !inf) |
| 52 |
{ |
| 53 |
_retry_query_info: |
| 54 |
inf = g_file_query_info(gf, query, |
| 55 |
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 56 |
cancellable, &err); |
| 57 |
if(!inf) |
| 58 |
{ |
| 59 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 60 |
g_error_free(err); |
| 61 |
err = NULL; |
| 62 |
if(act == FM_JOB_RETRY) |
| 63 |
goto _retry_query_info; |
| 64 |
} |
| 65 |
} |
| 66 |
else |
| 67 |
g_object_ref(inf); |
| 68 |
|
| 69 |
type = g_file_info_get_file_type(inf); |
| 70 |
|
| 71 |
/* change owner */ |
| 72 |
if( !fm_job_is_cancelled(fmjob) && job->uid != -1 ) |
| 73 |
{ |
| 74 |
_retry_change_owner: |
| 75 |
if(!g_file_set_attribute_uint32(gf, G_FILE_ATTRIBUTE_UNIX_UID, |
| 76 |
job->uid, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 77 |
cancellable, &err) ) |
| 78 |
{ |
| 79 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 80 |
g_error_free(err); |
| 81 |
err = NULL; |
| 82 |
if(act == FM_JOB_RETRY) |
| 83 |
goto _retry_change_owner; |
| 84 |
} |
| 85 |
else |
| 86 |
changed = TRUE; |
| 87 |
} |
| 88 |
|
| 89 |
/* change group */ |
| 90 |
if( !fm_job_is_cancelled(fmjob) && job->gid != -1 ) |
| 91 |
{ |
| 92 |
_retry_change_group: |
| 93 |
if(!g_file_set_attribute_uint32(gf, G_FILE_ATTRIBUTE_UNIX_GID, |
| 94 |
job->gid, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 95 |
cancellable, &err) ) |
| 96 |
{ |
| 97 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 98 |
g_error_free(err); |
| 99 |
err = NULL; |
| 100 |
if(act == FM_JOB_RETRY) |
| 101 |
goto _retry_change_group; |
| 102 |
} |
| 103 |
else |
| 104 |
changed = TRUE; |
| 105 |
} |
| 106 |
|
| 107 |
/* change mode */ |
| 108 |
if( !fm_job_is_cancelled(fmjob) && job->new_mode_mask ) |
| 109 |
{ |
| 110 |
guint32 mode = g_file_info_get_attribute_uint32(inf, G_FILE_ATTRIBUTE_UNIX_MODE); |
| 111 |
mode &= ~job->new_mode_mask; |
| 112 |
mode |= (job->new_mode & job->new_mode_mask); |
| 113 |
|
| 114 |
/* FIXME: this behavior should be optional. */ |
| 115 |
/* treat dirs with 'r' as 'rx' */ |
| 116 |
if(type == G_FILE_TYPE_DIRECTORY) |
| 117 |
{ |
| 118 |
if((job->new_mode_mask & S_IRUSR) && (mode & S_IRUSR)) |
| 119 |
mode |= S_IXUSR; |
| 120 |
if((job->new_mode_mask & S_IRGRP) && (mode & S_IRGRP)) |
| 121 |
mode |= S_IXGRP; |
| 122 |
if((job->new_mode_mask & S_IROTH) && (mode & S_IROTH)) |
| 123 |
mode |= S_IXOTH; |
| 124 |
} |
| 125 |
|
| 126 |
/* new mode */ |
| 127 |
_retry_chmod: |
| 128 |
if( !g_file_set_attribute_uint32(gf, G_FILE_ATTRIBUTE_UNIX_MODE, |
| 129 |
mode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 130 |
cancellable, &err) ) |
| 131 |
{ |
| 132 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 133 |
g_error_free(err); |
| 134 |
err = NULL; |
| 135 |
if(act == FM_JOB_RETRY) |
| 136 |
goto _retry_chmod; |
| 137 |
} |
| 138 |
else |
| 139 |
changed = TRUE; |
| 140 |
} |
| 141 |
/* change display name, icon, hidden, target */ |
| 142 |
if (!fm_job_is_cancelled(fmjob) && job->display_name) |
| 143 |
{ |
| 144 |
GFile *renamed; |
| 145 |
_retry_disp_name: |
| 146 |
renamed = g_file_set_display_name(gf, job->display_name, cancellable, &err); |
| 147 |
if (renamed == NULL) |
| 148 |
{ |
| 149 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 150 |
g_clear_error(&err); |
| 151 |
if(act == FM_JOB_RETRY) |
| 152 |
goto _retry_disp_name; |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
g_object_unref(renamed); |
| 157 |
changed = TRUE; |
| 158 |
} |
| 159 |
} |
| 160 |
if (!fm_job_is_cancelled(fmjob) && job->icon) |
| 161 |
{ |
| 162 |
_retry_change_icon: |
| 163 |
if (!g_file_set_attribute(gf, G_FILE_ATTRIBUTE_STANDARD_ICON, |
| 164 |
G_FILE_ATTRIBUTE_TYPE_OBJECT, job->icon, |
| 165 |
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 166 |
cancellable, &err)) |
| 167 |
{ |
| 168 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 169 |
g_clear_error(&err); |
| 170 |
if(act == FM_JOB_RETRY) |
| 171 |
goto _retry_change_icon; |
| 172 |
} |
| 173 |
else |
| 174 |
changed = TRUE; |
| 175 |
} |
| 176 |
if (!fm_job_is_cancelled(fmjob) && job->set_hidden >= 0) |
| 177 |
{ |
| 178 |
gboolean hidden = job->set_hidden > 0; |
| 179 |
_retry_change_hidden: |
| 180 |
if (!g_file_set_attribute(gf, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN, |
| 181 |
G_FILE_ATTRIBUTE_TYPE_BOOLEAN, &hidden, |
| 182 |
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 183 |
cancellable, &err)) |
| 184 |
{ |
| 185 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 186 |
g_clear_error(&err); |
| 187 |
if(act == FM_JOB_RETRY) |
| 188 |
goto _retry_change_hidden; |
| 189 |
} |
| 190 |
else |
| 191 |
changed = TRUE; |
| 192 |
} |
| 193 |
if (!fm_job_is_cancelled(fmjob) && job->target) |
| 194 |
{ |
| 195 |
_retry_change_target: |
| 196 |
if (!g_file_set_attribute_string(gf, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI, |
| 197 |
job->target, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 198 |
cancellable, &err)) |
| 199 |
{ |
| 200 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 201 |
g_clear_error(&err); |
| 202 |
if(act == FM_JOB_RETRY) |
| 203 |
goto _retry_change_target; |
| 204 |
} |
| 205 |
else |
| 206 |
changed = TRUE; |
| 207 |
} |
| 208 |
|
| 209 |
/* currently processed file. */ |
| 210 |
if(inf) |
| 211 |
{ |
| 212 |
fm_file_ops_job_emit_cur_file(job, g_file_info_get_display_name(inf)); |
| 213 |
g_object_unref(inf); |
| 214 |
inf = NULL; |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
char* basename = g_file_get_basename(gf); |
| 219 |
char* disp = basename ? g_filename_display_name(basename) : NULL; |
| 220 |
/* FIXME: translate it */ |
| 221 |
fm_file_ops_job_emit_cur_file(job, disp ? disp : "(invalid file)"); |
| 222 |
g_free(disp); |
| 223 |
g_free(basename); |
| 224 |
} |
| 225 |
|
| 226 |
++job->finished; |
| 227 |
fm_file_ops_job_emit_percent(job); |
| 228 |
|
| 229 |
path = fm_path_new_for_gfile(gf); |
| 230 |
if( !fm_job_is_cancelled(fmjob) && job->recursive && type == G_FILE_TYPE_DIRECTORY) |
| 231 |
{ |
| 232 |
GFileEnumerator* enu; |
| 233 |
FmFolder *sub_folder; |
| 234 |
_retry_enum_children: |
| 235 |
enu = g_file_enumerate_children(gf, query, |
| 236 |
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 237 |
cancellable, &err); |
| 238 |
if(!enu) |
| 239 |
{ |
| 240 |
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 241 |
g_error_free(err); |
| 242 |
err = NULL; |
| 243 |
if(act == FM_JOB_RETRY) |
| 244 |
goto _retry_enum_children; |
| 245 |
fm_path_unref(path); |
| 246 |
return FALSE; |
| 247 |
} |
| 248 |
|
| 249 |
sub_folder = fm_folder_find_by_path(path); |
| 250 |
while( ! fm_job_is_cancelled(fmjob) ) |
| 251 |
{ |
| 252 |
inf = g_file_enumerator_next_file(enu, cancellable, &err); |
| 253 |
if(inf) |
| 254 |
{ |
| 255 |
GFile* sub = g_file_get_child(gf, g_file_info_get_name(inf)); |
| 256 |
ret = _fm_file_ops_job_change_attr_file(job, sub, inf, sub_folder); |
| 257 |
g_object_unref(sub); |
| 258 |
g_object_unref(inf); |
| 259 |
if(!ret) /* _fm_file_ops_job_change_attr_file() failed */ |
| 260 |
break; |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
if(err) |
| 265 |
{ |
| 266 |
fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD); |
| 267 |
g_error_free(err); |
| 268 |
err = NULL; |
| 269 |
/* FM_JOB_RETRY is not supported here */ |
| 270 |
} |
| 271 |
else /* EOF */ |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
g_object_unref(enu); |
| 276 |
if (sub_folder) |
| 277 |
g_object_unref(sub_folder); |
| 278 |
if(!folder || !_fm_folder_event_file_changed(folder, path)) |
| 279 |
fm_path_unref(path); |
| 280 |
} |
| 281 |
else if (!changed || !folder || !_fm_folder_event_file_changed(folder, path)) |
| 282 |
fm_path_unref(path); |
| 283 |
return ret; |
| 284 |
} |
| 285 |
|
| 286 |
gboolean _fm_file_ops_job_change_attr_run(FmFileOpsJob* job) |
| 287 |
{ |
| 288 |
GList* l; |
| 289 |
|
| 290 |
/* prepare the job, count total work needed with FmDeepCountJob */ |
| 291 |
if(job->recursive) |
| 292 |
{ |
| 293 |
FmDeepCountJob* dc = fm_deep_count_job_new(job->srcs, FM_DC_JOB_DEFAULT); |
| 294 |
fm_job_run_sync(FM_JOB(dc)); |
| 295 |
job->total = dc->count; |
| 296 |
g_object_unref(dc); |
| 297 |
} |
| 298 |
else |
| 299 |
job->total = fm_path_list_get_length(job->srcs); |
| 300 |
|
| 301 |
g_debug("total number of files to change attribute: %llu", (long long unsigned int)job->total); |
| 302 |
|
| 303 |
fm_file_ops_job_emit_prepared(job); |
| 304 |
|
| 305 |
l = fm_path_list_peek_head_link(job->srcs); |
| 306 |
/* check if we trying to set display name for more than one file and fail */ |
| 307 |
if (!fm_job_is_cancelled(FM_JOB(job)) && l->next && |
| 308 |
(job->display_name || job->target)) |
| 309 |
{ |
| 310 |
GError *error; |
| 311 |
|
| 312 |
if (job->display_name) |
| 313 |
error = g_error_new(G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
| 314 |
_("Setting display name can be done only for single file")); |
| 315 |
else |
| 316 |
error = g_error_new(G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, |
| 317 |
_("Setting target can be done only for single file")); |
| 318 |
fm_job_emit_error(FM_JOB(job), error, FM_JOB_ERROR_CRITICAL); |
| 319 |
g_error_free(error); |
| 320 |
return FALSE; |
| 321 |
} |
| 322 |
for(; ! fm_job_is_cancelled(FM_JOB(job)) && l;l=l->next) |
| 323 |
{ |
| 324 |
gboolean ret; |
| 325 |
GFile* src = fm_path_to_gfile(FM_PATH(l->data)); |
| 326 |
FmFolder *folder = fm_folder_find_by_path(l->data); |
| 327 |
|
| 328 |
ret = _fm_file_ops_job_change_attr_file(job, src, NULL, folder); |
| 329 |
g_object_unref(src); |
| 330 |
if (folder) |
| 331 |
g_object_unref(folder); |
| 332 |
|
| 333 |
if(!ret) /* error! */ |
| 334 |
return FALSE; |
| 335 |
} |
| 336 |
return TRUE; |
| 337 |
} |