rem
stringlengths
0
83.5k
add
stringlengths
0
223k
context
stringlengths
10
471k
meta
stringlengths
120
236
#endif
void scan_man_page(const char *man_page){ if (!man_page) return;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Start scanning man page" << endl;#endif // ## Do more init // Unlike man2html, we actually call this several times, hence the need to // properly cleanup all those static vars s_ifelseval.clear(); s_characterDefinitionMap.clear(); InitCharacterDefinitions(); s_stringDefinitionMap.clear(); InitStringDefinitions(); s_numberDefinitionMap.clear(); InitNumberDefinitions(); s_argumentList.clear(); section = 0; s_dollarZero = ""; // No macro called yet! output_possible = false; int strLength = qstrlen(man_page); char *buf = new char[strLength + 2]; qstrcpy(buf+1, man_page); buf[0] = '\n';#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Parse man page" << endl;#endif scan_troff(buf+1,0,NULL);#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Man page parsed!" << endl;#endif while (itemdepth || dl_set[itemdepth]) { out_html("</DL>\n"); if (dl_set[itemdepth]) dl_set[itemdepth]=0; else if (itemdepth > 0) itemdepth--; } out_html(set_font("R")); out_html(change_to_size(0)); if (!fillout) { fillout=1; out_html("</PRE>"); } out_html(NEWLINE); if (section) { output_real("<div style=\"margin-left: 2cm\">\n"); section = 0; } if (output_possible) { output_real("</div>\n"); output_real("<div class=\"bannerBottom\" style=\"background-image: url("); output_real(cssPath); output_real("/bottom-middle.png); background-repeat: x-repeat; width: 100%; height: 100px; bottom:0pt;\">\n"); output_real("<div class=\"bannerBottomLeft\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-left.png\" style=\"margin: 0pt;\" alt=\"Bottom left of the banner\">\n"); output_real("</div>\n"); output_real("<div class=\"bannerBottomRight\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-right.png\" style=\"margin: 0pt\" alt=\"Bottom right of the banner\">\n"); output_real("</div>\n"); output_real("</div>\n"); output_real("</BODY>\n</HTML>\n"); } delete [] buf; // Release memory s_characterDefinitionMap.clear(); s_stringDefinitionMap.clear(); s_numberDefinitionMap.clear(); s_argumentList.clear(); // reinit static variables for reuse delete [] buffer; buffer = 0; escapesym='\\'; nobreaksym='\''; controlsym='.'; fieldsym=0; padsym=0; buffpos=0; buffmax=0; scaninbuff=false; itemdepth=0; for (int i = 0; i < 20; i++) dl_set[i] = 0; still_dd=false; for (int i = 0; i < 12; i++) tabstops[i] = (i+1)*8; maxtstop=12; curpos=0; mandoc_name_count = 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/buggy/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
void scan_man_page(const char *man_page){ if (!man_page) return;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Start scanning man page" << endl;#endif // ## Do more init // Unlike man2html, we actually call this several times, hence the need to // properly cleanup all those static vars s_ifelseval.clear(); s_characterDefinitionMap.clear(); InitCharacterDefinitions(); s_stringDefinitionMap.clear(); InitStringDefinitions(); s_numberDefinitionMap.clear(); InitNumberDefinitions(); s_argumentList.clear(); section = 0; s_dollarZero = ""; // No macro called yet! output_possible = false; int strLength = qstrlen(man_page); char *buf = new char[strLength + 2]; qstrcpy(buf+1, man_page); buf[0] = '\n';#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Parse man page" << endl;#endif scan_troff(buf+1,0,NULL);#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Man page parsed!" << endl;#endif while (itemdepth || dl_set[itemdepth]) { out_html("</DL>\n"); if (dl_set[itemdepth]) dl_set[itemdepth]=0; else if (itemdepth > 0) itemdepth--; } out_html(set_font("R")); out_html(change_to_size(0)); if (!fillout) { fillout=1; out_html("</PRE>"); } out_html(NEWLINE); if (section) { output_real("<div style=\"margin-left: 2cm\">\n"); section = 0; } if (output_possible) { output_real("</div>\n"); output_real("<div class=\"bannerBottom\" style=\"background-image: url("); output_real(cssPath); output_real("/bottom-middle.png); background-repeat: x-repeat; width: 100%; height: 100px; bottom:0pt;\">\n"); output_real("<div class=\"bannerBottomLeft\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-left.png\" style=\"margin: 0pt;\" alt=\"Bottom left of the banner\">\n"); output_real("</div>\n"); output_real("<div class=\"bannerBottomRight\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-right.png\" style=\"margin: 0pt\" alt=\"Bottom right of the banner\">\n"); output_real("</div>\n"); output_real("</div>\n"); output_real("</BODY>\n</HTML>\n"); } delete [] buf; // Release memory s_characterDefinitionMap.clear(); s_stringDefinitionMap.clear(); s_numberDefinitionMap.clear(); s_argumentList.clear(); // reinit static variables for reuse delete [] buffer; buffer = 0; escapesym='\\'; nobreaksym='\''; controlsym='.'; fieldsym=0; padsym=0; buffpos=0; buffmax=0; scaninbuff=false; itemdepth=0; for (int i = 0; i < 20; i++) dl_set[i] = 0; still_dd=false; for (int i = 0; i < 12; i++) tabstops[i] = (i+1)*8; maxtstop=12; curpos=0; mandoc_name_count = 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/buggy/kioslave/man/man2html.cpp
#endif
void scan_man_page(const char *man_page){ if (!man_page) return;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Start scanning man page" << endl;#endif // ## Do more init // Unlike man2html, we actually call this several times, hence the need to // properly cleanup all those static vars s_ifelseval.clear(); s_characterDefinitionMap.clear(); InitCharacterDefinitions(); s_stringDefinitionMap.clear(); InitStringDefinitions(); s_numberDefinitionMap.clear(); InitNumberDefinitions(); s_argumentList.clear(); section = 0; s_dollarZero = ""; // No macro called yet! output_possible = false; int strLength = qstrlen(man_page); char *buf = new char[strLength + 2]; qstrcpy(buf+1, man_page); buf[0] = '\n';#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Parse man page" << endl;#endif scan_troff(buf+1,0,NULL);#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Man page parsed!" << endl;#endif while (itemdepth || dl_set[itemdepth]) { out_html("</DL>\n"); if (dl_set[itemdepth]) dl_set[itemdepth]=0; else if (itemdepth > 0) itemdepth--; } out_html(set_font("R")); out_html(change_to_size(0)); if (!fillout) { fillout=1; out_html("</PRE>"); } out_html(NEWLINE); if (section) { output_real("<div style=\"margin-left: 2cm\">\n"); section = 0; } if (output_possible) { output_real("</div>\n"); output_real("<div class=\"bannerBottom\" style=\"background-image: url("); output_real(cssPath); output_real("/bottom-middle.png); background-repeat: x-repeat; width: 100%; height: 100px; bottom:0pt;\">\n"); output_real("<div class=\"bannerBottomLeft\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-left.png\" style=\"margin: 0pt;\" alt=\"Bottom left of the banner\">\n"); output_real("</div>\n"); output_real("<div class=\"bannerBottomRight\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-right.png\" style=\"margin: 0pt\" alt=\"Bottom right of the banner\">\n"); output_real("</div>\n"); output_real("</div>\n"); output_real("</BODY>\n</HTML>\n"); } delete [] buf; // Release memory s_characterDefinitionMap.clear(); s_stringDefinitionMap.clear(); s_numberDefinitionMap.clear(); s_argumentList.clear(); // reinit static variables for reuse delete [] buffer; buffer = 0; escapesym='\\'; nobreaksym='\''; controlsym='.'; fieldsym=0; padsym=0; buffpos=0; buffmax=0; scaninbuff=false; itemdepth=0; for (int i = 0; i < 20; i++) dl_set[i] = 0; still_dd=false; for (int i = 0; i < 12; i++) tabstops[i] = (i+1)*8; maxtstop=12; curpos=0; mandoc_name_count = 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/buggy/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
void scan_man_page(const char *man_page){ if (!man_page) return;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Start scanning man page" << endl;#endif // ## Do more init // Unlike man2html, we actually call this several times, hence the need to // properly cleanup all those static vars s_ifelseval.clear(); s_characterDefinitionMap.clear(); InitCharacterDefinitions(); s_stringDefinitionMap.clear(); InitStringDefinitions(); s_numberDefinitionMap.clear(); InitNumberDefinitions(); s_argumentList.clear(); section = 0; s_dollarZero = ""; // No macro called yet! output_possible = false; int strLength = qstrlen(man_page); char *buf = new char[strLength + 2]; qstrcpy(buf+1, man_page); buf[0] = '\n';#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Parse man page" << endl;#endif scan_troff(buf+1,0,NULL);#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Man page parsed!" << endl;#endif while (itemdepth || dl_set[itemdepth]) { out_html("</DL>\n"); if (dl_set[itemdepth]) dl_set[itemdepth]=0; else if (itemdepth > 0) itemdepth--; } out_html(set_font("R")); out_html(change_to_size(0)); if (!fillout) { fillout=1; out_html("</PRE>"); } out_html(NEWLINE); if (section) { output_real("<div style=\"margin-left: 2cm\">\n"); section = 0; } if (output_possible) { output_real("</div>\n"); output_real("<div class=\"bannerBottom\" style=\"background-image: url("); output_real(cssPath); output_real("/bottom-middle.png); background-repeat: x-repeat; width: 100%; height: 100px; bottom:0pt;\">\n"); output_real("<div class=\"bannerBottomLeft\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-left.png\" style=\"margin: 0pt;\" alt=\"Bottom left of the banner\">\n"); output_real("</div>\n"); output_real("<div class=\"bannerBottomRight\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-right.png\" style=\"margin: 0pt\" alt=\"Bottom right of the banner\">\n"); output_real("</div>\n"); output_real("</div>\n"); output_real("</BODY>\n</HTML>\n"); } delete [] buf; // Release memory s_characterDefinitionMap.clear(); s_stringDefinitionMap.clear(); s_numberDefinitionMap.clear(); s_argumentList.clear(); // reinit static variables for reuse delete [] buffer; buffer = 0; escapesym='\\'; nobreaksym='\''; controlsym='.'; fieldsym=0; padsym=0; buffpos=0; buffmax=0; scaninbuff=false; itemdepth=0; for (int i = 0; i < 20; i++) dl_set[i] = 0; still_dd=false; for (int i = 0; i < 12; i++) tabstops[i] = (i+1)*8; maxtstop=12; curpos=0; mandoc_name_count = 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/buggy/kioslave/man/man2html.cpp
#endif
void scan_man_page(const char *man_page){ if (!man_page) return;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Start scanning man page" << endl;#endif // ## Do more init // Unlike man2html, we actually call this several times, hence the need to // properly cleanup all those static vars s_ifelseval.clear(); s_characterDefinitionMap.clear(); InitCharacterDefinitions(); s_stringDefinitionMap.clear(); InitStringDefinitions(); s_numberDefinitionMap.clear(); InitNumberDefinitions(); s_argumentList.clear(); section = 0; s_dollarZero = ""; // No macro called yet! output_possible = false; int strLength = qstrlen(man_page); char *buf = new char[strLength + 2]; qstrcpy(buf+1, man_page); buf[0] = '\n';#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Parse man page" << endl;#endif scan_troff(buf+1,0,NULL);#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Man page parsed!" << endl;#endif while (itemdepth || dl_set[itemdepth]) { out_html("</DL>\n"); if (dl_set[itemdepth]) dl_set[itemdepth]=0; else if (itemdepth > 0) itemdepth--; } out_html(set_font("R")); out_html(change_to_size(0)); if (!fillout) { fillout=1; out_html("</PRE>"); } out_html(NEWLINE); if (section) { output_real("<div style=\"margin-left: 2cm\">\n"); section = 0; } if (output_possible) { output_real("</div>\n"); output_real("<div class=\"bannerBottom\" style=\"background-image: url("); output_real(cssPath); output_real("/bottom-middle.png); background-repeat: x-repeat; width: 100%; height: 100px; bottom:0pt;\">\n"); output_real("<div class=\"bannerBottomLeft\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-left.png\" style=\"margin: 0pt;\" alt=\"Bottom left of the banner\">\n"); output_real("</div>\n"); output_real("<div class=\"bannerBottomRight\">\n"); output_real("<img src=\""); output_real(cssPath); output_real("/bottom-right.png\" style=\"margin: 0pt\" alt=\"Bottom right of the banner\">\n"); output_real("</div>\n"); output_real("</div>\n"); output_real("</BODY>\n</HTML>\n"); } delete [] buf; // Release memory s_characterDefinitionMap.clear(); s_stringDefinitionMap.clear(); s_numberDefinitionMap.clear(); s_argumentList.clear(); // reinit static variables for reuse delete [] buffer; buffer = 0; escapesym='\\'; nobreaksym='\''; controlsym='.'; fieldsym=0; padsym=0; buffpos=0; buffmax=0; scaninbuff=false; itemdepth=0; for (int i = 0; i < 20; i++) dl_set[i] = 0; still_dd=false; for (int i = 0; i < 12; i++) tabstops[i] = (i+1)*8; maxtstop=12; curpos=0; mandoc_name_count = 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/buggy/kioslave/man/man2html.cpp
ThumbCreator *new_creator()
KDE_EXPORT ThumbCreator *new_creator()
ThumbCreator *new_creator() { KImageIO::registerFormats(); return new ImageCreator; }
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/e1e7cf009e0e126565e643210583d75c2758d527/imagecreator.cpp/clean/kioslave/thumbnail/imagecreator.cpp
app_entry(e, KIO::UDS_NAME, n);
app_entry(e, KIO::UDS_NAME, QFile::decodeName(n.local8Bit()));
app_dir(UDSEntry& e, const QString & n, size_t s){ e.clear(); app_entry(e, KIO::UDS_NAME, n); app_entry(e, KIO::UDS_FILE_TYPE, S_IFDIR); app_entry(e, KIO::UDS_ACCESS, 0400); app_entry(e, KIO::UDS_SIZE, s);}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/f3545a0ec89521bff52b55816dfd80c7d0cdab92/audiocd.cpp/clean/kioslave/audiocd/audiocd.cpp
app_entry(e, KIO::UDS_NAME, n);
app_entry(e, KIO::UDS_NAME, QFile::decodeName(n.local8Bit()));
app_file(UDSEntry& e, const QString & n, size_t s){ e.clear(); app_entry(e, KIO::UDS_NAME, n); app_entry(e, KIO::UDS_FILE_TYPE, S_IFREG); app_entry(e, KIO::UDS_ACCESS, 0400); app_entry(e, KIO::UDS_SIZE, s);}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/f3545a0ec89521bff52b55816dfd80c7d0cdab92/audiocd.cpp/clean/kioslave/audiocd/audiocd.cpp
void increase_size(size_t size) {
void increase_size(unsigned long long size) {
void increase_size(size_t size) { clean_size += size; }
2690 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2690/98d20ecf3abc9fcecac9169f9ccc60f8d1d235dd/Buffercache.h/clean/ceph/client/Buffercache.h
void miss_finish() { assert(state == BUFHD_STATE_INFLIGHT); state = BUFHD_STATE_CLEAN; wakeup_read_waiters(); wakeup_write_waiters(); }
2690 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2690/82e27fcf573596b9755772393b60ef67c62420df/Buffercache.h/buggy/ceph/client/Buffercache.h
dout(10) << "former age: " << former_age << " age: " << age << endl;
time_t get_age() { time_t age; if (_dbufs.empty()) { age = 0; } else { age = time(NULL) - _dbufs.begin()->second->dirty_since; } dout(10) << "former age: " << former_age << " age: " << age << endl; //assert((!(former_age > 30)) || (age > 0)); former_age = age; return age; }
2690 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2690/185b4405d4a2a7012104048179392323dfe3bcae/Buffercache.h/buggy/ceph/client/Buffercache.h
former_age = age;
time_t get_age() { time_t age; if (_dbufs.empty()) { age = 0; } else { age = time(NULL) - _dbufs.begin()->second->dirty_since; } dout(10) << "former age: " << former_age << " age: " << age << endl; //assert((!(former_age > 30)) || (age > 0)); former_age = age; return age; }
2690 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2690/185b4405d4a2a7012104048179392323dfe3bcae/Buffercache.h/buggy/ceph/client/Buffercache.h
DEBUG_CHECK_NULL(env, arg0)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__I_3II (JNIEnv *env, jclass that, jint arg0, jintArray arg1, jint arg2){ jint *lparg1=NULL; DEBUG_CALL("MoveMemory\n") if (arg1) lparg1 = (*env)->GetIntArrayElements(env, arg1, NULL); MoveMemory((PVOID)arg0, (CONST VOID *)lparg1, arg2); if (arg1) (*env)->ReleaseIntArrayElements(env, arg1, lparg1, 0);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg0)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__I_3CI (JNIEnv *env, jclass that, jint arg0, jcharArray arg1, jint arg2){ jchar *lparg1=NULL; DEBUG_CALL("MoveMemory\n") if (arg1) lparg1 = (*env)->GetCharArrayElements(env, arg1, NULL); MoveMemory((PVOID)arg0, (CONST VOID *)lparg1, arg2); if (arg1) (*env)->ReleaseCharArrayElements(env, arg1, lparg1, 0);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory___3III (JNIEnv *env, jclass that, jintArray arg0, jint arg1, jint arg2){ jint *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = (*env)->GetIntArrayElements(env, arg0, NULL); MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) (*env)->ReleaseIntArrayElements(env, arg0, lparg0, 0);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory___3CII (JNIEnv *env, jclass that, jcharArray arg0, jint arg1, jint arg2){ jchar *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = (*env)->GetCharArrayElements(env, arg0, NULL); MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) (*env)->ReleaseCharArrayElements(env, arg0, lparg0, 0);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__Lorg_eclipse_swt_internal_win32_MEASUREITEMSTRUCT_2II (JNIEnv *env, jclass that, jobject arg0, jint arg1, jint arg2){ DECL_GLOB(pGlob) MEASUREITEMSTRUCT _arg0, *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = &_arg0; MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) setMEASUREITEMSTRUCTFields(env, arg0, lparg0, &PGLOB(MEASUREITEMSTRUCTFc));}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__Lorg_eclipse_swt_internal_win32_TVITEM_2II (JNIEnv *env, jclass that, jobject arg0, jint arg1, jint arg2){ DECL_GLOB(pGlob) TVITEM _arg0, *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = &_arg0; MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) setTVITEMFields(env, arg0, lparg0, &PGLOB(TVITEMFc));}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__Lorg_eclipse_swt_internal_win32_NMHEADER_2II (JNIEnv *env, jclass that, jobject arg0, jint arg1, jint arg2){ DECL_GLOB(pGlob) NMHEADER _arg0, *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = &_arg0; MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) setNMHEADERFields(env, arg0, lparg0, &PGLOB(NMHEADERFc));}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__Lorg_eclipse_swt_internal_win32_NMHDR_2II (JNIEnv *env, jclass that, jobject arg0, jint arg1, jint arg2){ DECL_GLOB(pGlob) NMHDR _arg0, *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = &_arg0; MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) setNMHDRFields(env, arg0, lparg0, &PGLOB(NMHDRFc));}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__Lorg_eclipse_swt_internal_win32_HDITEM_2II (JNIEnv *env, jclass that, jobject arg0, jint arg1, jint arg2){ DECL_GLOB(pGlob) HDITEM _arg0, *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = &_arg0; MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) setHDITEMFields(env, arg0, lparg0, &PGLOB(HDITEMFc));}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__Lorg_eclipse_swt_internal_win32_DRAWITEMSTRUCT_2II (JNIEnv *env, jclass that, jobject arg0, jint arg1, jint arg2){ DECL_GLOB(pGlob) DRAWITEMSTRUCT _arg0, *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = &_arg0; MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) setDRAWITEMSTRUCTFields(env, arg0, lparg0, &PGLOB(DRAWITEMSTRUCTFc));}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg0)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__ILorg_eclipse_swt_internal_win32_RECT_2I (JNIEnv *env, jclass that, jint arg0, jobject arg1, jint arg2){ DECL_GLOB(pGlob) RECT _arg1, *lparg1=NULL; DEBUG_CALL("MoveMemory\n") if (arg1) lparg1 = getRECTFields(env, arg1, &_arg1, &PGLOB(RECTFc)); MoveMemory((PVOID)arg0, (CONST VOID *)lparg1, arg2);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg0)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__ILorg_eclipse_swt_internal_win32_TRIVERTEX_2I (JNIEnv *env, jclass that, jint arg0, jobject arg1, jint arg2){ DECL_GLOB(pGlob) TRIVERTEX _arg1, *lparg1=NULL; DEBUG_CALL("MoveMemory\n") if (arg1) lparg1 = getTRIVERTEXFields(env, arg1, &_arg1, &PGLOB(TRIVERTEXFc)); MoveMemory((PVOID)arg0, (CONST VOID *)lparg1, arg2);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg0)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__I_3BI (JNIEnv *env, jclass that, jint arg0, jbyteArray arg1, jint arg2){ jbyte *lparg1=NULL; DEBUG_CALL("MoveMemory\n") if (arg1) lparg1 = (*env)->GetByteArrayElements(env, arg1, NULL); MoveMemory((PVOID)arg0, (CONST VOID *)lparg1, arg2); if (arg1) (*env)->ReleaseByteArrayElements(env, arg1, lparg1, 0);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg0)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__ILorg_eclipse_swt_internal_win32_WINDOWPOS_2I (JNIEnv *env, jclass that, jint arg0, jobject arg1, jint arg2){ DECL_GLOB(pGlob) WINDOWPOS _arg1, *lparg1=NULL; DEBUG_CALL("MoveMemory\n") if (arg1) lparg1 = getWINDOWPOSFields(env, arg1, &_arg1, &PGLOB(WINDOWPOSFc)); MoveMemory((PVOID)arg0, (CONST VOID *)lparg1, arg2);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory___3BII (JNIEnv *env, jclass that, jbyteArray arg0, jint arg1, jint arg2){ jbyte *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = (*env)->GetByteArrayElements(env, arg0, NULL); MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) (*env)->ReleaseByteArrayElements(env, arg0, lparg0, 0);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
DEBUG_CHECK_NULL(env, arg1)
JNIEXPORT void JNICALL Java_org_eclipse_swt_internal_win32_OS_MoveMemory__Lorg_eclipse_swt_internal_win32_WINDOWPOS_2II (JNIEnv *env, jclass that, jobject arg0, jint arg1, jint arg2){ DECL_GLOB(pGlob) WINDOWPOS _arg0, *lparg0=NULL; DEBUG_CALL("MoveMemory\n") if (arg0) lparg0 = &_arg0; MoveMemory((PVOID)lparg0, (CONST VOID *)arg1, arg2); if (arg0) setWINDOWPOSFields(env, arg0, lparg0, &PGLOB(WINDOWPOSFc));}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/ea8b10752d0a908b46d628e28b8b2033139f866e/swt.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/swt.c
if (arg2) (*env)->ReleaseByteArrayElements(env, arg2, lparg2, 0);
JNIEXPORT jint JNICALL OS_NATIVE(gtk_1text_1buffer_1get_1text) (JNIEnv *env, jclass that, jint arg0, jbyteArray arg1, jbyteArray arg2, jboolean arg3){ jbyte *lparg1=NULL; jbyte *lparg2=NULL; jint rc; NATIVE_ENTER(env, that, "gtk_1text_1buffer_1get_1text\n") if (arg1) lparg1 = (*env)->GetByteArrayElements(env, arg1, NULL); if (arg2) lparg2 = (*env)->GetByteArrayElements(env, arg2, NULL); rc = (jint)gtk_text_buffer_get_text((GtkTextBuffer *)arg0, (GtkTextIter *)lparg1, (GtkTextIter *)lparg2, (gboolean)arg3); if (arg1) (*env)->ReleaseByteArrayElements(env, arg1, lparg1, 0); if (arg2) (*env)->ReleaseByteArrayElements(env, arg2, lparg2, 0); NATIVE_EXIT(env, that, "gtk_1text_1buffer_1get_1text\n") return rc;}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/6a52ce5f9ad9a011658713e467efe5bce839e4e8/os.c/clean/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os.c
if (arg2) (*env)->ReleaseIntArrayElements(env, arg2, lparg2, 0);
JNIEXPORT void JNICALL OS_NATIVE(gtk_1widget_1style_1get) (JNIEnv *env, jclass that, jint arg0, jbyteArray arg1, jintArray arg2, jint arg3){ jbyte *lparg1=NULL; jint *lparg2=NULL; NATIVE_ENTER(env, that, "gtk_1widget_1style_1get\n") if (arg1) lparg1 = (*env)->GetByteArrayElements(env, arg1, NULL); if (arg2) lparg2 = (*env)->GetIntArrayElements(env, arg2, NULL); gtk_widget_style_get((GtkWidget *)arg0, (const gchar *)lparg1, lparg2, arg3); if (arg1) (*env)->ReleaseByteArrayElements(env, arg1, lparg1, 0); if (arg2) (*env)->ReleaseIntArrayElements(env, arg2, lparg2, 0); NATIVE_EXIT(env, that, "gtk_1widget_1style_1get\n")}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/6a52ce5f9ad9a011658713e467efe5bce839e4e8/os.c/clean/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os.c
}; };
} }
void getDriveAndPath(const QString& path, QString& drive, QString& rest){ drive=""; rest=""; QStringList list=QStringList::split("/",path); for (QStringList::Iterator it=list.begin(); it!=list.end(); it++) { if (it==list.begin()) drive=(*it)+":"; else rest=rest+"/"+(*it); };};
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/636da51c007c5938d231a1505ac78200190911a5/kio_floppy.cpp/clean/kioslave/floppy/kio_floppy.cpp
(*env)->SetIntField(env, lpObject, WNDCLASSFc.lpszMenuName, (jint)lpStruct->lpszMenuName); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hbrBackground, (jint)lpStruct->hbrBackground); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hCursor, (jint)lpStruct->hCursor); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hIcon, (jint)lpStruct->hIcon); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hInstance, (jint)lpStruct->hInstance); (*env)->SetIntField(env, lpObject, WNDCLASSFc.cbWndExtra, (jint)lpStruct->cbWndExtra); (*env)->SetIntField(env, lpObject, WNDCLASSFc.cbClsExtra, (jint)lpStruct->cbClsExtra); (*env)->SetIntField(env, lpObject, WNDCLASSFc.lpfnWndProc, (jint)lpStruct->lpfnWndProc); (*env)->SetIntField(env, lpObject, WNDCLASSFc.style, (jint)lpStruct->style);
void setWNDCLASSFields(JNIEnv *env, jobject lpObject, WNDCLASS *lpStruct){ if (!WNDCLASSFc.cached) cacheWNDCLASSFields(env, lpObject); (*env)->SetIntField(env, lpObject, WNDCLASSFc.lpszClassName, (jint)lpStruct->lpszClassName); (*env)->SetIntField(env, lpObject, WNDCLASSFc.lpszMenuName, (jint)lpStruct->lpszMenuName); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hbrBackground, (jint)lpStruct->hbrBackground); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hCursor, (jint)lpStruct->hCursor); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hIcon, (jint)lpStruct->hIcon); (*env)->SetIntField(env, lpObject, WNDCLASSFc.hInstance, (jint)lpStruct->hInstance); (*env)->SetIntField(env, lpObject, WNDCLASSFc.cbWndExtra, (jint)lpStruct->cbWndExtra); (*env)->SetIntField(env, lpObject, WNDCLASSFc.cbClsExtra, (jint)lpStruct->cbClsExtra); (*env)->SetIntField(env, lpObject, WNDCLASSFc.lpfnWndProc, (jint)lpStruct->lpfnWndProc); (*env)->SetIntField(env, lpObject, WNDCLASSFc.style, (jint)lpStruct->style);}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/87f953c8eda7bc7ceddf6227acc0065974196814/structs.c/clean/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/structs.c
static int obp=0;
static void out_html(const char *c){ if (!c) return; // Added, probably due to the const? char *c2 = qstrdup(c); char *c3 = c2; if (no_newline_output) { int i=0; no_newline_output=1; while (c2[i]) { if (!no_newline_output) c2[i-1]=c2[i]; if (c2[i]=='\n') no_newline_output=0; i++; } if (!no_newline_output) c2[i-1]=0; } if (scaninbuff) { while (*c2) { if (buffpos>=buffmax) { char *h = new char[buffmax*2]; if (!h) exit(1); memcpy(h, buffer, buffmax); delete [] buffer; buffer=h; buffmax=buffmax*2; } buffer[buffpos++]=*c2++; } } else if (output_possible) { while (*c2) { outbuffer[obp++]=*c2; if (*c=='\n' || obp >= HUGE_STR_MAX) { outbuffer[obp]='\0'; add_links(outbuffer); obp=0; } c2++; } } delete [] c3;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/73a483f2715bcbbc280c566c827c49f20a975fd5/man2html.cpp/clean/kioslave/man/man2html.cpp
while (isalnum(*c) || *c == '.' || *c == ':' ||
while (isalnum(*c) || *c == '.' || *c == ':' ||
static char *scan_request(char *c){ /* BSD Mandoc stuff */ static int mandoc_synopsis=0; /* True if we are in the synopsis section */ static int mandoc_command=0; /* True if this is mandoc page */ static int mandoc_bd_options; /* Only copes with non-nested Bd's */ int i,j,mode=0; char *h; char *wordlist[MAX_WORDLIST]; int words; char *sl; STRDEF *owndef; while (*c==' ' || *c=='\t') c++; if (c[0]=='\n') return c+1; if (c[1]=='\n') j=1; else j=2; while (c[j]==' ' || c[j]=='\t') j++; if (c[0]==escapesym) { /* some pages use .\" .\$1 .\} */ /* .\$1 is too difficult/stuppid */ if (c[1]=='$') c=skip_till_newline(c); else c = scan_escape(c+1); } else { i=V(c[0],c[1]); switch (i) { case V('a','b'): h=c+j; while (*h && *h !='\n') h++; *h='\0'; if (scaninbuff && buffpos) { buffer[buffpos]='\0'; puts(buffer); } /* fprintf(stderr, "%s\n", c+2); */ return 0; break; case V('d','i'): { STRDEF *de; /* int oldcurpos=curpos; */ c=c+j; i=V(c[0],c[1]); if (*c=='\n') { c++;break; } while (*c && *c!='\n') c++; c++; h=c; while (*c && strncmp(c,".di",3)) while (*c && *c++!='\n'); *c='\0'; de=strdef; while (de && de->nr !=i) de=de->next; if (!de) { de=new STRDEF(); de->nr=i; de->slen=0; de->next=strdef; de->st=NULL; strdef=de; } else { delete [] de->st; de->slen=0; de->st=NULL; } scan_troff(h,0,&de->st); if (*c) *c='.'; while (*c && *c++!='\n'); break; } case V('d','s'): mode=1; case V('a','s'): { STRDEF *de; int oldcurpos=curpos; c=c+j; i=V(c[0],c[1]); j=0; while (c[j] && c[j]!='\n') j++; if (j<3) { c=c+j; break; } if (c[1]==' ') c=c+1; else c=c+2; while (isspace(*c)) c++; if (*c=='"') c++; de=strdef; while (de && de->nr != i) de=de->next; single_escape=1; curpos=0; if (!de) { char *h; de=new STRDEF(); de->nr=i; de->slen=0; de->next=strdef; de->st=NULL; strdef=de; h=NULL; c=scan_troff(c, 1, &h); de->st=h; de->slen=curpos; } else { if (mode) { char *h=NULL; c=scan_troff(c, 1, &h); delete [] de->st; de->slen=0; de->st=h; } else c=scan_troff(c,1,&de->st); de->slen+=curpos; } single_escape=0; curpos=oldcurpos; } break; case V('b','r'): if (still_dd) out_html("<DD>"); else out_html("<BR>\n"); curpos=0; c=c+j; if (c[0]==escapesym) { c=scan_escape(c+1); } c=skip_till_newline(c);break; case V('c','2'): c=c+j; if (*c!='\n') { nobreaksym=*c; } else nobreaksym='\''; c=skip_till_newline(c); break; case V('c','c'): c=c+j; if (*c!='\n') { controlsym=*c; } else controlsym='.'; c=skip_till_newline(c); break; case V('c','e'): c=c+j; if (*c=='\n') { i=1; } else { i=0; while ('0'<=*c && *c<='9') { i=i*10+*c-'0'; c++; } } c=skip_till_newline(c); /* center next i lines */ if (i>0) { out_html("<CENTER>\n"); while (i && *c) { char *line=NULL; c=scan_troff(c,1, &line); if (line && strncmp(line, "<BR>", 4)) { out_html(line); out_html("<BR>\n"); delete [] line; i--; } } out_html("</CENTER>\n"); curpos=0; } break; case V('e','c'): c=c+j; if (*c!='\n') { escapesym=*c; } else escapesym='\\'; break; c=skip_till_newline(c); case V('e','o'): escapesym='\0'; c=skip_till_newline(c); break; case V('e','x'): return 0; break; case V('f','c'): c=c+j; if (*c=='\n') { fieldsym=padsym='\0'; } else { fieldsym=c[0]; padsym=c[1]; } c=skip_till_newline(c); break; case V('f','i'): if (!fillout) { out_html(change_to_font(0)); out_html(change_to_size('0')); out_html("</PRE>\n"); } curpos=0; fillout=1; c=skip_till_newline(c); break; case V('f','t'): c=c+j; if (*c=='\n') { out_html(change_to_font(0)); } else { if (*c==escapesym) { int fn; c=scan_expression(c, &fn); c--; out_html(change_to_font(fn)); } else { out_html(change_to_font(*c)); c++; } } c=skip_till_newline(c); break; case V('e','l'): { int ifelseval = s_ifelseval.pop(); /* .el anything : else part of if else */ if (ifelseval) { c=c+j; c[-1]='\n'; c=scan_troff(c,1,NULL); } else c=skip_till_newline(c+j); break; } case V('i','e'): /* .ie c anything : then part of if else */ case V('i','f'): { /* .if c anything * .if !c anything * .if N anything * .if !N anything * .if 'string1'string2' anything * .if !'string1'string2' anything */ c=c+j; c=scan_expression(c, &i); int ifelseval=!i; s_ifelseval.push( ifelseval ); if (i) { *c='\n'; c++; c=scan_troff(c,1,NULL); } else c=skip_till_newline(c); break; } case V('i','g'): { const char *endwith="..\n"; i=3; c=c+j; if (*c!='\n' && *c != '\\') { /* Not newline or comment */ endwith=c-1;i=1; c[-1]='.'; while (*c && *c!='\n') c++,i++; } c++; while (*c && strncmp(c,endwith,i)) while (*c++!='\n'); while (*c && *c++!='\n'); break; } case V('n','f'): if (fillout) { out_html(change_to_font(0)); out_html(change_to_size('0')); out_html("<PRE>\n"); } curpos=0; fillout=0; c=skip_till_newline(c); break; case V('p','s'): c=c+j; if (*c=='\n') { out_html(change_to_size('0')); } else { j=0;i=0; if (*c=='-') { j= -1;c++; } else if (*c=='+') { j=1;c++;} c=scan_expression(c, &i); if (!j) { j=1; if (i>5) i=i-10; } out_html(change_to_size(i*j)); } c=skip_till_newline(c); break; case V('s','p'): c=c+j; if (fillout) out_html("<P>"); else { out_html(NEWLINE); NEWLINE[0]='\n'; } curpos=0; c=skip_till_newline(c); break; case V('s','o'): { /* FILE *f; */ char *buf; char *name=NULL; curpos=0; c=c+j; if (*c=='/') { h=c; } else { h=c-3; h[0]='.'; h[1]='.'; h[2]='/'; } while (*c!='\n') c++; *c='\0'; scan_troff(h,1, &name); if (name[3]=='/') h=name+3; else h=name; /* this works alright, except for section 3 */ buf=read_man_page(h); if (!buf) { fprintf(stderr, "man2html: unable to open or read file %s.\n", h); out_html("<BLOCKQUOTE>" "man2html: unable to open or read file.\n"); out_html(h); out_html("</BLOCKQUOTE>\n"); } else { scan_troff(buf+1,0,NULL); } if (buf) delete [] buf; if (name) delete [] name; *c++='\n'; break; } case V('t','a'): c=c+j; j=0; while (*c!='\n') { sl=scan_expression(c, &tabstops[j]); if (j>0 && (*c=='-' || *c=='+')) tabstops[j]+=tabstops[j-1]; c=sl; while (*c==' ' || *c=='\t') c++; j++; } maxtstop=j; curpos=0; break; case V('t','i'): /*while (itemdepth || dl_set[itemdepth]) { out_html("</DL>\n"); if (dl_set[itemdepth]) dl_set[itemdepth]=0; else itemdepth--; }*/ out_html("<BR>\n"); c=c+j; c=scan_expression(c, &j); for (i=0; i<j; i++) out_html("&nbsp;"); curpos=j; c=skip_till_newline(c); break; case V('t','m'): c=c+j; h=c; while (*c!='\n') c++; *c='\0'; /* fprintf(stderr,"%s\n", h); */ *c='\n'; break; case V('B',' '): case V('B','\n'): case V('I',' '): case V('I','\n'): /* parse one line in a certain font */ out_html(change_to_font(*c)); trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; c=scan_troff(c, 1, NULL); out_html(change_to_font('R')); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('F','d'): //for "Function definitions", mdoc package case V('F','n'): //for "Function calls": brackets and commas have to be inserted automatically case V('F','o'): case V('F','c'): { bool inFdMode=(c[1]=='d'); char font[2] ; font[0] = 'B'; font[1] = 'B'; c=c+j; if (*c=='\n') c++; char *eol=strchr(c,'\n'); char *semicolon=strchr(c,';'); if ((semicolon!=0) && (semicolon<eol)) *semicolon=' '; sl=fill_words(c, wordlist, &words); c=sl+1; /* .BR name (section) ** indicates a link. It will be added in the output routine. */ for (i=0; i<words; i++) { wordlist[i][-1]=' '; out_html(change_to_font(font[i&1])); scan_troff(wordlist[i],1,NULL); if (inFdMode) continue; if (i==0) { out_html(" ("); if (!mandoc_synopsis) out_html(") "); } else if (i<words-1) out_html(", "); } if (mandoc_synopsis) { if (!inFdMode) out_html(");"); out_html("<br>"); }; out_html(change_to_font('R')); out_html(NEWLINE); if (!fillout) curpos=0; else curpos++; }; break; case V('O','P'): /* groff manpages use this construction */ /* .OP a b : [ <B>a</B> <I>b</I> ] */ mode=1; c[0]='B'; c[1]='I'; out_html(change_to_font('R')); out_html("["); curpos++; case V('F','t'): //perhaps "Function return type" case V('F','a'): //"Function argument" case V('B','R'): case V('B','I'): case V('I','B'): case V('I','R'): case V('R','B'): case V('R','I'): { bool inFMode=(c[0]=='F'); if (inFMode) { c[0]='B'; c[1]='I'; }; char font[2] ; font[0] = c[0]; font[1] = c[1]; c=c+j; if (*c=='\n') c++; sl=fill_words(c, wordlist, &words); c=sl+1; /* .BR name (section) ** indicates a link. It will be added in the output routine. */ for (i=0; i<words; i++) { if ((mode) || (inFMode)) { out_html(" "); curpos++; } wordlist[i][-1]=' '; out_html(change_to_font(font[i&1])); scan_troff(wordlist[i],1,NULL); } out_html(change_to_font('R')); if (mode) { out_html(" ]"); curpos++; } out_html(NEWLINE); if (!fillout) curpos=0; else curpos++; } break; case V('D','T'): for (j=0;j<20; j++) tabstops[j]=(j+1)*8; maxtstop=20; c=skip_till_newline(c); break; case V('I','P'): sl=fill_words(c+j, wordlist, &words); c=sl+1; if (!dl_set[itemdepth]) { out_html("<DL COMPACT>\n"); dl_set[itemdepth]=1; } out_html("<DT>"); if (words) { scan_troff(wordlist[0], 1,NULL); } out_html("<DD>"); curpos=0; break; case V('T','P'): if (!dl_set[itemdepth]) { out_html("<P><DL COMPACT>\n"); dl_set[itemdepth]=1; } out_html("<DT>"); c=skip_till_newline(c); /* somewhere a definition ends with '.TP' */ if (!*c) still_dd=1; else { c=scan_troff(c,1,NULL); out_html("<DD>"); } curpos=0; break; case V('I','X'): /* general index */ c=skip_till_newline(c); break; case V('P',' '): case V('P','\n'): case V('L','P'): case V('P','P'): if (dl_set[itemdepth]) { out_html("</DL>\n"); dl_set[itemdepth]=0; } if (fillout) out_html("<P>\n"); else { out_html(NEWLINE); NEWLINE[0]='\n'; } curpos=0; c=skip_till_newline(c); break; case V('H','P'): if (!dl_set[itemdepth]) { out_html("<DL COMPACT>"); dl_set[itemdepth]=1; } out_html("<DT>\n"); still_dd=1; c=skip_till_newline(c); curpos=0; break; case V('P','D'): c=skip_till_newline(c); break; case V('R','s'): /* BSD mandoc */ case V('R','S'): sl=fill_words(c+j, wordlist, &words); j=1; if (words>0) scan_expression(wordlist[0], &j); if (j>=0) { itemdepth++; dl_set[itemdepth]=0; out_html("<DL COMPACT><DT><DD>"); c=skip_till_newline(c); curpos=0; break; } case V('R','e'): /* BSD mandoc */ case V('R','E'): if (itemdepth > 0) { if (dl_set[itemdepth]) out_html("</DL>"); out_html("</DL>\n"); itemdepth--; } c=skip_till_newline(c); curpos=0; break; case V('S','B'): out_html(change_to_size(-1)); out_html(change_to_font('B')); c=scan_troff(c+j, 1, NULL); out_html(change_to_font('R')); out_html(change_to_size('0')); break; case V('S','M'): c=c+j; if (*c=='\n') c++; out_html(change_to_size(-1)); trans_char(c,'"','\a'); c=scan_troff(c,1,NULL); out_html(change_to_size('0')); break; case V('S','s'): /* BSD mandoc */ mandoc_command = 1; case V('S','S'): mode=1; case V('S','h'): /* BSD mandoc */ /* hack for fallthru from above */ mandoc_command = !mode || mandoc_command; case V('S','H'): c=c+j; if (*c=='\n') c++; while (itemdepth || dl_set[itemdepth]) { out_html("</DL>\n"); if (dl_set[itemdepth]) dl_set[itemdepth]=0; else if (itemdepth > 0) itemdepth--; } out_html(change_to_font(0)); out_html(change_to_size(0)); if (!fillout) { fillout=1; out_html("</PRE>"); } trans_char(c,'"', '\a'); /* &nbsp; for mosaic users */ if (section) { out_html("</div>\n"); section=0; } if (mode) out_html("\n<H3>"); else out_html("\n<H2>"); mandoc_synopsis = strncmp(c, "SYNOPSIS", 8) == 0; c = mandoc_command ? scan_troff_mandoc(c,1,NULL) : scan_troff(c,1,NULL); if (mode) out_html("</H3>\n"); else out_html("</H2>\n"); out_html("<div>\n"); section=1; curpos=0; break; case V('S','x'): // reference to a section header out_html(change_to_font('B')); trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; c=scan_troff(c, 1, NULL); out_html(change_to_font('R')); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('T','S'): c=scan_table(c); break; case V('D','t'): /* BSD mandoc */ mandoc_command = 1; case V('T','H'): if (!output_possible) { sl = fill_words(c+j, wordlist, &words); if (words>1) { for (i=1; i<words; i++) wordlist[i][-1]='\0'; *sl='\0'; output_possible=1; out_html( DOCTYPE"<HTML><HEAD><TITLE>Manpage of "); out_html( wordlist[0]); out_html( "</TITLE>\n"); out_html( "<link rel=\"stylesheet\" href=\"KDE_COMMON_DIR/kde-default.css\" type=\"text/css\">\n" ); out_html( "</HEAD>\n\n" ); out_html("<BODY BGCOLOR=\"#FFFFFF\">\n\n" ); out_html("<div id=\"headline\" style=\"position : absolute; height : 85px; z-index :\n" ); out_html("100; background : transparent; text-align : center; text-transform:\n" ); out_html("smallcaps; width : 100%; top : 0px; left : 0px; width : 100%; color :\n" ); out_html("#000000;\">\n" ); out_html("\n" ); out_html("<H1>" ); out_html( wordlist[0] ); out_html("</H1>\n" ); out_html("</div>\n" ); out_html("<div id=\"navbackground\" style=\"position : absolute; width : 100%; height\n" ); out_html(": 124px; background-image : url('KDE_COMMON_DIR/doctop2.png'); z-index : 5; left\n" ); out_html(": 0px; top : 0px; padding : 0px;\"> <div id=\"bulb1\" style=\"padding : 0px;\n" ); out_html("position : absolute; z-index : 15; width : 150px; height : 85px; top :\n" ); out_html("0px; left : 0px; background : url('KDE_COMMON_DIR/doctop1.png') repeat;\"></div>\n" ); out_html("<div id=\"gradient\" style=\"position : absolute; width : 275px; height :\n" ); out_html("85px; z-index : 19px; top : 0px; padding : 0px; left : 150px;\n" ); out_html("background-image : url('KDE_COMMON_DIR/doctop1a.png'); background-repeat :\n" ); out_html("no-repeat; background-color : transparent; visibility : visible;\"></div>\n" ); out_html("\n" ); out_html("<div id=\"bulb-bit\" style=\"position : absolute; width : 100%; height :\n" ); out_html("25px; top : 85px; left : 0px; background-image :\n" ); out_html("url('KDE_COMMON_DIR/doctop1b.png'); background-repeat : no-repeat;\n" ); out_html("background-color : transparent; z-index : 5;\"></div></div>\n" ); out_html("<h1>" ); out_html( wordlist[0] ); out_html( "</h1>\n" ); out_html("\n" ); out_html("Section: " ); if (words>4) out_html(wordlist[4]); else out_html(section_name(wordlist[1])); out_html(" ("); out_html(wordlist[1]); out_html(")\n"); *sl='\n'; if (mandoc_command) out_html("<BR>BSD mandoc<BR>"); } c=sl+1; } else c=skip_till_newline(c); curpos=0; break; case V('T','X'): { sl=fill_words(c+j, wordlist, &words); *sl='\0'; out_html(change_to_font('I')); if (words>1) wordlist[1][-1]='\0'; const char *c2=lookup_abbrev(wordlist[0]); curpos+=strlen(c2); out_html(c2); out_html(change_to_font('R')); if (words>1) out_html(wordlist[1]); *sl='\n'; c=sl+1; } break; case V('r','m'): /* .rm xx : Remove request, macro or string */ case V('r','n'): /* .rn xx yy : Rename request, macro or string xx to yy */ { STRDEF *de; c=c+j; i=V(c[0],c[1]); c=c+2; while (isspace(*c) && *c!='\n') c++; j=V(c[0],c[1]); while (*c && *c!='\n') c++; c++; de=strdef; while (de && de->nr!=j) de=de->next; if (de) { delete [] de->st; de->st=0; de->nr=0; } de=strdef; while (de && de->nr!=i) de=de->next; if (de) de->nr=j; break; } case V('n','x'): /* .nx filename : next file. */ case V('i','n'): /* .in +-N : Indent */ c=skip_till_newline(c); break; case V('n','r'): /* .nr R +-N M: define and set number register R by +-N; ** auto-increment by M */ { INTDEF *intd; c=c+j; i=V(c[0],c[1]); c=c+2; intd=intdef; while (intd && intd->nr!=i) intd=intd->next; if (!intd) { intd = new INTDEF(); intd->nr=i; intd->val=0; intd->incr=0; intd->next=intdef; intdef=intd; } while (*c==' ' || *c=='\t') c++; c=scan_expression(c,&intd->val); if (*c!='\n') { while (*c==' ' || *c=='\t') c++; c=scan_expression(c,&intd->incr); } c=skip_till_newline(c); break; } case V('a','m'): /* .am xx yy : append to a macro. */ /* define or handle as .ig yy */ mode=1; case V('d','e'): /* .de xx yy : define or redefine macro xx; end at .yy (..) */ /* define or handle as .ig yy */ { STRDEF *de; int olen=0; char c2; c=c+j; sl=fill_words(c, wordlist, &words); c2 = c[1]; if (c2 == '\n') c2 = ' '; i=V(c[0],c2);j=2; if (words==1) { wordlist[1] = qstrdup(".."); } else { wordlist[1]--; wordlist[1][0]='.'; j=3; } c=sl+1; sl=c; while (*c && strncmp(c,wordlist[1],j)) c=skip_till_newline(c); de=defdef; while (de && de->nr!= i) de=de->next; if (mode && de) olen=strlen(de->st); j=olen+c-sl; h = stralloc(j*2+4); if (h) { for (j=0; j<olen; j++) h[j]=de->st[j]; if (!j || h[j-1]!='\n') h[j++]='\n'; while (sl!=c) { if (sl[0]=='\\' && sl[1]=='\\') { h[j++]='\\'; sl++; } else h[j++]=*sl; sl++; } h[j]=0; if (de) { delete [] de->st; de->st=h; } else { de = new STRDEF(); de->nr=i; de->next=defdef; de->st=h; defdef=de; } } if (words==1) { delete [] wordlist[1]; } } c=skip_till_newline(c); break; case V('B','l'): /* BSD mandoc */ { char list_options[NULL_TERMINATED(MED_STR_MAX)]; char *nl = strchr(c,'\n'); c=c+j; if (dl_set[itemdepth]) { /* These things can nest. */ itemdepth++; } if (nl) { /* Parse list options */ strlimitcpy(list_options, c, nl - c, MED_STR_MAX); } if (strstr(list_options, "-bullet")) { /* HTML Unnumbered List */ dl_set[itemdepth] = BL_BULLET_LIST; out_html("<UL>\n"); } else if (strstr(list_options, "-enum")) { /* HTML Ordered List */ dl_set[itemdepth] = BL_ENUM_LIST; out_html("<OL>\n"); } else { /* HTML Descriptive List */ dl_set[itemdepth] = BL_DESC_LIST; out_html("<DL COMPACT>\n"); } if (fillout) out_html("<P>\n"); else { out_html(NEWLINE); NEWLINE[0]='\n'; } curpos=0; c=skip_till_newline(c); break; } case V('E','l'): /* BSD mandoc */ c=c+j; if (dl_set[itemdepth] & BL_DESC_LIST) { out_html("</DL>\n"); } else if (dl_set[itemdepth] & BL_BULLET_LIST) { out_html("</UL>\n"); } else if (dl_set[itemdepth] & BL_ENUM_LIST) { out_html("</OL>\n"); } dl_set[itemdepth]=0; if (itemdepth > 0) itemdepth--; if (fillout) out_html("<P>\n"); else { out_html(NEWLINE); NEWLINE[0]='\n'; } curpos=0; c=skip_till_newline(c); break; case V('I','t'): /* BSD mandoc */ c=c+j; if (strncmp(c, "Xo", 2) == 0 && isspace(*(c+2))) { c = skip_till_newline(c); } if (dl_set[itemdepth] & BL_DESC_LIST) { out_html("<DT>"); out_html(change_to_font('B')); if (*c=='\n') { /* Don't allow embedded comms after a newline */ c++; c=scan_troff(c,1,NULL); } else { /* Do allow embedded comms on the same line. */ c=scan_troff_mandoc(c,1,NULL); } out_html(change_to_font('R')); out_html(NEWLINE); out_html("<DD>"); } else if (dl_set[itemdepth] & (BL_BULLET_LIST | BL_ENUM_LIST)) { out_html("<LI>"); c=scan_troff_mandoc(c,1,NULL); out_html(NEWLINE); } if (fillout) curpos++; else curpos=0; break; case V('B','k'): /* BSD mandoc */ case V('E','k'): /* BSD mandoc */ case V('D','d'): /* BSD mandoc */ case V('O','s'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; c=scan_troff_mandoc(c, 1, NULL); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('B','t'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; out_html(" is currently in beta test."); if (fillout) curpos++; else curpos=0; break; case V('A','t'): /* BSD mandoc */ case V('F','x'): /* BSD mandoc */ case V('N','x'): /* BSD mandoc */ case V('O','x'): /* BSD mandoc */ case V('B','x'): /* BSD mandoc */ case V('U','x'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; if (i==V('A','t')) out_html("AT&amp;T UNIX "); else if (i==V('F','x')) out_html("FreeBSD "); else if (i==V('N','x')) out_html("NetBSD "); else if (i==V('O','x')) out_html("OpenBSD "); else if (i==V('B','x')) out_html("BSD "); else if (i==V('U','x')) out_html("UNIX "); c=scan_troff_mandoc(c, 1, NULL); if (fillout) curpos++; else curpos=0; break; case V('D','l'): /* BSD mandoc */ c=c+j; out_html(NEWLINE); out_html("<BLOCKQUOTE>"); out_html(change_to_font('L')); if (*c=='\n') c++; c=scan_troff_mandoc(c, 1, NULL); out_html(change_to_font('R')); out_html("</BLOCKQUOTE>"); if (fillout) curpos++; else curpos=0; break; case V('B','d'): /* BSD mandoc */ { /* Seems like a kind of example/literal mode */ char bd_options[NULL_TERMINATED(MED_STR_MAX)]; char *nl = strchr(c,'\n'); c=c+j; if (nl) { strlimitcpy(bd_options, c, nl - c, MED_STR_MAX); } out_html(NEWLINE); mandoc_bd_options = 0; /* Remember options for terminating Bl */ if (strstr(bd_options, "-offset indent")) { mandoc_bd_options |= BD_INDENT; out_html("<BLOCKQUOTE>\n"); } if ( strstr(bd_options, "-literal") || strstr(bd_options, "-unfilled")) { if (fillout) { mandoc_bd_options |= BD_LITERAL; out_html(change_to_font(0)); out_html(change_to_size('0')); out_html("<PRE>\n"); } curpos=0; fillout=0; } c=skip_till_newline(c); break; } case V('E','d'): /* BSD mandoc */ if (mandoc_bd_options & BD_LITERAL) { if (!fillout) { out_html(change_to_font(0)); out_html(change_to_size('0')); out_html("</PRE>\n"); } } if (mandoc_bd_options & BD_INDENT) out_html("</BLOCKQUOTE>\n"); curpos=0; fillout=1; c=skip_till_newline(c); break; case V('B','e'): /* BSD mandoc */ c=c+j; if (fillout) out_html("<P>"); else { out_html(NEWLINE); NEWLINE[0]='\n'; } curpos=0; c=skip_till_newline(c); break; case V('X','r'): /* BSD mandoc */ { /* Translate xyz 1 to xyz(1) * Allow for multiple spaces. Allow the section to be missing. */ char buff[NULL_TERMINATED(MED_STR_MAX)]; char *bufptr; trans_char(c,'"','\a'); bufptr = buff; c = c+j; if (*c == '\n') c++; /* Skip spaces */ while (isspace(*c) && *c != '\n') c++; while (isalnum(*c) || *c == '.' || *c == ':' || *c == '_' || *c == '-') { /* Copy the xyz part */ *bufptr = *c; bufptr++; if (bufptr >= buff + MED_STR_MAX) break; c++; } while (isspace(*c) && *c != '\n') c++; /* Skip spaces */ if (isdigit(*c)) { /* Convert the number if there is one */ *bufptr = '('; bufptr++; if (bufptr < buff + MED_STR_MAX) { while (isalnum(*c)) { *bufptr = *c; bufptr++; if (bufptr >= buff + MED_STR_MAX) break; c++; } if (bufptr < buff + MED_STR_MAX) { *bufptr = ')'; bufptr++; } } } while (*c != '\n') { /* Copy the remainder */ if (!isspace(*c)) { *bufptr = *c; bufptr++; if (bufptr >= buff + MED_STR_MAX) break; } c++; } *bufptr = '\n'; bufptr[1] = 0; scan_troff_mandoc(buff, 1, NULL); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; } break; case V('F','l'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; out_html("-"); if (*c!='\n') { out_html(change_to_font('B')); c=scan_troff_mandoc(c, 1, NULL); out_html(change_to_font('R')); } out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('P','a'): /* BSD mandoc */ case V('P','f'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; c=scan_troff_mandoc(c, 1, NULL); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('P','p'): /* BSD mandoc */ if (fillout) out_html("<P>\n"); else { out_html(NEWLINE); NEWLINE[0]='\n'; } curpos=0; c=skip_till_newline(c); break; case V('D','q'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; out_html("``"); c=scan_troff_mandoc(c, 1, NULL); out_html("''"); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('O','p'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; out_html(change_to_font('R')); out_html("["); c=scan_troff_mandoc(c, 1, NULL); out_html(change_to_font('R')); out_html("]"); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('O','o'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; out_html(change_to_font('R')); out_html("["); c=scan_troff_mandoc(c, 1, NULL); if (fillout) curpos++; else curpos=0; break; case V('O','c'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; c=scan_troff_mandoc(c, 1, NULL); out_html(change_to_font('R')); out_html("]"); if (fillout) curpos++; else curpos=0; break; case V('P','q'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; out_html("("); c=scan_troff_mandoc(c, 1, NULL); out_html(")"); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('Q','l'): /* BSD mandoc */ { /* Single quote first word in the line */ char *sp; trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; sp = c; do { /* Find first whitespace after the * first word that isn't a mandoc macro */ while (*sp && isspace(*sp)) sp++; while (*sp && !isspace(*sp)) sp++; } while (*sp && isupper(*(sp-2)) && islower(*(sp-1))); /* Use a newline to mark the end of text to * be quoted */ if (*sp) *sp = '\n'; out_html("`"); /* Quote the text */ c=scan_troff_mandoc(c, 1, NULL); out_html("'"); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; } case V('S','q'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; out_html("`"); c=scan_troff_mandoc(c, 1, NULL); out_html("'"); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('A','r'): /* BSD mandoc */ /* parse one line in italics */ out_html(change_to_font('I')); trans_char(c,'"','\a'); c=c+j; if (*c=='\n') { /* An empty Ar means "file ..." */ out_html("file ..."); } else { c=scan_troff_mandoc(c, 1, NULL); } out_html(change_to_font('R')); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('A','d'): /* BSD mandoc */ case V('E','m'): /* BSD mandoc */ case V('V','a'): /* BSD mandoc */ case V('X','c'): /* BSD mandoc */ /* parse one line in italics */ out_html(change_to_font('I')); trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; c=scan_troff_mandoc(c, 1, NULL); out_html(change_to_font('R')); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('N','d'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; out_html(" - "); c=scan_troff_mandoc(c, 1, NULL); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('N','m'): /* BSD mandoc */ { static char mandoc_name[NULL_TERMINATED(SMALL_STR_MAX)] = ""; trans_char(c,'"','\a'); c=c+j; if (mandoc_synopsis) { /* Break lines only in the Synopsis. * The Synopsis section seems to be treated * as a special case - Bummer! */ static int count = 0; /* Don't break on the first Nm */ if (count) { out_html("<BR>"); } else { char *end = strchr(c, '\n'); if (end) { /* Remember the name for later. */ strlimitcpy(mandoc_name, c, end - c, SMALL_STR_MAX); } } count++; } out_html(change_to_font('B')); while (*c == ' '|| *c == '\t') c++; if (*c == '\n') { /* If Nm has no argument, use one from an earlier * Nm command that did have one. Hope there aren't * too many commands that do this. */ out_html(mandoc_name); } else { c=scan_troff_mandoc(c, 1, NULL); } out_html(change_to_font('R')); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; } case V('C','d'): /* BSD mandoc */ case V('C','m'): /* BSD mandoc */ case V('I','c'): /* BSD mandoc */ case V('M','s'): /* BSD mandoc */ case V('O','r'): /* BSD mandoc */ case V('S','y'): /* BSD mandoc */ /* parse one line in bold */ out_html(change_to_font('B')); trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; c=scan_troff_mandoc(c, 1, NULL); out_html(change_to_font('R')); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('D','v'): /* BSD mandoc */ case V('E','v'): /* BSD mandoc */ case V('F','r'): /* BSD mandoc */ case V('L','i'): /* BSD mandoc */ case V('N','o'): /* BSD mandoc */ case V('N','s'): /* BSD mandoc */ case V('T','n'): /* BSD mandoc */ case V('n','N'): /* BSD mandoc */ trans_char(c,'"','\a'); c=c+j; if (*c=='\n') c++; out_html(change_to_font('B')); c=scan_troff_mandoc(c, 1, NULL); out_html(change_to_font('R')); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; break; case V('%','A'): /* BSD mandoc biblio stuff */ case V('%','D'): case V('%','N'): case V('%','O'): case V('%','P'): case V('%','Q'): case V('%','V'): c=c+j; if (*c=='\n') c++; c=scan_troff(c, 1, NULL); /* Don't allow embedded mandoc coms */ if (fillout) curpos++; else curpos=0; break; case V('%','B'): case V('%','J'): case V('%','R'): case V('%','T'): c=c+j; out_html(change_to_font('I')); if (*c=='\n') c++; c=scan_troff(c, 1, NULL); /* Don't allow embedded mandoc coms */ out_html(change_to_font('R')); if (fillout) curpos++; else curpos=0; break; default: /* search macro database of self-defined macros */ owndef = defdef; while (owndef && owndef->nr!=i) owndef=owndef->next; if (owndef) { char **oldargument; int deflen; int onff; sl=fill_words(c+j, wordlist, &words); c=sl+1; *sl='\0'; for (i=1;i<words; i++) wordlist[i][-1]='\0'; for (i=0; i<words; i++) { char *h=NULL; if (mandoc_command) { scan_troff_mandoc(wordlist[i],1,&h); } else { scan_troff(wordlist[i],1,&h); } wordlist[i] = qstrdup(h); delete [] h; } for (i=words;i<20; i++) wordlist[i]=NULL; deflen = strlen(owndef->st); for (i=0; (owndef->st[deflen+2+i]=owndef->st[i]); i++); oldargument=argument; argument=wordlist; onff=newline_for_fun; if (mandoc_command) { scan_troff_mandoc(owndef->st+deflen+2, 0, NULL); } else { scan_troff(owndef->st+deflen+2, 0, NULL); } newline_for_fun=onff; argument=oldargument; for (i=0; i<words; i++) delete [] wordlist[i]; *sl='\n'; } else if (mandoc_command && ((isupper(*c) && islower(*(c+1))) || (islower(*c) && isupper(*(c+1)))) ) { /* Let through any BSD mandoc commands that haven't * been delt with. * I don't want to miss anything out of the text. */ char buf[4]; strncpy(buf,c,2); buf[2] = ' '; buf[3] = '\0'; out_html(buf); /* Print the command (it might just be text). */ c=c+j; trans_char(c,'"','\a'); if (*c=='\n') c++; out_html(change_to_font('R')); c=scan_troff(c, 1, NULL); out_html(NEWLINE); if (fillout) curpos++; else curpos=0; } else { c=skip_till_newline(c); } break; } } if (fillout) { out_html(NEWLINE); curpos++; } NEWLINE[0]='\n'; return c;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/73a483f2715bcbbc280c566c827c49f20a975fd5/man2html.cpp/clean/kioslave/man/man2html.cpp
(JNIEnv *env, jclass, jint fnNumber, jint ppVtbl, jint arg0, jint arg1)
(JNIEnv *env, jclass, jint fnNumber, jint ppVtbl, jint arg0)
JNIEXPORT void JNICALL XPCOM_NATIVE(VtblCallNoRet__III) (JNIEnv *env, jclass, jint fnNumber, jint ppVtbl, jint arg0, jint arg1){ P_OLE_FNNORET_3 fn = (P_OLE_FNNORET_3)(*(int **)ppVtbl)[fnNumber]; fn(ppVtbl, arg0, arg1); }
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/b3328f923c6c5acfa69ef5a114838ed79faa9028/xpcom.cpp/clean/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/library/xpcom.cpp
P_OLE_FNNORET_3 fn = (P_OLE_FNNORET_3)(*(int **)ppVtbl)[fnNumber];
P_OLE_FNNORET_2 fn = (P_OLE_FNNORET_2)(*(int **)ppVtbl)[fnNumber];
JNIEXPORT void JNICALL XPCOM_NATIVE(VtblCallNoRet__III) (JNIEnv *env, jclass, jint fnNumber, jint ppVtbl, jint arg0, jint arg1){ P_OLE_FNNORET_3 fn = (P_OLE_FNNORET_3)(*(int **)ppVtbl)[fnNumber]; fn(ppVtbl, arg0, arg1); }
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/b3328f923c6c5acfa69ef5a114838ed79faa9028/xpcom.cpp/clean/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/library/xpcom.cpp
fn(ppVtbl, arg0, arg1);
fn(ppVtbl, arg0);
JNIEXPORT void JNICALL XPCOM_NATIVE(VtblCallNoRet__III) (JNIEnv *env, jclass, jint fnNumber, jint ppVtbl, jint arg0, jint arg1){ P_OLE_FNNORET_3 fn = (P_OLE_FNNORET_3)(*(int **)ppVtbl)[fnNumber]; fn(ppVtbl, arg0, arg1); }
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/b3328f923c6c5acfa69ef5a114838ed79faa9028/xpcom.cpp/clean/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/library/xpcom.cpp
lpStruct->bmBitsPixel = (*env)->GetShortField(env, lpObject, BITMAPFc.bmBitsPixel); lpStruct->bmPlanes = (*env)->GetShortField(env, lpObject, BITMAPFc.bmPlanes); lpStruct->bmWidthBytes = (*env)->GetIntField(env, lpObject, BITMAPFc.bmWidthBytes); lpStruct->bmHeight = (*env)->GetIntField(env, lpObject, BITMAPFc.bmHeight); lpStruct->bmWidth = (*env)->GetIntField(env, lpObject, BITMAPFc.bmWidth); lpStruct->bmType = (*env)->GetIntField(env, lpObject, BITMAPFc.bmType);
BITMAP *getBITMAPFields(JNIEnv *env, jobject lpObject, BITMAP *lpStruct){ if (!BITMAPFc.cached) cacheBITMAPFields(env, lpObject); lpStruct->bmBits = (LPVOID)(*env)->GetIntField(env, lpObject, BITMAPFc.bmBits); lpStruct->bmBitsPixel = (*env)->GetShortField(env, lpObject, BITMAPFc.bmBitsPixel); lpStruct->bmPlanes = (*env)->GetShortField(env, lpObject, BITMAPFc.bmPlanes); lpStruct->bmWidthBytes = (*env)->GetIntField(env, lpObject, BITMAPFc.bmWidthBytes); lpStruct->bmHeight = (*env)->GetIntField(env, lpObject, BITMAPFc.bmHeight); lpStruct->bmWidth = (*env)->GetIntField(env, lpObject, BITMAPFc.bmWidth); lpStruct->bmType = (*env)->GetIntField(env, lpObject, BITMAPFc.bmType); return lpStruct;}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/87f953c8eda7bc7ceddf6227acc0065974196814/structs.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/structs.c
lpStruct->key = (*env)->GetShortField(env, lpObject, ACCELFc.key); lpStruct->fVirt = (*env)->GetByteField(env, lpObject, ACCELFc.fVirt);
ACCEL *getACCELFields(JNIEnv *env, jobject lpObject, ACCEL *lpStruct){ if (!ACCELFc.cached) cacheACCELFields(env, lpObject); lpStruct->cmd = (*env)->GetShortField(env, lpObject, ACCELFc.cmd); lpStruct->key = (*env)->GetShortField(env, lpObject, ACCELFc.key); lpStruct->fVirt = (*env)->GetByteField(env, lpObject, ACCELFc.fVirt); return lpStruct;}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/87f953c8eda7bc7ceddf6227acc0065974196814/structs.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/structs.c
bool mounted() const { return isMounted; };
bool mounted() const { return isMounted; }
bool mounted() const { return isMounted; };
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/1bc02759a28a74399de714c15dd7a3117f6f50de/disks.h/clean/kioslave/devices/kdedmodule/disks.h
KInstance instance( "kio_nntp" ); if (argc != 4) { fprintf(stderr, "usage statement needs to go here\n"); exit(-1); } NNTPProtocol slave(argv[2], argv[3]); slave.dispatchLoop(); return 0;
KInstance instance( "kio_nntp" ); if (argc != 4) { fprintf(stderr, "usage statement needs to go here\n"); exit(-1); } NNTPProtocol slave(argv[2], argv[3]); slave.dispatchLoop(); return 0;
int kdemain(int argc, char **argv){ KInstance instance( "kio_nntp" ); if (argc != 4) { fprintf(stderr, "usage statement needs to go here\n"); exit(-1); } NNTPProtocol slave(argv[2], argv[3]); slave.dispatchLoop(); return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/39a40b82a2fc55ed7e977945ed4c332480807109/nntp.cc/buggy/kioslave/nntp/nntp.cc
VG_(printf)("info->exebase %p\n info->map_base %p\n",info->exe_base,info->map_base);
static int load_ELF(char *hdr, int len, int fd, const char *name, struct exeinfo *info){ struct elfinfo *e; struct elfinfo *interp = NULL; ESZ(Addr) minaddr = ~0; /* lowest mapped address */ ESZ(Addr) maxaddr = 0; /* highest mapped address */ ESZ(Addr) interp_addr = 0; /* interpreter (ld.so) address */ ESZ(Word) interp_size = 0; /* interpreter size */ ESZ(Word) interp_align = VKI_PAGE_SIZE; int i; void *entry; ESZ(Addr) ebase = 0;#ifdef HAVE_PIE ebase = info->exe_base;#endif VG_(printf)("In load_ELF!\n"); e = readelf(fd, name); if (e == NULL) return ENOEXEC; /* The kernel maps position-independent executables at TASK_SIZE*2/3; duplicate this behavior as close as we can. */ if (e->e.e_type == ET_DYN && ebase == 0) { VG_(printf)("screwing up \n"); ebase = VG_PGROUNDDN(info->exe_base + (info->exe_end - info->exe_base) * 2 / 3); } info->phnum = e->e.e_phnum; info->entry = e->e.e_entry +ebase; info->phdr = 0; if(1){ VG_(printf)("info->phnum:%d\n , info->entry:%p\n",info->phnum, info->entry); } for(i = 0; i < e->e.e_phnum; i++) { ESZ(Phdr) *ph = &e->p[i]; VG_(printf)("p_type = %p\n",ph->p_type); switch(ph->p_type) { case PT_PHDR: printf("in pt_hdr\n"); info->phdr = ph->p_vaddr + ebase; VG_(printf)("info->phdr = ph->p_vaddr + ebase; (%p + %p = %p)\n", ph->p_vaddr, ebase, info->phdr); break; case PT_LOAD: VG_(printf) ("in pt_load\n"); if (ph->p_vaddr < minaddr) minaddr = ph->p_vaddr; if (ph->p_vaddr+ph->p_memsz > maxaddr) maxaddr = ph->p_vaddr+ph->p_memsz; break; case PT_DYNAMIC: VG_(printf) ("in pt_dynamic\n"); if (ph->p_vaddr < minaddr) minaddr = ph->p_vaddr; if (ph->p_vaddr+ph->p_memsz > maxaddr) maxaddr = ph->p_vaddr+ph->p_memsz; VG_(printf)("pt_dynamic_addr = %p [this must match obj->dynamic in ld.so_elf]\n", ph->p_vaddr); break; case PT_INTERP: { char *buf = hack_malloc(ph->p_filesz+1); int j; int intfd; int baseaddr_set; SysRes res; vg_assert(buf); VG_(pread)(fd, buf, ph->p_filesz, ph->p_offset); buf[ph->p_filesz] = '\0';/* intfd = VG_(open)(buf, O_RDONLY, 444); // WAS just open - kailash // *//* if (intfd == -1) { *//* VG_(printf)("valgrind: m_ume.c: can't open interpreter\n"); *//* VG_(exit)(1); *//* } */ res = VG_(open)(buf, VKI_O_RDONLY, VKI_S_IRUSR); if ( !res.isError ) { intfd = res.val; } else { VG_(printf)("valgrind: m_ume.c: can't open interpreter\n"); VG_(exit)(1); } interp = readelf(intfd, buf); if (interp == NULL) { VG_(printf)("valgrind: m_ume.c: can't read interpreter\n"); return 1; } //FIXME VG_(free)(buf); baseaddr_set = 0; for(j = 0; j < interp->e.e_phnum; j++) { ESZ(Phdr) *iph = &interp->p[j]; ESZ(Addr) end; if (iph->p_type != PT_LOAD) continue; if (!baseaddr_set) { interp_addr = iph->p_vaddr; interp_align = iph->p_align; baseaddr_set = 1; } /* assumes that all segments in the interp are close */ end = (iph->p_vaddr - interp_addr) + iph->p_memsz; if (end > interp_size) interp_size = end; } break; default: // do nothing break; } } } printf("interp_addr = %p\n", interp_addr); if (info->phdr == 0) info->phdr = minaddr + ebase + e->e.e_phoff; if (info->exe_base != info->exe_end) { if (minaddr >= maxaddr || (minaddr + ebase < info->exe_base || maxaddr + ebase > info->exe_end)) { VG_(printf)("Executable range %p-%p is outside the\n" "acceptable range %p-%p\n", (void *)minaddr + ebase, (void *)maxaddr + ebase, (void *)info->exe_base, (void *)info->exe_end); return ENOMEM; } } /*XXX- for netbsd I am not sure if this check is right , info->exe_end etc are based on some assumptions which are not valid for linux, for example it assumes stack on top i think , this warrants more investigation. */ info->brkbase = mapelf(e, ebase); /* map the executable */ if (info->brkbase == 0) return ENOMEM; if(!interp) printf("interp is null\n"); else printf("interp = %p\n",interp); if (interp != NULL) { /* reserve a chunk of address space for interpreter */ SysRes res; Char* base = (Char *)info->exe_base; Char* baseoff; Int flags = VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS; if (info->map_base != 0) { base = (char *)VG_ROUNDUP(info->map_base, interp_align); flags |= VKI_MAP_FIXED; } printf("doing mmap here\n"); res = VG_(mmap_native)(base, interp_size, VKI_PROT_NONE, flags, -1, 0); check_mmap(res, base, interp_size); vg_assert(!res.isError); base = (Char*)res.val; baseoff = base - interp_addr; mapelf(interp, (ESZ(Addr))baseoff); printf("after mmap and mapelf here\n"); VG_(close)(interp->fd); entry = baseoff + interp->e.e_entry; info->interp_base = (ESZ(Addr))base; printf("info interp_base = %p\n " ,info->interp_base); //FIXME VG_(free)(interp->p); //FIXME VG_(free)(interp); } else entry = (void *)(ebase + e->e.e_entry); info->exe_base = minaddr + ebase; info->exe_end = maxaddr + ebase; info->init_eip = (Addr)entry; printf("info->init_eip: %x",info->init_eip); //FIXME VG_(free)(e->p); //FIXME VG_(free)(e); return 0;}
12192 /local/tlutelli/issta_data/temp/c/2005_temp/2005/12192/fe4746d6fba90584e880285ed6309b4d005a690a/m_ume.c/clean/branches/aspacem/valgrind/coregrind/m_ume.c
res = VG_(mmap_native)(base, interp_size, VKI_PROT_NONE, flags, -1, 0);
res = VG_(mmap_native)( base , interp_size, VKI_PROT_NONE, flags, -1, 0);
static int load_ELF(char *hdr, int len, int fd, const char *name, struct exeinfo *info){ struct elfinfo *e; struct elfinfo *interp = NULL; ESZ(Addr) minaddr = ~0; /* lowest mapped address */ ESZ(Addr) maxaddr = 0; /* highest mapped address */ ESZ(Addr) interp_addr = 0; /* interpreter (ld.so) address */ ESZ(Word) interp_size = 0; /* interpreter size */ ESZ(Word) interp_align = VKI_PAGE_SIZE; int i; void *entry; ESZ(Addr) ebase = 0;#ifdef HAVE_PIE ebase = info->exe_base;#endif VG_(printf)("In load_ELF!\n"); e = readelf(fd, name); if (e == NULL) return ENOEXEC; /* The kernel maps position-independent executables at TASK_SIZE*2/3; duplicate this behavior as close as we can. */ if (e->e.e_type == ET_DYN && ebase == 0) { VG_(printf)("screwing up \n"); ebase = VG_PGROUNDDN(info->exe_base + (info->exe_end - info->exe_base) * 2 / 3); } info->phnum = e->e.e_phnum; info->entry = e->e.e_entry +ebase; info->phdr = 0; if(1){ VG_(printf)("info->phnum:%d\n , info->entry:%p\n",info->phnum, info->entry); } for(i = 0; i < e->e.e_phnum; i++) { ESZ(Phdr) *ph = &e->p[i]; VG_(printf)("p_type = %p\n",ph->p_type); switch(ph->p_type) { case PT_PHDR: printf("in pt_hdr\n"); info->phdr = ph->p_vaddr + ebase; VG_(printf)("info->phdr = ph->p_vaddr + ebase; (%p + %p = %p)\n", ph->p_vaddr, ebase, info->phdr); break; case PT_LOAD: VG_(printf) ("in pt_load\n"); if (ph->p_vaddr < minaddr) minaddr = ph->p_vaddr; if (ph->p_vaddr+ph->p_memsz > maxaddr) maxaddr = ph->p_vaddr+ph->p_memsz; break; case PT_DYNAMIC: VG_(printf) ("in pt_dynamic\n"); if (ph->p_vaddr < minaddr) minaddr = ph->p_vaddr; if (ph->p_vaddr+ph->p_memsz > maxaddr) maxaddr = ph->p_vaddr+ph->p_memsz; VG_(printf)("pt_dynamic_addr = %p [this must match obj->dynamic in ld.so_elf]\n", ph->p_vaddr); break; case PT_INTERP: { char *buf = hack_malloc(ph->p_filesz+1); int j; int intfd; int baseaddr_set; SysRes res; vg_assert(buf); VG_(pread)(fd, buf, ph->p_filesz, ph->p_offset); buf[ph->p_filesz] = '\0';/* intfd = VG_(open)(buf, O_RDONLY, 444); // WAS just open - kailash // *//* if (intfd == -1) { *//* VG_(printf)("valgrind: m_ume.c: can't open interpreter\n"); *//* VG_(exit)(1); *//* } */ res = VG_(open)(buf, VKI_O_RDONLY, VKI_S_IRUSR); if ( !res.isError ) { intfd = res.val; } else { VG_(printf)("valgrind: m_ume.c: can't open interpreter\n"); VG_(exit)(1); } interp = readelf(intfd, buf); if (interp == NULL) { VG_(printf)("valgrind: m_ume.c: can't read interpreter\n"); return 1; } //FIXME VG_(free)(buf); baseaddr_set = 0; for(j = 0; j < interp->e.e_phnum; j++) { ESZ(Phdr) *iph = &interp->p[j]; ESZ(Addr) end; if (iph->p_type != PT_LOAD) continue; if (!baseaddr_set) { interp_addr = iph->p_vaddr; interp_align = iph->p_align; baseaddr_set = 1; } /* assumes that all segments in the interp are close */ end = (iph->p_vaddr - interp_addr) + iph->p_memsz; if (end > interp_size) interp_size = end; } break; default: // do nothing break; } } } printf("interp_addr = %p\n", interp_addr); if (info->phdr == 0) info->phdr = minaddr + ebase + e->e.e_phoff; if (info->exe_base != info->exe_end) { if (minaddr >= maxaddr || (minaddr + ebase < info->exe_base || maxaddr + ebase > info->exe_end)) { VG_(printf)("Executable range %p-%p is outside the\n" "acceptable range %p-%p\n", (void *)minaddr + ebase, (void *)maxaddr + ebase, (void *)info->exe_base, (void *)info->exe_end); return ENOMEM; } } /*XXX- for netbsd I am not sure if this check is right , info->exe_end etc are based on some assumptions which are not valid for linux, for example it assumes stack on top i think , this warrants more investigation. */ info->brkbase = mapelf(e, ebase); /* map the executable */ if (info->brkbase == 0) return ENOMEM; if(!interp) printf("interp is null\n"); else printf("interp = %p\n",interp); if (interp != NULL) { /* reserve a chunk of address space for interpreter */ SysRes res; Char* base = (Char *)info->exe_base; Char* baseoff; Int flags = VKI_MAP_PRIVATE|VKI_MAP_ANONYMOUS; if (info->map_base != 0) { base = (char *)VG_ROUNDUP(info->map_base, interp_align); flags |= VKI_MAP_FIXED; } printf("doing mmap here\n"); res = VG_(mmap_native)(base, interp_size, VKI_PROT_NONE, flags, -1, 0); check_mmap(res, base, interp_size); vg_assert(!res.isError); base = (Char*)res.val; baseoff = base - interp_addr; mapelf(interp, (ESZ(Addr))baseoff); printf("after mmap and mapelf here\n"); VG_(close)(interp->fd); entry = baseoff + interp->e.e_entry; info->interp_base = (ESZ(Addr))base; printf("info interp_base = %p\n " ,info->interp_base); //FIXME VG_(free)(interp->p); //FIXME VG_(free)(interp); } else entry = (void *)(ebase + e->e.e_entry); info->exe_base = minaddr + ebase; info->exe_end = maxaddr + ebase; info->init_eip = (Addr)entry; printf("info->init_eip: %x",info->init_eip); //FIXME VG_(free)(e->p); //FIXME VG_(free)(e); return 0;}
12192 /local/tlutelli/issta_data/temp/c/2005_temp/2005/12192/fe4746d6fba90584e880285ed6309b4d005a690a/m_ume.c/clean/branches/aspacem/valgrind/coregrind/m_ume.c
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError);
kdebug( KDEBUG_ERROR, 7102, "################# COULD NOT PUT %d",currentError);
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
KURL tmpurl( "ftp:/" );
KURL tmpurl( "ftp:/" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
int read_size = 0;
int read_size = 0;
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
setup_alarm( KProtocolManager::self().readTimeout() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
if ( t - t_last >= 1 ) {
if ( t - t_last >= 1 ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
while ( check( connection() ) )
while ( check( connection() ) ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
while ( check( &slave ) )
while ( check( &slave ) ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
}
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
if ( job.hasFinished() ) {
if ( job.hasFinished() ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
while( !job.hasFinished() )
while( !job.hasFinished() ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
if ( t - t_start >= 1 ) {
if ( t - t_start >= 1 ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
if ( ( e = ftp.ftpStat( udest_orig ) ) ) { if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size )
if ( ( e = ftp.ftpStat( udest_orig ) ) ) { if ( e->size == 0 ) { ftp.ftpDelete( udest_orig.path() ); } else if ( !_overwrite && !_resume ) { if ( e->size == _size ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
else
} else {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
}
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
} } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { if ( ! m_bMarkPartial )
} else if ( m_bMarkPartial ) { ftp.ftpRename( udest_orig.path(), udest_part.path() ); } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { if ( e->size == 0 ) { ftp.ftpDelete( udest_part.path() ); } else if ( !_overwrite && !_resume ) { if ( e->size == _size ) { error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); } else { error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); } ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } else if ( ! m_bMarkPartial ) {
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; }
}
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno );
kdebug( KDEBUG_ERROR, 7102, "Write Access denied for '%s' %d",udest.path(), errno );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() );
kdebug( KDEBUG_ERROR, 7102, "########## COULD NOT WRITE %s", udest.path().ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
} else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() );
} else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; KInstance instance( "kio_ftp" ); kdebug( KDEBUG_INFO, 7102, "Starting"); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); kdebug( KDEBUG_INFO, 7102, "Done");}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); kdebug( KDEBUG_INFO, 7102, "Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc( *soit ); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ kdebug( KDEBUG_INFO, 7102, "Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( !slave.isRunning() ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); kdebug( KDEBUG_INFO, 7102, "Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } kdebug( KDEBUG_INFO, 7102, "Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // kdebug( KDEBUG_INFO, 7102, "Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } kdebug( KDEBUG_INFO, 7102, "Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // kdebug( KDEBUG_INFO, 7102, "Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); kdebug( KDEBUG_INFO, 7102, "Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } kdebug( KDEBUG_INFO, 7102, "Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; kdebug( KDEBUG_INFO, 7102, "Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { kdebug( KDEBUG_INFO, 7102, "Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; kdebug( KDEBUG_INFO, 7102, "Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } kdebug( KDEBUG_INFO, 7102, "All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; kdebug( KDEBUG_INFO, 7102, "Iterating" ); soit = _source.begin(); kdebug( KDEBUG_INFO, 7102, "Looping" ); for( ; soit != _source.end(); ++soit ) { kdebug( KDEBUG_INFO, 7102, "Executing %s", (*soit).ascii() ); KURL usrc(*soit); kdebug( KDEBUG_INFO, 7102, "Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } kdebug( KDEBUG_INFO, 7102, "Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; kdebug( KDEBUG_INFO, 7102, "Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; kdebug( KDEBUG_INFO, 7102, "########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); kdebug( KDEBUG_INFO, 7102, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; kdebug( KDEBUG_INFO, 7102, "##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { kdebug( KDEBUG_INFO, 7102, "#%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ kdebug( KDEBUG_INFO, 7102, "=============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { //Return those as well //if ( e->name == "." || e->name == ".." ) // continue; kdebug( KDEBUG_INFO, 7102, "Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST ============" ); ftp.closedir(); kdebug( KDEBUG_INFO, 7102, "============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); kdebug( KDEBUG_INFO, 7102, "=============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } kdebug( KDEBUG_INFO, 7102, "=============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { kdebug( KDEBUG_INFO, 7102, " =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { kdebug( KDEBUG_INFO, 7102, "========== DONE DIR ========= %s", _url ); isDirectory(); } else { kdebug( KDEBUG_INFO, 7102, "========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/547d6e42e2ae74bd9bb4a448ba25b3249b059101/main.cc/buggy/kioslave/ftp/main.cc
fprintf(stderr, "man2html: out of memory\n");
cerr << "man2html: out of memory" << endl;
static char *stralloc(int len){ /* allocate enough for len + NULL */ char *news = new char [len+1];#ifdef SIMPLE_MAN2HTML if (!news) { fprintf(stderr, "man2html: out of memory\n"); exit(EXIT_FAILURE); }#else// modern compilers do not return a NULL pointer for a new#endif return news;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
fprintf(stderr,"Memory full, cannot output!");
cerr << "Memory full, cannot output!" << endl;
static void out_html(const char *c){ if (!c) return; // Added, probably due to the const? char *c2 = qstrdup(c); char *c3 = c2; static int obp=0; if (no_newline_output) { int i=0; no_newline_output=1; while (c2[i]) { if (!no_newline_output) c2[i-1]=c2[i]; if (c2[i]=='\n') no_newline_output=0; i++; } if (!no_newline_output) c2[i-1]=0; } if (scaninbuff) { while (*c2) { if (buffpos>=buffmax) { char *h = new char[buffmax*2]; #ifdef SIMPLE_MAN2HTML if (!h) { fprintf(stderr,"Memory full, cannot output!"); exit(1); }#else// modern compiler do not return a NULL for a new#endif memcpy(h, buffer, buffmax); delete [] buffer; buffer=h; buffmax=buffmax*2; } buffer[buffpos++]=*c2++; } } else if (output_possible) { while (*c2) { outbuffer[obp++]=*c2; if (*c=='\n' || obp >= HUGE_STR_MAX) { outbuffer[obp]='\0'; add_links(outbuffer); obp=0; } c2++; } } delete [] c3;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_character( char*& c ){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr ); // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse character name: " << name << endl;#endif return ""; } c++; } else if ( *c =='C' || c[1]== '\'' ) { // \C'name' c+=2; while ( *c && *c != '\'' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find('\''); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse (\\C mode) character name: " << name << endl;#endif return ""; } c++; } // Note: characters with a one character length name doe not exist, as they would collide with other escapes // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_characterDefinitionMap.find(name); if (it==s_characterDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find character with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Character with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_string(char*& c){ QCString name; if ( *c == '(' ) { // \*(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \*[long_name] Long name // Named character groff(7) // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse string name: " << name << endl;#endif return ""; } c++; } else { // \*a Name of one character name+=*c; c++; } // Now we have the name, let us find it between the string names QMap<QCString,StringDefinition>::iterator it=s_stringDefinitionMap.find(name); if (it==s_stringDefinitionMap.end()) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find string with name: " << name << endl;#endif // No output, as an undefined string is empty by default return ""; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "String with name: \"" << name << "\" => " << (*it).m_output << endl;#endif return (*it).m_output; }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_dollar_parameter(char*& c){ unsigned int argno = 0; // No dollar argument number yet! if ( *c == '0' ) { //kdDebug(7107) << "$0" << endl; c++; return s_dollarZero; } else if ( *c >= '1' && *c <= '9' ) { //kdDebug(7107) << "$ direct" << endl; argno = ( *c - '0' ); c++; } else if ( *c == '(' ) { //kdDebug(7107) << "$(" << endl; if ( c[1] && c[2] && c[1] >= '0' && c[1] <= '9' && c[2] >= '0' && c[2] <= '9' ) { argno = ( c[1] - '0' ) * 10 + ( c[2] - '0' ); c += 3; } else { if ( !c[1] ) c++; else if ( !c[2] ) c+=2; else c += 3; return ""; } } else if ( *c == '[' ) { //kdDebug(7107) << "$[" << endl; argno = 0; c++; while ( *c && *c>='0' && *c<='9' && *c!=']' ) { argno *= 10; argno += ( *c - '0' ); c++; } if ( *c != ']' ) { return ""; } c++; } else if ( ( *c == '*' ) || ( *c == '@' ) ) { const bool quote = ( *c == '@' ); QValueList<char*>::const_iterator it = s_argumentList.begin(); QCString param; bool space = false; for ( ; it != s_argumentList.end(); ++it ) { if (space) param += " "; if (quote) param += '\"'; // Not as HTML, as it could be used by macros ! param += (*it); if (quote) param += '\"'; // Not as HTML, as it could be used by macros! space = true; } c++; return param; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: unknown parameter $" << *c << endl;#endif return ""; } //kdDebug(7107) << "ARG $" << argno << endl; if ( !s_argumentList.isEmpty() && argno > 0 ) { //kdDebug(7107) << "ARG $" << argno << " OK!" << endl; argno--; if ( argno >= s_argumentList.size() ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find parameter $" << (argno+1) << endl;#endif return ""; } return s_argumentList[argno]; } return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_dollar_parameter(char*& c){ unsigned int argno = 0; // No dollar argument number yet! if ( *c == '0' ) { //kdDebug(7107) << "$0" << endl; c++; return s_dollarZero; } else if ( *c >= '1' && *c <= '9' ) { //kdDebug(7107) << "$ direct" << endl; argno = ( *c - '0' ); c++; } else if ( *c == '(' ) { //kdDebug(7107) << "$(" << endl; if ( c[1] && c[2] && c[1] >= '0' && c[1] <= '9' && c[2] >= '0' && c[2] <= '9' ) { argno = ( c[1] - '0' ) * 10 + ( c[2] - '0' ); c += 3; } else { if ( !c[1] ) c++; else if ( !c[2] ) c+=2; else c += 3; return ""; } } else if ( *c == '[' ) { //kdDebug(7107) << "$[" << endl; argno = 0; c++; while ( *c && *c>='0' && *c<='9' && *c!=']' ) { argno *= 10; argno += ( *c - '0' ); c++; } if ( *c != ']' ) { return ""; } c++; } else if ( ( *c == '*' ) || ( *c == '@' ) ) { const bool quote = ( *c == '@' ); QValueList<char*>::const_iterator it = s_argumentList.begin(); QCString param; bool space = false; for ( ; it != s_argumentList.end(); ++it ) { if (space) param += " "; if (quote) param += '\"'; // Not as HTML, as it could be used by macros ! param += (*it); if (quote) param += '\"'; // Not as HTML, as it could be used by macros! space = true; } c++; return param; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: unknown parameter $" << *c << endl;#endif return ""; } //kdDebug(7107) << "ARG $" << argno << endl; if ( !s_argumentList.isEmpty() && argno > 0 ) { //kdDebug(7107) << "ARG $" << argno << " OK!" << endl; argno--; if ( argno >= s_argumentList.size() ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find parameter $" << (argno+1) << endl;#endif return ""; } return s_argumentList[argno]; } return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_dollar_parameter(char*& c){ unsigned int argno = 0; // No dollar argument number yet! if ( *c == '0' ) { //kdDebug(7107) << "$0" << endl; c++; return s_dollarZero; } else if ( *c >= '1' && *c <= '9' ) { //kdDebug(7107) << "$ direct" << endl; argno = ( *c - '0' ); c++; } else if ( *c == '(' ) { //kdDebug(7107) << "$(" << endl; if ( c[1] && c[2] && c[1] >= '0' && c[1] <= '9' && c[2] >= '0' && c[2] <= '9' ) { argno = ( c[1] - '0' ) * 10 + ( c[2] - '0' ); c += 3; } else { if ( !c[1] ) c++; else if ( !c[2] ) c+=2; else c += 3; return ""; } } else if ( *c == '[' ) { //kdDebug(7107) << "$[" << endl; argno = 0; c++; while ( *c && *c>='0' && *c<='9' && *c!=']' ) { argno *= 10; argno += ( *c - '0' ); c++; } if ( *c != ']' ) { return ""; } c++; } else if ( ( *c == '*' ) || ( *c == '@' ) ) { const bool quote = ( *c == '@' ); QValueList<char*>::const_iterator it = s_argumentList.begin(); QCString param; bool space = false; for ( ; it != s_argumentList.end(); ++it ) { if (space) param += " "; if (quote) param += '\"'; // Not as HTML, as it could be used by macros ! param += (*it); if (quote) param += '\"'; // Not as HTML, as it could be used by macros! space = true; } c++; return param; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: unknown parameter $" << *c << endl;#endif return ""; } //kdDebug(7107) << "ARG $" << argno << endl; if ( !s_argumentList.isEmpty() && argno > 0 ) { //kdDebug(7107) << "ARG $" << argno << " OK!" << endl; argno--; if ( argno >= s_argumentList.size() ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find parameter $" << (argno+1) << endl;#endif return ""; } return s_argumentList[argno]; } return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_dollar_parameter(char*& c){ unsigned int argno = 0; // No dollar argument number yet! if ( *c == '0' ) { //kdDebug(7107) << "$0" << endl; c++; return s_dollarZero; } else if ( *c >= '1' && *c <= '9' ) { //kdDebug(7107) << "$ direct" << endl; argno = ( *c - '0' ); c++; } else if ( *c == '(' ) { //kdDebug(7107) << "$(" << endl; if ( c[1] && c[2] && c[1] >= '0' && c[1] <= '9' && c[2] >= '0' && c[2] <= '9' ) { argno = ( c[1] - '0' ) * 10 + ( c[2] - '0' ); c += 3; } else { if ( !c[1] ) c++; else if ( !c[2] ) c+=2; else c += 3; return ""; } } else if ( *c == '[' ) { //kdDebug(7107) << "$[" << endl; argno = 0; c++; while ( *c && *c>='0' && *c<='9' && *c!=']' ) { argno *= 10; argno += ( *c - '0' ); c++; } if ( *c != ']' ) { return ""; } c++; } else if ( ( *c == '*' ) || ( *c == '@' ) ) { const bool quote = ( *c == '@' ); QValueList<char*>::const_iterator it = s_argumentList.begin(); QCString param; bool space = false; for ( ; it != s_argumentList.end(); ++it ) { if (space) param += " "; if (quote) param += '\"'; // Not as HTML, as it could be used by macros ! param += (*it); if (quote) param += '\"'; // Not as HTML, as it could be used by macros! space = true; } c++; return param; } else {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: unknown parameter $" << *c << endl;#endif return ""; } //kdDebug(7107) << "ARG $" << argno << endl; if ( !s_argumentList.isEmpty() && argno > 0 ) { //kdDebug(7107) << "ARG $" << argno << " OK!" << endl; argno--; if ( argno >= s_argumentList.size() ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: cannot find parameter $" << (argno+1) << endl;#endif return ""; } return s_argumentList[argno]; } return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\n[.$] == " << s_argumentList.size() << endl; #endif
kdDebug(7107) << "\\n[.$] == " << s_argumentList.size() << endl;
static int read_only_number_register( const QCString& name ){ // Internal read-only variables if ( name == ".$" ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\n[.$] == " << s_argumentList.size() << endl;#endif return s_argumentList.size(); } else if ( name == ".g" ) return 0; // We are not groff(1) else if ( name == ".s" ) return current_size;#if 0 // ### TODO: map the fonts to a number else if ( name == ".f" ) return current_font;#endif else if ( name == ".P" ) return 0; // We are not printing else if ( name == ".A" ) return s_nroff;#ifndef SIMPLE_MAN2HTML // Special KDE KIO man: else if ( name == ".KDE_VERSION_MAJOR" ) return KDE_VERSION_MAJOR; else if ( name == ".KDE_VERSION_MINOR" ) return KDE_VERSION_MINOR; else if ( name == ".KDE_VERSION_RELEASE" ) return KDE_VERSION_RELEASE; else if ( name == ".KDE_VERSION" ) return KDE_VERSION;#endif // ### TODO: should .T be set to "html"? But we are not the HTML post-processor. :-( // ### TODO: groff defines much more read-only number registers#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "EXCEPTION: unknown read-only number register: " << name << endl;#endif return 0; // Undefined variable}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static int scan_number_register( char*& c){ int sign = 0; // Sign for auto-increment (if any) switch (*c) { case '+': sign = 1; c++; break; case '-': sign = -1; c++; break; default: break; } QCString name; if ( *c == '[' ) { c++; if ( *c == '+' ) { sign = 1; c++; } else if ( *c == '-' ) { sign = -1; c++; } while ( *c && *c != ']' && *c != '\n' ) { // ### TODO: a \*[string] could be inside and should be processed name+=*c; c++; } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse number register name: " << name << endl;#endif return 0; } c++; } else if ( *c == '(' ) { c++; if ( *c == '+' ) { sign = 1; c++; } else if ( *c == '-' ) { sign = -1; c++; } name+=c[0]; name+=c[1]; c+=2; } else { name = *c; c++; } if ( name[0] == '.' ) { return read_only_number_register( name ); } else { QMap< QCString, NumberDefinition >::iterator it = s_numberDefinitionMap.find( name ); if ( it == s_numberDefinitionMap.end() ) { return 0; // Undefined variable } else { (*it).m_value += sign * (*it).m_increment; return (*it).m_value; } }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static int scan_number_register( char*& c){ int sign = 0; // Sign for auto-increment (if any) switch (*c) { case '+': sign = 1; c++; break; case '-': sign = -1; c++; break; default: break; } QCString name; if ( *c == '[' ) { c++; if ( *c == '+' ) { sign = 1; c++; } else if ( *c == '-' ) { sign = -1; c++; } while ( *c && *c != ']' && *c != '\n' ) { // ### TODO: a \*[string] could be inside and should be processed name+=*c; c++; } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse number register name: " << name << endl;#endif return 0; } c++; } else if ( *c == '(' ) { c++; if ( *c == '+' ) { sign = 1; c++; } else if ( *c == '-' ) { sign = -1; c++; } name+=c[0]; name+=c[1]; c+=2; } else { name = *c; c++; } if ( name[0] == '.' ) { return read_only_number_register( name ); } else { QMap< QCString, NumberDefinition >::iterator it = s_numberDefinitionMap.find( name ); if ( it == s_numberDefinitionMap.end() ) { return 0; // Undefined variable } else { (*it).m_value += sign * (*it).m_increment; return (*it).m_value; } }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
name = *c;
name += *c;
static int scan_number_register( char*& c){ int sign = 0; // Sign for auto-increment (if any) switch (*c) { case '+': sign = 1; c++; break; case '-': sign = -1; c++; break; default: break; } QCString name; if ( *c == '[' ) { c++; if ( *c == '+' ) { sign = 1; c++; } else if ( *c == '-' ) { sign = -1; c++; } while ( *c && *c != ']' && *c != '\n' ) { // ### TODO: a \*[string] could be inside and should be processed name+=*c; c++; } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse number register name: " << name << endl;#endif return 0; } c++; } else if ( *c == '(' ) { c++; if ( *c == '+' ) { sign = 1; c++; } else if ( *c == '-' ) { sign = -1; c++; } name+=c[0]; name+=c[1]; c+=2; } else { name = *c; c++; } if ( name[0] == '.' ) { return read_only_number_register( name ); } else { QMap< QCString, NumberDefinition >::iterator it = s_numberDefinitionMap.find( name ); if ( it == s_numberDefinitionMap.end() ) { return 0; // Undefined variable } else { (*it).m_value += sign * (*it).m_increment; return (*it).m_value; } }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_font( char*& c ){ QCString name; if ( *c == '(' ) { // \f(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \f[long_name] Long name // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse font name: " << name << endl;#endif return ""; } c++; } else { // \fa Font name with one character or one digit // ### HACK do *not* use: name = *c; or name would be empty name += *c; c++; } kdDebug(7107) << "FONT NAME: " << name << endl; // Now we have the name, let us find the font bool ok = false; const unsigned int number = name.toUInt( &ok ); if ( ok ) { if ( number < 5 ) { const char* fonts[] = { "R", "I", "B", "BI", "CR" }; // Regular, Italic, Bold, Bold Italic, Courier regular name = fonts[ number ]; } else { kdDebug(7107) << "EXCEPTION: font has too big number: " << name << " => " << number << endl; name = "R"; // Let assume Regular } } else if ( name.isEmpty() ) { kdDebug(7107) << "EXCEPTION: font has no name: " << name << endl; name = "R"; // Let assume Regular } if ( !skip_escape ) return set_font( name ); else return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_font( char*& c ){ QCString name; if ( *c == '(' ) { // \f(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \f[long_name] Long name // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse font name: " << name << endl;#endif return ""; } c++; } else { // \fa Font name with one character or one digit // ### HACK do *not* use: name = *c; or name would be empty name += *c; c++; } kdDebug(7107) << "FONT NAME: " << name << endl; // Now we have the name, let us find the font bool ok = false; const unsigned int number = name.toUInt( &ok ); if ( ok ) { if ( number < 5 ) { const char* fonts[] = { "R", "I", "B", "BI", "CR" }; // Regular, Italic, Bold, Bold Italic, Courier regular name = fonts[ number ]; } else { kdDebug(7107) << "EXCEPTION: font has too big number: " << name << " => " << number << endl; name = "R"; // Let assume Regular } } else if ( name.isEmpty() ) { kdDebug(7107) << "EXCEPTION: font has no name: " << name << endl; name = "R"; // Let assume Regular } if ( !skip_escape ) return set_font( name ); else return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static QCString scan_named_font( char*& c ){ QCString name; if ( *c == '(' ) { // \f(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \f[long_name] Long name // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse font name: " << name << endl;#endif return ""; } c++; } else { // \fa Font name with one character or one digit // ### HACK do *not* use: name = *c; or name would be empty name += *c; c++; } kdDebug(7107) << "FONT NAME: " << name << endl; // Now we have the name, let us find the font bool ok = false; const unsigned int number = name.toUInt( &ok ); if ( ok ) { if ( number < 5 ) { const char* fonts[] = { "R", "I", "B", "BI", "CR" }; // Regular, Italic, Bold, Bold Italic, Courier regular name = fonts[ number ]; } else { kdDebug(7107) << "EXCEPTION: font has too big number: " << name << " => " << number << endl; name = "R"; // Let assume Regular } } else if ( name.isEmpty() ) { kdDebug(7107) << "EXCEPTION: font has no name: " << name << endl; name = "R"; // Let assume Regular } if ( !skip_escape ) return set_font( name ); else return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static QCString scan_named_font( char*& c ){ QCString name; if ( *c == '(' ) { // \f(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \f[long_name] Long name // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse font name: " << name << endl;#endif return ""; } c++; } else { // \fa Font name with one character or one digit // ### HACK do *not* use: name = *c; or name would be empty name += *c; c++; } kdDebug(7107) << "FONT NAME: " << name << endl; // Now we have the name, let us find the font bool ok = false; const unsigned int number = name.toUInt( &ok ); if ( ok ) { if ( number < 5 ) { const char* fonts[] = { "R", "I", "B", "BI", "CR" }; // Regular, Italic, Bold, Bold Italic, Courier regular name = fonts[ number ]; } else { kdDebug(7107) << "EXCEPTION: font has too big number: " << name << " => " << number << endl; name = "R"; // Let assume Regular } } else if ( name.isEmpty() ) { kdDebug(7107) << "EXCEPTION: font has no name: " << name << endl; name = "R"; // Let assume Regular } if ( !skip_escape ) return set_font( name ); else return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
kdDebug(7107) << "FONT NAME: " << name << endl;
static QCString scan_named_font( char*& c ){ QCString name; if ( *c == '(' ) { // \f(ab Name of two characters if ( c[1] == escapesym ) { QCString cstr; c = scan_escape_direct( c+2, cstr );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "\(" << cstr << endl;#endif // ### FIXME: check if we have really 2 characters (and only 2, not more either) name = cstr.left(2); } else { name+=c[1]; name+=c[2]; c+=3; } } else if ( *c == '[' ) { // \f[long_name] Long name // We must find the ] to get a name c++; while ( *c && *c != ']' && *c != '\n' ) { if ( *c == escapesym ) { QCString cstr; c = scan_escape_direct( c+1, cstr ); const int result = cstr.find(']'); if ( result == -1 ) name += cstr; else { // Note: we drop the characters after the ] name += cstr.left( result ); } } else { name+=*c; c++; } } if ( !*c || *c == '\n' ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Found linefeed! Could not parse font name: " << name << endl;#endif return ""; } c++; } else { // \fa Font name with one character or one digit // ### HACK do *not* use: name = *c; or name would be empty name += *c; c++; } kdDebug(7107) << "FONT NAME: " << name << endl; // Now we have the name, let us find the font bool ok = false; const unsigned int number = name.toUInt( &ok ); if ( ok ) { if ( number < 5 ) { const char* fonts[] = { "R", "I", "B", "BI", "CR" }; // Regular, Italic, Bold, Bold Italic, Courier regular name = fonts[ number ]; } else { kdDebug(7107) << "EXCEPTION: font has too big number: " << name << " => " << number << endl; name = "R"; // Let assume Regular } } else if ( name.isEmpty() ) { kdDebug(7107) << "EXCEPTION: font has no name: " << name << endl; name = "R"; // Let assume Regular } if ( !skip_escape ) return set_font( name ); else return "";}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifdef SIMPLE_MAN2HTML fprintf(stderr, "man2html: unknown operator %c.\n", oper); #else
static char *scan_expression(char *c, int *result){ int value=0,value2,sign=1,opex=0; char oper='c'; if (*c=='!') { c=scan_expression(c+1, &value); value= (!value); } else if (*c=='n') { c++; value=s_nroff; } else if (*c=='t') { c++; value=1-s_nroff; } else if (*c=='\'' || *c=='"' || *c<' ' || (*c=='\\' && c[1]=='(')) { /* ?string1?string2? ** test if string1 equals string2. */ char *st1=NULL, *st2=NULL, *h; char *tcmp=NULL; char sep; sep=*c; if (sep=='\\') { tcmp=c; c=c+3; } c++; h=c; while (*c!= sep && (!tcmp || qstrncmp(c,tcmp,4))) c++; *c='\n'; scan_troff(h, 1, &st1); *c=sep; if (tcmp) c=c+3; c++; h=c; while (*c!=sep && (!tcmp || qstrncmp(c,tcmp,4))) c++; *c='\n'; scan_troff(h,1,&st2); *c=sep; if (!st1 && !st2) value=1; else if (!st1 || !st2) value=0; else value=(!qstrcmp(st1, st2)); delete [] st1; delete [] st2; if (tcmp) c=c+3; c++; } else { while (*c && !isspace(*c) && *c!=')' && opex >= 0) { opex=0; switch (*c) { case '(': c=scan_expression(c+1, &value2); value2=sign*value2; opex=1; break; case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { int num=0,denum=1; value2=0; while (isdigit(*c)) value2=value2*10+((*c++)-'0'); if (*c=='.' && isdigit(c[1])) { c++; while (isdigit(*c)) { num=num*10+((*c++)-'0'); denum=denum*10; } } if (isalpha(*c)) { /* scale indicator */ switch (*c) { case 'i': /* inch -> 10pt */ value2=value2*10+(num*10+denum/2)/denum; num=0; break; default: break; } c++; } value2=value2+(num+denum/2)/denum; value2=sign*value2; opex=1; if (*c=='.') opex = -1; } break; case '\\': c=scan_escape(c+1); value2=intresult*sign; if (isalpha(*c)) c++; /* scale indicator */ opex=1; break; case '-': if (oper) { sign=-1; c++; break; } case '>': case '<': case '+': case '/': case '*': case '%': case '&': case '=': case ':': if (c[1]=='=') oper=(*c++) +16; else oper=*c; c++; break; default: c++; break; } if (opex > 0) { sign=1; switch (oper) { case 'c': value=value2; break; case '-': value=value-value2; break; case '+': value=value+value2; break; case '*': value=value*value2; break; case '/': if (value2) value=value/value2; break; case '%': if (value2) value=value%value2; break; case '<': value=(value<value2); break; case '>': value=(value>value2); break; case '>'+16: value=(value>=value2); break; case '<'+16: value=(value<=value2); break; case '=': case '='+16: value=(value==value2); break; case '&': value = (value && value2); break; case ':': value = (value || value2); break; default: {#ifdef SIMPLE_MAN2HTML fprintf(stderr, "man2html: unknown operator %c.\n", oper);#else kdDebug(7107) << "Unknown operator " << char(oper) << endl;#endif } } oper=0; } } if (*c==')') c++; } *result=value; return c;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static char *scan_expression(char *c, int *result){ int value=0,value2,sign=1,opex=0; char oper='c'; if (*c=='!') { c=scan_expression(c+1, &value); value= (!value); } else if (*c=='n') { c++; value=s_nroff; } else if (*c=='t') { c++; value=1-s_nroff; } else if (*c=='\'' || *c=='"' || *c<' ' || (*c=='\\' && c[1]=='(')) { /* ?string1?string2? ** test if string1 equals string2. */ char *st1=NULL, *st2=NULL, *h; char *tcmp=NULL; char sep; sep=*c; if (sep=='\\') { tcmp=c; c=c+3; } c++; h=c; while (*c!= sep && (!tcmp || qstrncmp(c,tcmp,4))) c++; *c='\n'; scan_troff(h, 1, &st1); *c=sep; if (tcmp) c=c+3; c++; h=c; while (*c!=sep && (!tcmp || qstrncmp(c,tcmp,4))) c++; *c='\n'; scan_troff(h,1,&st2); *c=sep; if (!st1 && !st2) value=1; else if (!st1 || !st2) value=0; else value=(!qstrcmp(st1, st2)); delete [] st1; delete [] st2; if (tcmp) c=c+3; c++; } else { while (*c && !isspace(*c) && *c!=')' && opex >= 0) { opex=0; switch (*c) { case '(': c=scan_expression(c+1, &value2); value2=sign*value2; opex=1; break; case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { int num=0,denum=1; value2=0; while (isdigit(*c)) value2=value2*10+((*c++)-'0'); if (*c=='.' && isdigit(c[1])) { c++; while (isdigit(*c)) { num=num*10+((*c++)-'0'); denum=denum*10; } } if (isalpha(*c)) { /* scale indicator */ switch (*c) { case 'i': /* inch -> 10pt */ value2=value2*10+(num*10+denum/2)/denum; num=0; break; default: break; } c++; } value2=value2+(num+denum/2)/denum; value2=sign*value2; opex=1; if (*c=='.') opex = -1; } break; case '\\': c=scan_escape(c+1); value2=intresult*sign; if (isalpha(*c)) c++; /* scale indicator */ opex=1; break; case '-': if (oper) { sign=-1; c++; break; } case '>': case '<': case '+': case '/': case '*': case '%': case '&': case '=': case ':': if (c[1]=='=') oper=(*c++) +16; else oper=*c; c++; break; default: c++; break; } if (opex > 0) { sign=1; switch (oper) { case 'c': value=value2; break; case '-': value=value-value2; break; case '+': value=value+value2; break; case '*': value=value*value2; break; case '/': if (value2) value=value/value2; break; case '%': if (value2) value=value%value2; break; case '<': value=(value<value2); break; case '>': value=(value>value2); break; case '>'+16: value=(value>=value2); break; case '<'+16: value=(value<=value2); break; case '=': case '='+16: value=(value==value2); break; case '&': value = (value && value2); break; case ':': value = (value || value2); break; default: {#ifdef SIMPLE_MAN2HTML fprintf(stderr, "man2html: unknown operator %c.\n", oper);#else kdDebug(7107) << "Unknown operator " << char(oper) << endl;#endif } } oper=0; } } if (*c==')') c++; } *result=value; return c;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static void request_while( char*& c, int j, bool mdoc ){ // ### TODO: .break and .continue#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Entering .while" << endl;#endif c += j; char* newline = skip_till_newline( c ); const char oldchar = *newline; *newline = 0; // We store the full .while stuff into a QCString as if it would be a macro const QCString macro = c ;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "'Macro' of .while" << endl << macro << endl;#endif // Prepare for continuing after .while loop end *newline = oldchar; c = newline; // Process -while loop const bool oldwhileloop = s_whileloop; s_whileloop = true; int result = true; // It must be an int due to the call to scan_expression while ( result ) { // Unlike for a normal macro, we have the condition at start, so we do not need to prepend extra bytes char* liveloop = qstrdup( macro.data() );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Scanning .while condition" << endl;#endif char* end_expression = scan_expression( liveloop, &result ); if ( result ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "New .while iteration" << endl;#endif // The condition is true, so call the .while's content char* help = end_expression + 1; while ( *help && ( *help == ' ' || *help == '\t' ) ) ++help; if ( ! *help ) { // We have a problem, so stop .while result = false; break; } if ( mdoc ) scan_troff_mandoc( help, false, 0 ); else scan_troff( help, false, 0 ); } delete[] liveloop; } // s_whileloop = oldwhileloop;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Ending .while" << endl;#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static void request_while( char*& c, int j, bool mdoc ){ // ### TODO: .break and .continue#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Entering .while" << endl;#endif c += j; char* newline = skip_till_newline( c ); const char oldchar = *newline; *newline = 0; // We store the full .while stuff into a QCString as if it would be a macro const QCString macro = c ;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "'Macro' of .while" << endl << macro << endl;#endif // Prepare for continuing after .while loop end *newline = oldchar; c = newline; // Process -while loop const bool oldwhileloop = s_whileloop; s_whileloop = true; int result = true; // It must be an int due to the call to scan_expression while ( result ) { // Unlike for a normal macro, we have the condition at start, so we do not need to prepend extra bytes char* liveloop = qstrdup( macro.data() );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Scanning .while condition" << endl;#endif char* end_expression = scan_expression( liveloop, &result ); if ( result ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "New .while iteration" << endl;#endif // The condition is true, so call the .while's content char* help = end_expression + 1; while ( *help && ( *help == ' ' || *help == '\t' ) ) ++help; if ( ! *help ) { // We have a problem, so stop .while result = false; break; } if ( mdoc ) scan_troff_mandoc( help, false, 0 ); else scan_troff( help, false, 0 ); } delete[] liveloop; } // s_whileloop = oldwhileloop;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Ending .while" << endl;#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#ifndef SIMPLE_MAN2HTML
static void request_while( char*& c, int j, bool mdoc ){ // ### TODO: .break and .continue#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Entering .while" << endl;#endif c += j; char* newline = skip_till_newline( c ); const char oldchar = *newline; *newline = 0; // We store the full .while stuff into a QCString as if it would be a macro const QCString macro = c ;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "'Macro' of .while" << endl << macro << endl;#endif // Prepare for continuing after .while loop end *newline = oldchar; c = newline; // Process -while loop const bool oldwhileloop = s_whileloop; s_whileloop = true; int result = true; // It must be an int due to the call to scan_expression while ( result ) { // Unlike for a normal macro, we have the condition at start, so we do not need to prepend extra bytes char* liveloop = qstrdup( macro.data() );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Scanning .while condition" << endl;#endif char* end_expression = scan_expression( liveloop, &result ); if ( result ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "New .while iteration" << endl;#endif // The condition is true, so call the .while's content char* help = end_expression + 1; while ( *help && ( *help == ' ' || *help == '\t' ) ) ++help; if ( ! *help ) { // We have a problem, so stop .while result = false; break; } if ( mdoc ) scan_troff_mandoc( help, false, 0 ); else scan_troff( help, false, 0 ); } delete[] liveloop; } // s_whileloop = oldwhileloop;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Ending .while" << endl;#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp
#endif
static void request_while( char*& c, int j, bool mdoc ){ // ### TODO: .break and .continue#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Entering .while" << endl;#endif c += j; char* newline = skip_till_newline( c ); const char oldchar = *newline; *newline = 0; // We store the full .while stuff into a QCString as if it would be a macro const QCString macro = c ;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "'Macro' of .while" << endl << macro << endl;#endif // Prepare for continuing after .while loop end *newline = oldchar; c = newline; // Process -while loop const bool oldwhileloop = s_whileloop; s_whileloop = true; int result = true; // It must be an int due to the call to scan_expression while ( result ) { // Unlike for a normal macro, we have the condition at start, so we do not need to prepend extra bytes char* liveloop = qstrdup( macro.data() );#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Scanning .while condition" << endl;#endif char* end_expression = scan_expression( liveloop, &result ); if ( result ) {#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "New .while iteration" << endl;#endif // The condition is true, so call the .while's content char* help = end_expression + 1; while ( *help && ( *help == ' ' || *help == '\t' ) ) ++help; if ( ! *help ) { // We have a problem, so stop .while result = false; break; } if ( mdoc ) scan_troff_mandoc( help, false, 0 ); else scan_troff( help, false, 0 ); } delete[] liveloop; } // s_whileloop = oldwhileloop;#ifndef SIMPLE_MAN2HTML kdDebug(7107) << "Ending .while" << endl;#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/02847f87e5aaf12517fc3417ea75ced7e8699eff/man2html.cpp/clean/kioslave/man/man2html.cpp